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)


file name is ./data/IMG_4139.jpg
 - format is JPEG
 - color mode is RGB
 - dpi is (72, 72)
 - size is (3264, 2448)


file name is ./data/IMG_4164.jpg
 - format is JPEG
 - color mode is RGB
 - dpi is (72, 72)
 - size is (3264, 2448)

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))


the dimension of ./data/IMG_4139.jpg is (2448, 3264, 3)
the dimension of ./data/IMG_4164.jpg is (2448, 3264, 3)

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]:
<matplotlib.image.AxesImage at 0x14d744890>

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


-rw-r--r--@ 1 dikien  staff  3455  5 13 12:52 ./data/IMG_4139.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)


the original size of ./data/IMG_4139.jpg is (3264, 2448)
the original size of ./data/IMG_4164.jpg is (3264, 2448)
the size of ./data/IMG_4139.jpg is (300, 200) after subrectangle
the size of ./data/IMG_4164.jpg is (300, 200) after subrectangle
Out[91]:
<matplotlib.image.AxesImage at 0x14d797ad0>

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]:
<matplotlib.image.AxesImage at 0x12e306990>

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]:
<matplotlib.image.AxesImage at 0x12e77c690>