简体   繁体   English

线条不会使用ImageDraw在图像上绘制

[英]Line won't draw on an image using ImageDraw

I am trying to use PIL to open an image, then draw a line on it, but it is not working. 我正在尝试使用PIL打开图像,然后在其上画一条线,但是它不起作用。 I am trying to make a leveling display, with the line being how far the user has leveled up. 我正在尝试显示一个水平显示,该行是用户水平升高的程度。

I've tried creating a new image using Image.new , and having a base already available and opening it using Image.open , and I've also tried putting the coordinates directly into the function, and just putting the two-tuples as variables and calling those variables into the function (as seen below). 我尝试使用Image.new创建一个新图像,并已经有一个可用的底座,然后使用Image.open打开它,并且还尝试将坐标直接放入函数中,并且仅将两个元组作为变量然后将这些变量调用到函数中(如下所示)。 The base image is a transparent image. 基本图像是透明图像。

    length = 300 # Used as a placeholder integer, as this would change depending on user's level
    base = Image.open('./level_up/base.png')
    draw = ImageDraw.Draw(base)
    pos1 = (280, 324)
    pos2 = (280, length + 324)
    draw.line((pos1, pos2), fill=(255, 102, 168), width=20)
    base.save('./level_up/base2.png')

I open up the image, which is a blank, transparent image that is 1500 x 296 , and nothing is there. 我打开图像,它是1500 x 296的空白透明图像,没有任何内容。 No line or anything. 没有行或任何东西。

As you are working with an image containing transparency (mode "RGBA" in PIL), you have to explicitly pass an opacity as the 4th component of colors. 在处理包含透明度的图像(PIL中的模式“ RGBA”)时,必须显式地将不透明度作为颜色的第4个分量传递。 Just add "255", meaning 100% opacity to the fill color in your call for line, and it should work: 只需添加“ 255”即可,这意味着在您拨打电话时,填充色的透明度为100%,它应该可以正常工作:

draw.line((pos1, pos2), fill=(255, 102, 168, 255), width=20)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM