본문 바로가기
Development/Python

[Python] 파이썬 icnsutil 모듈을 이용한 MacOS 아이콘 만들기

by 선인장 🌵 2022. 8. 30.
728x90
728x90

파이썬 icnsutil 모듈을 이용한 MacOS 아이콘 만들기

MacOS에서 애플리케이션을 제작 시 아이콘에 대해서는 일반적인 이미지 파일이 아닌 Apple에서만 사용해야 하는 아이콘 포맷이 있다.

예전에 간단히 Golang을 통해서 만들면서 MacOS에 적용되는 아이콘을 만드는 방법에 대해서 알아보고자 한다.

 

[Golang] MacOS CPU / Memory MeunBar Tray App

MacOS CPU / Memory MeunBar Tray App MacBook을 사용하면서 간간히 현재 CPU / Memory 사용량을 확인해야 하는 경우가 있다. 이런 경우 여러 무료로 오픈된 App이나, 유료로 나온 App을 사용하면 되겠지만 그냥..

happylie.tistory.com

[Python] 파이썬 icnsutil 모듈을 이용한 MacOS 아이콘 만들기

1. icnsutil 모듈 설치

우선 사용을 하기 위해서는 icnsutil 모듈을 설치해야 한다.

pip 명령어를 통해서 설치해보도록 하자.

  • $ pip install icnsutil
# pip를 이용한 icnsutil 모듈 설치

$ pip install icnsutil  
Collecting icnsutil
  Using cached icnsutil-1.0.1-py3-none-any.whl (20 kB)
Installing collected packages: icnsutil
Successfully installed icnsutil-1.0.1

$ pip freeze | grep "icnsutil"
icnsutil==1.0.1
 

icnsutil

A fully-featured python library to handle reading and writing icns files.

pypi.org

728x90

2. icnsutil 모듈 이용하기

이제 icnsutil 모듈을 설치했으면 파이썬 코드를 이용하여 icons 파일로 변경 및 추출, 정보 확인을 해보도록 하자.

2.1 이미지 파일을 icons 파일로 변환하기

Apple에서 사용하는 OS는 독자적인 icons 파일이 존재하며, 그에 따른 여러 가이드라인이 존재한다.

 

App Icons - Foundations - Human Interface Guidelines - Design - Apple Developer

App icons A unique, memorable icon communicates the purpose and personality of your experience and can help people recognize your app or game at a glance in the App Store and on their devices. Beautiful app icons are an important part of the user experienc

developer.apple.com

 

Apple Design Resources

Design apps quickly by using Sketch and Photoshop templates, plug-ins, and preconfigured UI elements.

developer.apple.com

그래서 웹브라우저에서 사용하는 icon 파일 혹은 일반 이미지 파일을 이용해서는 MacOS 애플리케이션에 아이콘으로 사용하기 쉽지 않다.

물론 여러 방법을 이용해서 아이콘을 제작할 수 있으나, 이번에는 이미지 파일을 쉽게 변환하여 사용할 수 있도록 작성하였다.

status.png
0.02MB
AppIcon.icns
0.02MB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import icnsutil

# 1. 이미지 파일을 icons 파일로 변환하기
org_file = './status.png'       # 원본 이미지 파일
icons_file = './AppIcon.icns'   # Apple icons 파일로 변환

imgtoicns = icnsutil.IcnsFile()
try:
    imgtoicns.add_media(file=org_file)
    imgtoicns.write(icons_file)
except Exception as err:
    print(err)
    sys.exit(0)

2.2 icons 파일에서 이미지 파일로 추출하기

반대로 MacOS에서 사용하던 애플리케이션의 아이콘 파일을 이미지 파일로 추출할 수도 있다.

app.icns
0.24MB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import icnsutil

# 2. icons 파일에서 이미지 파일로 추출하기
org_file = './app.icns'   # 원본 Apple icons 파일로 변환
extract_dir = './'        # 추출 이미지 위치
try:
    img = icnsutil.IcnsFile(org_file)
    img.export(extract_dir, allowed_ext='png', recursive=True, convert_png=True)
except Exception as err:
    print(err)
    sys.exit(0)

추출하게 되면 해당 icon에 사용된 내용으로 추출된 이미지를 확인할 수 있다.

2.3 icons 파일 정보 확인하기

2.3.1 description 함수 이용
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import icnsutil

# 3. icons 파일 정보 확인하기
# 3.1 description 함수 이용
org_file = './app.icns'         # Chrome Apple icons 파일
org_file1 = './AppIcon.icns'    # 제작한 Apple icons 파일
img_file = './status.png'       # 일반 이미지 파일

try:
    desc1 = icnsutil.IcnsFile.description(org_file)
    desc2 = icnsutil.IcnsFile.description(org_file1)
    desc3 = icnsutil.IcnsFile.description(img_file)
    print('desc1 description : {desc1}'.format(desc1=desc1))
    print('desc2 description : {desc2}'.format(desc2=desc2))
    print('desc3 description : {desc3}'.format(desc3=desc3))
except Exception as err:
    print(err)
    sys.exit(0)
    

### OUTPUT ###
desc1 description : TOC : 64 bytes, bin: TOC 
is32: 627 bytes, rgb: 16x16
s8mk: 256 bytes, mask: 16x16
il32: 2465 bytes, rgb: 32x32
l8mk: 1024 bytes, mask: 32x32
it32: 25597 bytes, rgb: 128x128
t8mk: 16384 bytes, mask: 128x128
ic08: 50304 bytes, png: 256x256
ic09: 157876 bytes, png: 512x512

desc2 description : ic09: 19401 bytes, png: 512x512

desc3 description : Not an ICNS file, missing "icns" header.
2.3.2 description 함수 이용
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import icnsutil

# 3. icons 파일 정보 확인하기
# 3.2 verify 함수 이용
org_file = './app.icns'         # Chrome Apple icons 파일
org_file1 = './AppIcon.icns'    # 제작한 Apple icons 파일
img_file = './status.png'       # 일반 이미지 파일

try:
    desc1 = icnsutil.IcnsFile.verify(org_file)
    desc2 = icnsutil.IcnsFile.verify(org_file1)
    desc3 = icnsutil.IcnsFile.verify(img_file)
    print('desc1 verify : {desc1}'.format(desc1=list(desc1)))
    print('desc2 verify : {desc2}'.format(desc2=list(desc2)))
    print('desc3 verify : {desc3}'.format(desc3=list(desc3)))
except Exception as err:
    print(err)
    sys.exit(0)
    

### OUTPUT ###
desc1 verify : []
desc2 verify : []
desc3 verify : ['Not an ICNS file, missing "icns" header.']
728x90

3. 예제 Code

이번에는 icnsutil 모듈을 이용하여 이미지 파일을 MacOS 애플리케이션에 사용 가능한 아이콘으로 만드는 방법에 대해서 알아보았다.

위에 나온 예제 파일은 Github에 올려놓았다.

해당 내용에 대해서 문제가 있거나 궁금한 내용이 있다면 Github Issus 혹은 댓글로 남겨주면 답변하도록 하겠다.

 

GitHub - happylie/python-example-code: Python Example Code

Python Example Code. Contribute to happylie/python-example-code development by creating an account on GitHub.

github.com

728x90
728x90


🌵댓글

 

loading