반응형
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

 

반응형

+ Recent posts