1. 简单地读入图片、显示图片
from PIL import Image
import matplotlib.pyplot as plt
img=Image.open('background.jpg')
plt.imshow(img)
plt.show()
2. 对图像中像素点的预处理
1)获取图像尺寸 width,height
2)data是一个1D的list,长度是width*height,list的每个元素都是1个反映该位置像素点颜色的tuple: (R,G,B)
3)pixels是一个2D的list,len(pixels)==height and len(pixels[0])==width. 同样地,其list的每个元素都是1个tuple: (R,G,B)
width, height = img.size
data = list(img.getdata())
## pixels: a 2D list, len(pixels)==height and len(pixels[0])==width
## pixels的每个元素都是1个反映该像素点颜色的tuple:(R,G,B)
pixels = [data[i * width: (i + 1) * width] for i in range(height)]
3. 处理指定位置的像素点并显示结果
def double_red(img):
return [[ (min(255,pixel[0]*2), pixel[1], pixel[2]) for pixel in row] for row in img]
res_img = double_red(pixels)
plt.imshow(res_img)
plt.show()
4. 处理前后对比图