Skip to content

Instantly share code, notes, and snippets.

@rkdgusrn1212
Last active May 15, 2019 14:15
Show Gist options
  • Save rkdgusrn1212/99ff767746da9dd949d3865fa12fd4ef to your computer and use it in GitHub Desktop.
Save rkdgusrn1212/99ff767746da9dd949d3865fa12fd4ef to your computer and use it in GitHub Desktop.
Image Sliding using Pillow in Python
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
gray_img = Image.open('Lenna.png').convert("LA")#밝기와 알파값을 이용해서 Grayscale로 변환
gray_img.save("gray_lenna.png")#grayscale로 변환된 흑백 이미지를 출력
row = gray_img.size[0]
col = gray_img.size[1]
sliding_up_img = Image.new("L", (row, col))#새 흑백이미지를 생성.
sliding_down_img = Image.new("L", (row, col))#새 흑백이미지를 생성.
sliding_up = 50
sliding_down = 50
for x in range(1 , row):
for y in range(1, col):
sliding_up_img.putpixel((x,y), gray_img.getpixel((x,y))[0]+sliding_up)
sliding_down_img.putpixel((x,y), gray_img.getpixel((x,y))[0]-sliding_down)
sliding_up_img.save("sliding_up_lenna.png")#위로 슬라이딩된 이미지 저장
sliding_down_img.save("sliding_down_lenna.png")#아래로 슬라이딩된 이미지 저장
y = gray_img.histogram()
y = y[0:256]
x = np.arange(len(y))
plt.figure(figsize=(8,24))
plt.subplot(311)
plt.title("gray hist")
plt.bar(x, y)
y = sliding_up_img.histogram()
x = np.arange(len(y))
plt.subplot(312)
plt.title("sliding up hist")
plt.bar(x, y)
y = sliding_down_img.histogram()
x = np.arange(len(y))
plt.subplot(313)
plt.title("sliding down hist")
plt.bar(x, y)
fig = plt.gcf()
plt.show()
fig.savefig("sliding_hist.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment