安裝

pip install pillow

In [3]:
from PIL import Image
In [91]:
img_origin = Image.open('sample.jpg')
img = img_origin.copy()
img
Out[91]:
In [5]:
img.size
Out[5]:
(720, 480)
In [44]:
img_gray = img.convert('L')
img_gray.save('sample_gray.jpg')
img_gray
Out[44]:
In [46]:
img.rotate(45)
Out[46]:
In [14]:
img.crop((100, 50, 700, 150))
Out[14]:

對 pixel 分別做處理

In [92]:
img.getpixel((1, 1)) #(w, h)
Out[92]:
(15, 34, 28)
In [93]:
%matplotlib inline
import matplotlib.pyplot as plt
In [94]:
pixel_count = img.histogram()
R_count = pixel_count[:256]
G_count = pixel_count[256:2*256]
B_count = pixel_count[2*256:3*256]

plt.figure(1)
plt.subplot(311)
plt.bar([i for i in range(256)], R_count, 1, color='red')
plt.subplot(312)
plt.bar([i for i in range(256)], G_count, 1, color='green')
plt.subplot(313)
plt.bar([i for i in range(256)], B_count, 1, color='blue')
Out[94]:
<Container object of 256 artists>

加強

In [95]:
def f(x):
    if x < 30:
        return 0
    if x > 225:
        return 255
    return (x-30) / (225 - 30) * 255

img = img_origin.copy()
W, H = img.size
for w in range(W):
    for h in range(H):
        r, g, b = img.getpixel((w, h))
        r = int(f(r))
        g = int(f(g))
        b = int(f(b))
        img.putpixel((w, h), (r, g, b))
img
Out[95]:
In [96]:
pixel_count = img.histogram()
R_count = pixel_count[:256]
G_count = pixel_count[256:2*256]
B_count = pixel_count[2*256:3*256]

plt.figure(1)
plt.subplot(311)
plt.bar([i for i in range(256)], R_count, 1, color='red')
plt.subplot(312)
plt.bar([i for i in range(256)], G_count, 1, color='green')
plt.subplot(313)
plt.bar([i for i in range(256)], B_count, 1, color='blue')
Out[96]:
<Container object of 256 artists>

調亮

對 rgb 都做「開根號 * 16」的運算
int(根號(0) * 16) = 0
int(根號(255) * 16) = 255
經過運算之後還會在 0~255 的範圍內

In [99]:
def f2(x):
    return int((x ** 0.5) * 16)

img = img_origin.copy()
W, H = img.size
for w in range(W):
    for h in range(H):
        r, g, b = img.getpixel((w, h))
        r = f2(r)
        g = f2(g)
        b = f2(b)
        img.putpixel((w, h), (r, g, b))
img
Out[99]:
In [100]:
pixel_count = img.histogram()
R_count = pixel_count[:256]
G_count = pixel_count[256:2*256]
B_count = pixel_count[2*256:3*256]

plt.figure(1)
plt.subplot(311)
plt.bar([i for i in range(256)], R_count, 1, color='red')
plt.subplot(312)
plt.bar([i for i in range(256)], G_count, 1, color='green')
plt.subplot(313)
plt.bar([i for i in range(256)], B_count, 1, color='blue')
Out[100]:
<Container object of 256 artists>
In [101]:
def f2(x):
    return int((x ** 0.5) * 16)

def f3(x):
    if x < 100:
        return 0
    return int((x-100) / (255-100) * 255)

im = img_origin.copy()
W, H = img.size
for w in range(W):
    for h in range(H):
        r, g, b = img.getpixel((w, h))
        r = f3(f2(r))
        g = f3(f2(g))
        b = f3(f2(b))
        img.putpixel((w, h), (r, g, b))
img
Out[101]:
In [102]:
pixel_count = img.histogram()
R_count = pixel_count[:256]
G_count = pixel_count[256:2*256]
B_count = pixel_count[2*256:3*256]

plt.figure(1)
plt.subplot(311)
plt.bar([i for i in range(256)], R_count, 1, color='red')
plt.subplot(312)
plt.bar([i for i in range(256)], G_count, 1, color='green')
plt.subplot(313)
plt.bar([i for i in range(256)], B_count, 1, color='blue')
Out[102]:
<Container object of 256 artists>

真。Enhancement

In [135]:
from PIL import ImageEnhance
In [136]:
img = img_origin.copy()
enhancer = ImageEnhance.Contrast(img)
In [137]:
enhancer.enhance(1)
Out[137]:
In [133]:
enhancer.enhance(1.5)
Out[133]:
In [109]:
enhancer.enhance(2)
Out[109]:
In [114]:
enhancer = ImageEnhance.Color(img)
In [120]:
enhancer.enhance(0.7)
Out[120]:
In [121]:
enhancer.enhance(1.3)
Out[121]:
In [122]:
enhancer = ImageEnhance.Brightness(img)
In [123]:
enhancer.enhance(0.7)
Out[123]:
In [125]:
enhancer.enhance(1.3)
Out[125]: