-
首先要有两张图:
1)目标图片:
image.png
2)
image.png
from PIL import Image
boy = Image.open('C:\\Users\\Mr.R\\Pictures\\111.jpg')
boy = boy.convert('RGBA')
print(boy.getpixel((0,0)))
w, h = boy.size
for x in range(0, w):
for y in range(0, h):
r, g, b, a = boy.getpixel((x, y))
if g-r==0 and g-b==0: #若是底色为白色(255, 255, 255, 255)
#如果底色为绿色则:
#g-r>0 and g-b>0:
a=0 #改成完全透明
boy.putpixel((x, y), (r, g, b, a)) #设置像素颜色
boy.save('C:\\Users\\Mr.R\\Pictures\\111-without-background.png')
from PIL import Image
dx = Image.open('C:\\Users\\Mr.R\\Pictures\\111-without-background.png')
hc = Image.open('C:\\Users\\Mr.R\\Pictures\\112.jpg')
w, h = dx.size
dx = dx.resize( (w//7, h//7) )#缩小倍数
hc.paste(dx, (120,-10), mask=dx.split()[3])#存放的坐标可以调整
hc.save('C:\\Users\\Mr.R\\Pictures\\111-with-new-background.jpg')
所以先提取中间的花,然后将其放在指定的坐标上!
image.png
网友评论