Pillow는 파이썬으로 이미지 파일을 조작하는 모듈이다.

 

컴퓨터의 이미지 파일에는 픽셀의 좌표가 있고 색상값이 있다. 특히 색의 삼원색을 나타내는 RGB 값의 범위를

 

(0,0,0) 검은색

(255,255,255) 하얀색

 

(255,0,0) 빨간색 RED

(0,255,0) 녹색   GREEN

(0,0,255) 파란색 BLUE

 

해서 RGB 이다. 여기서 투명도(알파값)를 더한 것을 RGBA 라고 한다.

 

예를 들어 (255,0,0,255)  은 불투명한 빨간색이고 (255,0,0,0) 완전 투명한 빨간색이다. 완전 투명하면 빨간색은 보이지 않는다.

 

벡터 그래픽이란 것도 있으나 이 포스팅에서 다루지는 않는다. 웹상에서 많이 쓰는 JPG와 PNG 파일은 픽셀(모니터의 점)로 이루어져 있다.

 

Pillow 의 설치는 아래와 같다.

 

https://pillow.readthedocs.io/en/stable/installation.html

 

Installation — Pillow (PIL Fork) 7.2.0 documentation

Build flags: --disable-zlib, --disable-jpeg, --disable-tiff, --disable-freetype, --disable-lcms, --disable-webp, --disable-webpmux, --disable-jpeg2000, --disable-imagequant, --disable-xcb. Disable building the corresponding feature even if the development

pillow.readthedocs.io

상세한 튜토리얼은 리드더독스를 참고하면 좋다.

 

이미지를 처리하는 기능은 대부분 거의 비슷하다. 기본 개념인 확대, 회전, 편집, 복사 등은 다 있다. 다른 언어나 다른 라이브러리를 사용해도 이미지 사용법은 거의 비슷하다. 파일을 열어서 로드하고, 인스턴스로 이미지를 조작한다

 

파이썬 같은 객체지향언어에서는 보통 이미지 파일을 불러와서 객체에 저장한다. 객체에는 픽셀들이 배열되고, 파일에 대한 정보를 담아놓는다. 클래스에는 이미지를 조작 가능한 메서드들이 들어있다.

 

예제> JPG 이미지를 불러와서 png gif 로 저장한다. 원본 그림에서 오려낸 이미지를 저장한다.

 

from PIL import Image
import os

os.chdir("D:\\")
catImg = Image.open('kitty.jpg')
width, height = catImg.size
print("width  : ", width)
print("height : ", height)
print("file name : ", catImg.filename)
print("format : ", catImg.format_description)
catImg.save('kitty.png')
catImg.save('kitty.gif')

newCat = catImg.crop((350,100,950,700))
newCat.save('cropped.png')

 

픽사베이 같은 곳에 이미지가 많으니까 실습에 사용하기 좋다.

 

이미지 객체에는 이미지의 사이즈, 파일이름, 포맷 등을 저장한다.

 

인스턴스에서 save 메소드만 불러와도 확장자에 따라 저장이 된다.

 

jpg 파일을 열어서 .png 라고 확장자를 바꿔주는 것 만으로도 모듈이 알아서 변환하기 때문에 편리하다. 파일 이름만 바뀐게 아니라 실제 변환을 해서 저장한다.

 

파일 확장자가 다르면 용량도 달라진다. 포맷을 변환했기 때문이다.

 

Crop 메소드는 파일을 오려준다. 오린 이미지의 일부를 다른 파일에 저장하면 된다.

 

 

 

위와 같이 원본파일에서 오린다.

 

기본 파일 오픈하는 조작이다. 역시 상세한 내용에 대해서는 온라인 문서화가 잘 되어 있으니 웹문서를 참고한다.

 

https://pillow.readthedocs.io/en/stable/index.html

 

Pillow — Pillow (PIL Fork) 7.2.0 documentation

© Copyright 1995-2011 Fredrik Lundh, 2010-2020 Alex Clark and Contributors Revision 2bd74943.

pillow.readthedocs.io

파이썬 Pillow 에도 다양한 이미지 필터가 있다. 픽사베이 등의 이미지를 가지고 논다는 느낌으로 하나씩 테스트하다보면 금방 익숙해 질 것이다.

 

https://ko.wikipedia.org/wiki/Python_Imaging_Library

 

Python Imaging Library - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. Python Imaging Library(PIL)은 파이썬 인터프리터에 다양한 이미지 파일 형식을 지원하고 강력한 이미지 처리와 그래픽 기능을 제공하는 자유-오픈 소스 소프트웨어 �

ko.wikipedia.org

http://zetcode.com/python/pillow/

 

Pillow tutorial - Python image programming with Pillow

Pillow tutorial last modified July 6, 2020 Pillow tutorial shows how to use Pillow in Python to work with images. The sources are available at the author's Github repository. Pillow Pillow is a Python Imaging Library (PIL), which adds support for opening,

zetcode.com

https://www.tutorialspoint.com/python_pillow/python_pillow_quick_guide.htm

 

Python Pillow - Quick Guide - Tutorialspoint

Python Pillow - Quick Guide Python Pillow - Overview In today’s digital world, we come across lots of digital images. In case, we are working with Python programming language, it provides lot of image processing libraries to add image processing capabili

www.tutorialspoint.com

좋은 튜토리얼이 많으니까 어렵지 않게 배울 수 있을 것이다.

공유하기

facebook twitter kakaoTalk kakaostory naver band