Python Image Library - Make area of image transparent

[亡魂溺海] 提交于 2019-12-01 09:25:48

You can do this way.

img = Image.open("withAlpha.png")
p = img.load()

for y in range(2):
    for x in range(img.size[0]):
        t = list(p[x,y])
        t[3] = 0
        p[x,y] = tuple(t)

img.save("result.png")

I would do it the following way:

img = Image.open("myimage.png")
p = img.load()
alpha = img.split()[-1]
width, height = img.size
for y in range(2): #First two rows
    for x in range(width): #The whole row
        alpha[x, y] = 0
img.putalpha(alpha)

I hope this works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!