用python实现图片的缩放,裁剪,颜色转换,图像增强,调节亮度

用python实现图片的缩放,颜色转换,图像增强,调节亮度等。

from PIL import Image
import matplotlib.pyplot as plt
import os
import matplotlib.image as mpimg
from scipy import misc
from skimage import data, exposure, img_as_float, color
from PIL import Image,ImageFilter
import numpy as np
from PIL import ImageEnhance


#img = Image.open(r'C:\Users\Desktop\1.jpg')
img = Image.open(r'C:\Users\Desktop\1.jpg')
#裁剪
crop_box=(0,0,300,200)
img1=img.crop(crop_box)
#plt.imshow(img1)
#旋转
#img_rotate=img.rotate(20, expand = True)
img_rotate=img.rotate(20)
plt.imshow(img_rotate)
plt.imshow(img_rotate,'Greys_r')
img_rotate.save('C:\\Users\\Desktop\\coal_rotate.jpg')
#缩放
img_resize1 = img.resize((150, 150))
plt.imshow(img_resize1,'Greys_r')
img_resize1.save('C:\\Users\\Desktop\\coal_resize.jpg')
#亮度增加
image=img_as_float(img)   #转换成指定格式
figure_adjust_low=exposure.adjust_gamma(image,2)   #图片调暗
figure_adjust_high=exposure.adjust_gamma(image,0.5) #图片调亮
plt.subplot(311)
plt.imshow(figure_adjust_low)
plt.imshow(figure_adjust_low,'Greys_r')
plt.axis('off')  # 不显示坐标轴
plt.subplot(312)
plt.imshow(figure_adjust_high)
plt.imshow(figure_adjust_high,'Greys_r')
plt.axis('off')  # 不显示坐标轴
plt.show()
#改变颜色
img2=Image.open('C:\\Users\\Desktop\\3.jpg')
image_color=np.array(img2)
figure_adjust=color.convert_colorspace(image_color,'rgb','HSV')
plt.subplot(212)
plt.imshow(figure_adjust)
plt.axis('off')  # 不显示坐标轴
plt.show()
#对比度增强
enh_con = ImageEnhance.Contrast(img2)
contrast = 6
img_contrasted = enh_con.enhance(contrast)
#亮度增强
enh_con = ImageEnhance.Brightness(img2)
Bright = 2
img_Bright = enh_con.enhance(Bright)
img_Bright.show()
#色度增强
enh_con = ImageEnhance.Color(img2)
color = 5
img_Color = enh_con.enhance(color)
img_Color.show()

猜你喜欢

转载自blog.csdn.net/yuzhongmanbu99/article/details/123965939