Contents
Tutorials
- Python Tutorial
- NumPy Tutorial
- Matplotlib Tutorial
- PyQt5 Tutorial
- BeautifulSoup Tutorial
- xlrd/xlwt Tutorial
- Pillow Tutorial
- Googletrans Tutorial
- PyWin32 Tutorial
- PyAutoGUI Tutorial
- Pyperclip Tutorial
- TensorFlow Tutorial
- Tips and Examples
Pillow 이미지 합치기¶
Pillow의 open(), new(), paste() 메서드를 사용해서 여러개의 이미지를 하나의 이미지로 저장할 수 있습니다.
아래의 손글씨 숫자 이미지 (200px × 200px)를 하나의 이미지로 병합해 보겠습니다.


두 개의 이미지 합치기¶
from PIL import Image
# 1. 병합할 이미지 만들기
merged = Image.new('L', (400, 200))
# 2. 이미지 불러오기
im0 = Image.open('mnist_0.png')
im1 = Image.open('mnist_1.png')
# 3. 이미지 붙여넣기
merged.paste(im0, (0, 0))
merged.paste(im1, (200, 0))
# 4. 병합한 이미지 저장하기
merged.save('mnist_merged.png')
결과는 아래와 같습니다.

합쳐진 이미지 (400px × 200px).¶
이번에는 손글씨 숫자 10개의 이미지를 두 줄로 배치해서 병합해 보겠습니다.
10개의 이미지 합치기¶
from PIL import Image
# 1. 병합할 이미지 만들기
merged = Image.new('L', (200 * 5, 200 * 2))
for i in range(2):
for j in range(5):
# 2. 이미지 불러오기
im = Image.open('mnist_' + str(5 * i + j) + '.png')
# 3. 이미지 붙여넣기
merged.paste(im, (200 * j, 200 * i))
# 4. 병합한 이미지 저장하기
merged.save('mnist_merged2.png')
결과는 아래와 같습니다.

합쳐진 이미지 (1000px × 400px).¶
이전글/다음글
이전글 : Pillow 픽셀 접근하기
다음글 : Pillow Reference