记录学习PIL库图像处理(一)

from PIL import Image
import numpy as np
im = Image.open(r"2.jpg").convert(‘RGBA’)
im2 = Image.open(r"3.jpg").convert(‘RGBA’)

#图像格式,大小,宽,高
print(im2.mode,im2.size,im2.width,im2.height)
#Image.rotate(angle, resample=0, expand=0, center=None, translate=None, fillcolor=None)
im.rotate(45).show() #逆时针旋转45°图像
r, g, b = Image.open(r"2.jpg").split() #图像要为RGB格式且返回的是图像,即分离图像
#合并图像
Image.merge(‘RGB’, [r, g, b]).show()
#在图im上附加一层im2图像,图像大小要一致且为RGBA格式,4x8位像素,带透明蒙版的真彩色
Image.alpha_composite(im, im2).show()
#合并两张图像,两张图像的格式和大小需要一致。透明度alpha接近0返回第一张图,接近1返回第二张图
#out = image1 * (1.0 - alpha) + image2 * alpha
Image.blend(im, im2, 0.5).show()
#透明蒙版混合图像。图像相同的模式和大小,b的格式必须是“ 1”或“ L”或“ RGBA”,大小相同
Image.composite(im, im2, b).show()
Image.eval(im, lambda x:x0.5).show() # 对该图像每个像素点进行0.5处理,即按像素缩放
#Image.new(模式,大小,颜色= 0)创建新图像
new_im = Image.new(‘RGB’, im.size, ‘red’)
#图像解析为像素矩阵,再变换回图像
a = np.asarray(im)
b = np.array(im)
Image.fromarray(a,mode=‘RGB’).show()
#未理解的
Image.frombytes(‘RGBA’,im.size,a).show()
Image.frombuffer(‘RGBA’,im.size,a).show()

官网学习地址

上一篇:边框与圆角


下一篇:HTML5 纯CSS3实现正方体旋转3D效果