Skip to content

Instantly share code, notes, and snippets.

@rkdgusrn1212
Last active December 4, 2021 10:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rkdgusrn1212/5eb95c0c019e280f07269967017c4f38 to your computer and use it in GitHub Desktop.
Save rkdgusrn1212/5eb95c0c019e280f07269967017c4f38 to your computer and use it in GitHub Desktop.
Image Gamma Correction 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("lenna_gray.png")#grayscale로 변환된 흑백 이미지를 출력
row = gray_img.size[0]
col = gray_img.size[1]
gamma1 = 0.88
gamma2 = 1.15
result_img1 = Image.new("L", (row, col))#새 흑백이미지를 생성.
result_img2 = Image.new("L", (row, col))#새 흑백이미지를 생성.
for x in range(1 , row):
for y in range(1, col):
value = pow(gray_img.getpixel((x,y))[0]/255,(1/gamma1))*255
if value >= 255 :
value = 255
result_img1.putpixel((x,y), int(value))
value = pow(gray_img.getpixel((x,y))[0]/255,(1/gamma2))*255
if value >= 255 :
value = 255
result_img2.putpixel((x,y), int(value))
result_img1.save("gamma_088.png")#감마 0.88 이미지 저장
result_img2.save("gamma_115.png")#감마 1.15 이미지 저장
plt.figure(figsize=(15,10))#그래프 크기 지정
y = gray_img.histogram()
y = y[0:256]
x = np.arange(len(y))
plt.subplot(221)#2x2의 첫째서브
plt.title("lenna gray hist")
plt.bar(x, y)
plt.subplot(222)#둘째 서브
y = result_img1.histogram()
x = np.arange(len(y))
plt.title("gamma 0.88 hist")
plt.bar(x, y)
plt.subplot(223)#셋째 서브
y = result_img2.histogram()
x = np.arange(len(y))
plt.title("gamma 1.15 hist")
plt.bar(x, y)
fig = plt.gcf()
plt.show()
fig.savefig('lenna_gamma_correction_hist.png')#이미지로 히스토그램을 저장
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment