반응형
from PIL import Image
PIL Image 모듈을 사용하기 위해서 위와 같이 import하는 것이 편하다.
img = Image.open('./help.jpeg')

이렇게 이미지를 불러오면 RGB 채널을 가진 3 channel의 이미지를 불러온다.
np.array(img).shape
#(225, 225, 3)
numpy array로 변환해서 확인하면 다음과 같이 확인할 수 있다.
이미지를 grayscale 그러니까 1 channel로 불러오고 싶으면 다음과 같이 변환을 시키면 된다.
img = Image.open('./help.jpeg').convert('L')
np.array(img).shape
#(225, 225)

혹은 grayscale로 불러온 이미지를 다시 RGB 이미지로 변환시키고 싶으면 다음과 같이 변환 시키면 된다.
img = img.convert('RGB')
np.array(img).shape
#(225, 225, 3)
References
https://pillow.readthedocs.io/en/stable/reference/Image.html
Image Module
The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, a...
pillow.readthedocs.io
반응형
'Coding' 카테고리의 다른 글
[PyQt5] QTreeView와 QFileSystemModel 활용(1) (0) | 2024.02.12 |
---|---|
[Python] Matplotlib을 사용해서 논문에 들어갈 그래프 완성도있게 그리기 (1) (0) | 2023.07.11 |
[Pytorch] 임의의 배열/텐서 만들기 (0) | 2023.03.23 |
[Python] 파이썬 스크립트로 실행파일 만들기 2 PyInstaller (0) | 2023.02.23 |
[Python] 파이썬 스크립트로 실행파일 만들기 PyInstaller (0) | 2023.02.22 |