Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PIL在python上向图像添加透明圆_Python_Image Processing_Png_Python Imaging Library - Fatal编程技术网

使用PIL在python上向图像添加透明圆

使用PIL在python上向图像添加透明圆,python,image-processing,png,python-imaging-library,Python,Image Processing,Png,Python Imaging Library,我有一个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

我有一个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')
如何使椭圆半透明


谢谢,传递4元组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(背景、前景、遮罩)
遮罩前景上的半透明圆圈

我按照这里的指示:


多亏@gareth res

嗨,我试过这个(在创建img和RGBA中的4个项目时使用了
RGBA
,结果不是很好。圆圈的颜色不再完全强烈,但它不会与下面的地图混合…看这里:你可能想试试Kris Kowal的解决方案,或者alpha复合函数(来自我在同一页的帖子)。