Python PIL实现彩色图像的裁剪和粘贴!!
from numpy import *
from PIL import Image
img=Image.open('..\\1.jpg') #打开图像
img_size = img.size
w = img_size[0]
h = img_size[1]
x1=0
y1=int(h/3)
x2=w
y2=2*int(h/3)
box=(x1,y1,x2,y2)
roi=img.crop(box) # 截取彩色图像的一部分
#roi.save("..\\2.png")
img2 = Image.new('RGB',(w,h),(0,0,0)) # 创建新彩色图像 全黑
img2.paste(roi,box) # 将一个图像复制到另一个图像的指定位置
img2.save("..\\3.png") #保存图像
结果:
原始图像 ----------> 处理结果