In [1]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
from PIL import ImageChops
from PIL import ImageOps
import numpy as np
import os
from operator import itemgetter
In [84]:
# 이미지 파일 경로
im1 = "./data/IMG_4139.jpg"
im2 = "./data/IMG_4164.jpg"
In [85]:
# 이미지 파일 로드
img1 = Image.open(im1)
img2 = Image.open(im2)
In [86]:
print "file name is %s" %img1.filename
print " - format is %s" %img1.format
print " - color mode is %s" %img1.mode
print " - dpi is %s" %str(img1.info['dpi'])
print " - size is %s" %str(img1.size)
print
print
print "file name is %s" %img2.filename
print " - format is %s" %img2.format
print " - color mode is %s" %img2.mode
print " - dpi is %s" %str(img2.info['dpi'])
print " - size is %s" %str(img2.size)
In [87]:
img1_np = np.asarray(img1)
img2_np = np.asarray(img2)
print "the dimension of %s is %s" %(img1.filename, str(img1_np.shape))
print "the dimension of %s is %s" %(img2.filename, str(img2_np.shape))
In [88]:
# img1 : <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3264x2448 at 0x1058E2DD0>
# img1_np : numpy array
# 둘 다 plt.imshow로 출력됨
# fig size setting
plt.figure(figsize=(16,12))
plt.subplot(121)
plt.axis('off')
plt.imshow(img1)
plt.subplot(122)
plt.axis('off')
plt.imshow(img2_np)
Out[88]:
In [89]:
# Create JPEG thumbnails
outfile = os.path.splitext(img1.filename)[0] + ".thumbnail.jpg"
size = (128, 128)
img1.thumbnail(size)
img1.save(outfile, "JPEG")
In [90]:
# 썸네일이 정상적으로 생성됨
!ls -al ./data/*.thumbnail.jpg
In [91]:
# Copying a subrectangle from an image
# 이미지 파일 로드
img1 = Image.open(im1)
img2 = Image.open(im2)
print "the original size of %s is %s" %(im1, str(img1.size))
print "the original size of %s is %s" %(im2, str(img2.size))
# The region is defined by a 4-tuple, where coordinates are (left, upper, right, lower).
box = (0, 0, 300, 200)
region1 = img1.crop(box)
region2 = img2.crop(box)
print "the size of %s is %s after subrectangle" %(im1, str(region2.size))
print "the size of %s is %s after subrectangle" %(im2, str(region2.size))
plt.figure(figsize=(16,12))
plt.subplot(121)
plt.axis('off')
plt.imshow(region1)
plt.subplot(122)
plt.axis('off')
plt.imshow(region2)
Out[91]:
In [92]:
# transpose
# 이미지 파일 로드
img1 = Image.open(im1)
img2 = Image.open(im2)
box = (0, 0, 300, 200)
region = img2.crop(box)
region1 = region.transpose(Image.ROTATE_180)
region2 = region.transpose(Image.ROTATE_270)
# img2.paste(region, img1)
# im.paste(region, img1)
plt.figure(figsize=(16,12))
plt.subplot(121)
plt.axis('off')
plt.imshow(region1)
plt.subplot(122)
plt.axis('off')
plt.imshow(region2)
Out[92]:
In [93]:
# Splitting and merging bands
im = Image.open(im1)
r, g, b = im.split()
im = Image.merge("RGB", (r, g, b))
plt.figure(figsize=(16,12))
plt.subplot(1,3,1)
plt.axis('off')
plt.imshow(im)
im = Image.merge("RGB", (b, r, g))
plt.subplot(1,3,2)
plt.axis('off')
plt.imshow(im)
im = Image.merge("RGB", (g, b, r))
plt.subplot(1,3,3)
plt.axis('off')
plt.imshow(im)
Out[93]: