改变图像亮度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24477135/article/details/86144964
import torch
import torch.nn as nn
from PIL import Image
import matplotlib.pyplot as plt
from skimage import data, exposure, img_as_float
import cv2

def change_img(img, h, w, A , b):
    for xi in range(0, w):
        for xj in range(0, h):
            # set the pixel value decrease to 20%
            img[xj, xi, 0] = int(img[xj, xi, 0] * A + b)
            img[xj, xi, 1] = int(img[xj, xi, 0] * A + b)
            img[xj, xi, 2] = int(img[xj, xi, 0] * A + b)

    return img

def Illumi_adjust(alpha, img):
  if alpha > 0 :
    img_out = img * (1 - alpha) + alpha * 255.0
  else:
    img_out = img * (1 + alpha)
  return img_out/255.0

img = Image.open('d:/000459.jpg')
img1 = Image.open('d:/000081.jpg')
# plt.figure()
# plt.imshow(img)
# plt.show()

# img=cv2.imread('d:/000459.jpg')
# img1 = img
# plt.figure()
# plt.imshow(img)
# plt.show()
# w = img.shape[1]
# h = img.shape[0]
# ii = 0
# gam1= exposure.adjust_gamma(img, 3)
# gam2= exposure.adjust_gamma(img, 2)
# gam3= exposure.adjust_gamma(img, 1)
# gam4= exposure.adjust_gamma(img, 0.8)
# gam5= exposure.adjust_gamma(img, 0.5)
# gam6= exposure.adjust_gamma(img, 0.3)

# gam1 = change_img(img, h, w, 1.0, -7)
# gam2 = change_img(img, h, w, 1.0, -4)
# gam3 = change_img(img, h, w, 1.0, -2)
# gam4 = change_img(img, h, w, 1.0, 2)
# gam5 = change_img(img, h, w, 1.0, 5)
# gam6 = change_img(img, h, w, 1.0, 8)

# gam1 = Illumi_adjust(-0.555, img)
# gam2 = Illumi_adjust(-0.3333, img)
# gam3 = Illumi_adjust(-0.13333, img)
# gam4 = Illumi_adjust(0.2222, img)
# gam5 = Illumi_adjust(0.4444, img)
# gam6 = Illumi_adjust(0.6666, img)
plt.subplot(271)  # 2行2列第1个图
plt.title("original")
plt.imshow(img)

gam1 = img.point(lambda x:x*2)
gam1.save("D:\\gam1.jpg")

plt.subplot(272)  # 2行2列第1个图
plt.imshow(gam1)

gam2 = img.point(lambda x:x*1.6)
gam2.save("D:\\gam2.jpg")
plt.subplot(273)  # 2行2列第1个图
plt.imshow(gam2)

gam3 = img.point(lambda x:x*1.2)
gam3.save("D:\\gam3.jpg")
plt.subplot(274)  # 2行2列第1个图
plt.imshow(gam3)


gam4 = img.point(lambda x:x*1.0)
gam4.save("D:\\gam4.jpg")
plt.subplot(275)  # 2行2列第1个图
plt.imshow(gam4)

gam5 = img.point(lambda x:x*0.8)
gam5.save("D:\\gam5.jpg")
plt.subplot(276)  # 2行2列第1个图
plt.imshow(gam5)

gam6 = img.point(lambda x:x*0.6)
gam6.save("D:\\gam6.jpg")
plt.subplot(277)  # 2行2列第1个图
plt.imshow(gam6)
##############################

################################
plt.subplot(278)  # 2行2列第1个图
plt.title("original")
plt.imshow(img1)

tgam1 = img1.point(lambda x:x*2)
tgam1.save("D:\\tgam1.jpg")

plt.subplot(279)  # 2行2列第1个图
plt.imshow(tgam1)

tgam2 = img1.point(lambda x:x*1.6)
tgam2.save("D:\\tgam2.jpg")
plt.subplot(2,7,10)  # 2行2列第1个图
plt.imshow(tgam2)

tgam3 = img1.point(lambda x:x*1.2)
tgam3.save("D:\\tgam3.jpg")
plt.subplot(2,7,11)  # 2行2列第1个图
plt.imshow(tgam3)


tgam4 = img1.point(lambda x:x*1.0)
tgam4.save("D:\\tgam4.jpg")
plt.subplot(2,7,12)  # 2行2列第1个图
plt.imshow(tgam4)

tgam5 = img1.point(lambda x:x*0.8)
tgam5.save("D:\\tgam5.jpg")
plt.subplot(2,7,13)  # 2行2列第1个图
plt.imshow(tgam5)

tgam6 = img1.point(lambda x:x*0.6)
tgam6.save("D:\\tgam6.jpg")
plt.subplot(2,7,14)  # 2行2列第1个图
plt.imshow(tgam6)
#gam1.save('d:\gam1.jpg')
#cv2.imwrite("D:\\gam1.jpg", gam1)
plt.show()

利用PIL中的Image读取图像,并利用point操作改变图像亮度,具体可以参考链接:python PIL 图像处理库简介(一)

结果如下:

猜你喜欢

转载自blog.csdn.net/qq_24477135/article/details/86144964