使用PIL在python上为图像添加透明圆圈 [英] Adding a transparent circle to an image on python with PIL

查看:2143
本文介绍了使用PIL在python上为图像添加透明圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python程序,它上面有一个带圆圈的png文件。现在我希望这个圆是半透明的,给定一个alpha值。

I have a python program that craetes a png file with a circle on it. Now I want this circle to be semi transparent, given an alpha value.

以下是我的工作:

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元组的RGB值, (255,128,10),传递4元组RGBA值:

Instead of a 3-tuple RGB value, (255, 128, 10), pass a 4-tuple RGBA value:

canvas.ellipse((p_x - 5, p_y - 5, p_x + 5, p_y + 5), 
               fill=(255, 128, 10, 50))






例如,


For example,

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')

这篇关于使用PIL在python上为图像添加透明圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆