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
Python 3.x Python PIL:如何绘制自定义填充多边形_Python 3.x_Image Processing_Python Imaging Library - Fatal编程技术网

Python 3.x Python PIL:如何绘制自定义填充多边形

Python 3.x Python PIL:如何绘制自定义填充多边形,python-3.x,image-processing,python-imaging-library,Python 3.x,Image Processing,Python Imaging Library,我想知道是否有一种方法可以绘制自定义填充多边形,我知道我可以绘制矩形和圆形,那么自定义多边形呢 具体来说,我想画一些类似下图的东西: 我怎样才能做到这一点,有什么想法吗。谢谢我的平面设计能力和耐心并不为人所知,但这应该给你一个想法: #!/usr/bin/env python3 from PIL import Image, ImageDraw # Form basic purple image without alpha w, h = 700, 250 im = Image.new('RG

我想知道是否有一种方法可以绘制自定义填充多边形,我知道我可以绘制矩形和圆形,那么自定义多边形呢

具体来说,我想画一些类似下图的东西:


我怎样才能做到这一点,有什么想法吗。谢谢

我的平面设计能力和耐心并不为人所知,但这应该给你一个想法:

#!/usr/bin/env python3

from PIL import Image, ImageDraw

# Form basic purple image without alpha
w, h = 700, 250
im = Image.new('RGB', (w,h), color=(66,0,102))

# Form alpha channel independently
alpha = Image.new('L', (w,h), color=0)

# Get drawing context
draw   = ImageDraw.Draw(alpha)
radius = 50
hline  = h-radius
OPAQUE, TRANSPARENT = 255, 0
# Draw white where we want opaque
draw.rectangle((0,0,w,hline), fill=OPAQUE)
draw.ellipse((w//2-radius,hline-radius,w//2+radius,h), fill=OPAQUE) 

# Draw black where we want transparent
draw.ellipse(((w//3)-radius,-radius,(w//3)+radius,radius), fill=TRANSPARENT)
draw.ellipse(((2*w//3)-radius,-radius,(2*w//3)+radius,radius), fill=TRANSPARENT)

# DEBUG only - not necessary
alpha.save('alpha.png')

# Put our shiny new alpha channel into our purple background and save
im.putalpha(alpha)
im.save('result.png')

alpha通道看起来像这样-我人为添加了一个红色边框,以便您可以看到它的范围:


我并不以我的平面设计能力或耐心修补美学而闻名,但这应该给你一个想法:

#!/usr/bin/env python3

from PIL import Image, ImageDraw

# Form basic purple image without alpha
w, h = 700, 250
im = Image.new('RGB', (w,h), color=(66,0,102))

# Form alpha channel independently
alpha = Image.new('L', (w,h), color=0)

# Get drawing context
draw   = ImageDraw.Draw(alpha)
radius = 50
hline  = h-radius
OPAQUE, TRANSPARENT = 255, 0
# Draw white where we want opaque
draw.rectangle((0,0,w,hline), fill=OPAQUE)
draw.ellipse((w//2-radius,hline-radius,w//2+radius,h), fill=OPAQUE) 

# Draw black where we want transparent
draw.ellipse(((w//3)-radius,-radius,(w//3)+radius,radius), fill=TRANSPARENT)
draw.ellipse(((2*w//3)-radius,-radius,(2*w//3)+radius,radius), fill=TRANSPARENT)

# DEBUG only - not necessary
alpha.save('alpha.png')

# Put our shiny new alpha channel into our purple background and save
im.putalpha(alpha)
im.save('result.png')

alpha通道看起来像这样-我人为添加了一个红色边框,以便您可以看到它的范围:


你的意思是类似函数的东西?(顺便说一句,我通过点击你的链接发现了这一点。)我想你必须零碎地做这件事,因为,
ImageDraw.polygon()
只画直线。你可以将
cairosvg
与PIL/Pillow一起使用,所以如果你熟悉SVG,并且能适应SVG,这可能是你的一个选择。@Mark Setchell,正如你所说,多边形只做直线。。。我从来没有听说过这个cairosvg软件包我会去看看的谢谢,我的第一个猜测是画一个矩形,然后用一个圆/饼片遮罩圆的一些区域,但还没有真正找到一种方法来实际实现它,这就是我上面所说的“零碎”的意思。。。识别矩形和圆形,并一次一块地构建它们。你是指类似函数的东西吗?(顺便说一句,我通过点击你的链接发现了这一点。)我想你必须零碎地做这件事,因为,
ImageDraw.polygon()
只画直线。你可以将
cairosvg
与PIL/Pillow一起使用,所以如果你熟悉SVG,并且能适应SVG,这可能是你的一个选择。@Mark Setchell,正如你所说,多边形只做直线。。。我从来没有听说过这个cairosvg软件包我会去看看的谢谢,我的第一个猜测是画一个矩形,然后用一个圆/饼片遮罩圆的一些区域,但还没有真正找到一种方法来实际实现它,这就是我上面所说的“零碎”的意思。。。识别矩形和圆形,然后一次一块地构建它们。很高兴它对你有用。祝你的项目好运!很高兴它对你有用。祝你的项目好运!