파이썬의 이미지 프로세싱은 Computer Vision과 Deep Learning 연구자들과 개발자들이 많이 사용하고 있으며,
라이브러리 생태계는 지속적으로, 발전하고 있고 요구되고 있습니다.
하지만, 여전히 오픈 소스 생태계에서 제공되는 라이브러리들의 기능으로는 섬세한 이미지 편집을 지원하지 못합니다.
즉 앞으로도 꾸준히 파이썬 이미지 프로세싱 라이브러리의 생태계는 기여와 발전이 필요합니다.
저는 유연하고 자유로운 이미지 편집을 파이썬에서 수행하기 위해 Pillow와 Wand, OpenCV를 병행하여 사용하였으며,
때로는 지원하지 않는 기능이나 버그로 인해 오픈 소스 생태계의 코드를 직접 수정하고 기여한 적이 있습니다.
이를 위해서 이미지 프로세싱의 깊은 이해를 바탕으로, 이미지 프로세싱 코드를 직접 다루어야 했습니다.
오늘 여러분에게 제가 겪었던 경험과 시행착오들을 짧고 간결하게 정리하여 공유드리고자 합니다.
저의 경험을 양분으로 여러분에게 어떤 인사이트가 되었으면 좋겠습니다.
5. 이 이미지는 RGB 형식이고
컴퓨터는 이것을 바이트 단위로
매트릭스(Matrix)라고 부르는
배열 형태로 저장합니다.
이미지 프로세싱이란
(166, 219, 216)
(176, 176, 152)
(150, 140, 111)
(147, 131, 112)
(135, 173, 159)
(199, 160, 127)
(199, 172, 155)
(209, 198, 189)
(131, 141, 115)
(180, 142, 108)
(173, 153, 150)
(204, 189, 176)
(95, 144, 134)
(125, 127, 107)
(154, 142, 132)
(187, 169, 154)
6. 이미지 프로세싱이란
우리는 다양한 수식을 이용하여 이미지를 변환(Transformation)할 수 있고
이것을 이미지 프로세싱(Image Processing)이라 부릅니다.
𝟏
𝟏
𝟏
𝟎
𝟎𝟎
𝟎 𝟎
𝟎 𝟏
𝟏
𝟏
𝑿
𝒀𝟎
𝟎 𝟎
𝟎 𝑾
𝑯
𝟏
𝟎
𝟎𝟎
𝟎 𝟎
𝟎
Original Translate Scale
𝒄𝒐𝒔𝜽
𝟏
𝟎
𝟎
𝟎 𝟎
𝒄𝒐𝒔𝜽
𝒔𝒊𝒏𝜽
−𝒔𝒊𝒏𝜽
Rotate
7. 동영상 처리
이미지 프로세싱이란
이미지 프로세싱은 다양한 종류가 있으며
사진, 그림, 동영상에 따라서 이미지 프로세싱이 여러 갈래로 나눠집니다.
Image Processing
사진, 그림 처리
기하학적 변환 색조화, 양자화, 색변환 패턴 인식, 특징 검출 압축 노이즈 제거 영상 분할, 접합
8. 이미지 프로세싱이란
오늘은 그 중에서 사진, 그림과 같은 2차원 데이터의
기하학적 변환에 대해서 얘기하고자 합니다.
기하학적 변환
9. 기하학적 변환 연산
기하학적 변환이란 이미지의 크기나 위치 변경, 혹은 회전 등의 변화를 가하는 것으로
어파인(Affine) 변환 혹은 원근(Perspective) 변환 등이 있습니다.
이동
(Translate)
회전
(Rotate)
10. from PIL import Image
img = Image.open('./cat.jpg')
x = 100
y = 50
a = 1
b = 0
c = -x # move the img 100 to the right
d = 0
e = 1
f = y # move the img 50 to the up
translate = img.transform(img.size, Image.AFFINE, (a, b, c, d, e, f))
translate.show()
Pillow Affine Translate
𝟏
𝟏
𝟏
𝑿
𝒀𝟎
𝟎 𝟎
𝟎
Translate
11. from PIL import Image
import math
img = Image.open('./cat.jpg')
angle = math.radians(45) # 45 degree
a = math.cos(angle)
b = math.sin(angle)
c = 0
d = -math.sin(angle)
e = math.cos(angle)
f = 0
rotate = img.transform(img.size, Image.AFFINE, (a, b, c, d, e, f), resample=Image.BILINEAR)
rotate.show()
𝒄𝒐𝒔𝜽
𝟏
𝟎
𝟎
𝟎 𝟎
𝒄𝒐𝒔𝜽
𝒔𝒊𝒏𝜽
−𝒔𝒊𝒏𝜽
Rotate
Pillow Affine Rotate
29. Wand
from wand.image import Image
from wand.display import display
with Image(filename='cat.jpg') as img:
print(img.size)
for r in 1, 2, 3:
with img.clone() as i:
i.resize(int(i.width * r * 0.25), int(i.height * r * 0.25))
i.rotate(90 * r)
i.save(filename='cat-{0}.jpg'.format(r))
display(i)
파이썬 라이브러리 Wand를 통해 ImageMagick을 사용할 수 있습니다.
39. convert -size 320x120 xc:lightblue
-draw "fill tomato circle 250,30 310,30
fill limegreen circle 55,75 15,80
font Candice font-size 72 decorate UnderLine
fill dodgerblue stroke navy stroke-width 2
translate 10,110 rotate -15 text 0,0 ' Anthony '"
draw_mvg.gif
Test with ImageMagick
ImageMagick 명령어로 동작 확인
40. Test with Python Wand
파이썬 Wand에서 테스트
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image
with Image(width=320, height=100, background=Color('lightblue')) as image:
with Drawing() as draw:
draw.font = 'Candice'
draw.font_size = 72.0
draw.text_decoration = 'underline'
draw.fill_color = Color('black')
draw.text(28, 68, 'Anthony')
draw(image)
image.save(filename='test.jpg')