Python叠加矩形框图层叠加矩形框图层2种方法及效果种方法及效果
两种方式以及效果:
方式一,使用方式一,使用PIL.Image.blend方式:方式:
from PIL import Image, ImageDraw
im = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
im2 = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
draw = ImageDraw.Draw(im2)
draw.rectangle([(1000, 500), (1200, 800)], fill=(255, 0, 0), width=2)
out = Image.blend(im, im2, 0.5)
out.save('d:/tmp/demo1.jpg')
im.close()
im2.close()
out.show()
out.close()
方式二:直接进行像素叠加:方式二:直接进行像素叠加:
from PIL import Image, ImageDraw
im = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
b = (255, 0, 0)
opacity = 0.5
for x in range(1000, 1200):
for y in range(500, 800):
p = im.getpixel((x, y))
p = [int(p[i]*(1-opacity) + b[i]*opacity) for i in range(3)] im.putpixel((x, y), tuple(p))
im.save('d:/tmp/demo2.jpg')
im.show()
im.close()
两种的效果图都如下:
评论0
最新资源