[英]Adding a transparent circle to an image on python with PIL
我有一個python程序,該程序可在png文件上放一個圓圈。 現在,給定一個alpha值,我希望這個圓是半透明的。
這是我的工作:
img_map = Image.new(some arguments here)
tile = Image.open('tile.png')
img_map.paste(tile, (x,y))
canvas = ImageDraw.Draw(img_map)
# Now I draw the circle:
canvas.ellipse((p_x - 5, p_y - 5, p_x + 5, p_y + 5), fill=(255, 128, 10))
# now save and close
del canvas
img_map.save(path_out + file_name, 'PNG')
如何使橢圓半透明?
謝謝
傳遞3元組RGBA值,而不是3元組RGB值(255、128、10):
canvas.ellipse((p_x - 5, p_y - 5, p_x + 5, p_y + 5),
fill=(255, 128, 10, 50))
例如,
import Image
import ImageDraw
img = Image.new('RGBA', size = (100, 100), color = (128, 128, 128, 255))
canvas = ImageDraw.Draw(img)
# Now I draw the circle:
p_x, p_y = 50, 50
canvas.ellipse((p_x - 5, p_y - 5, p_x + 5, p_y + 5), fill=(255, 128, 10, 50))
# now save and close
del canvas
img.save('/tmp/test.png', 'PNG')
我使用Image.composite(background, foreground, mask)
遮罩了前景上的半透明圓。
我按照這里的指示進行操作: 在PIL中將背景與透明圖像合並
感謝@ gareth-res
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.