Python PIL在图像上绘制半透明的方形覆盖图
这给了我一个巨大的黑色正方形。我希望它是一个半透明的黑色正方形,上面有一只猫。有什么想法吗?对不起,我关于它是一个bug的评论是不正确的,所以 您可以通过创建一个临时映像并使用,如下面的代码所示。请注意,它支持除黑色以外的半透明正方形Python PIL在图像上绘制半透明的方形覆盖图,python,python-3.x,python-imaging-library,pillow,Python,Python 3.x,Python Imaging Library,Pillow,这给了我一个巨大的黑色正方形。我希望它是一个半透明的黑色正方形,上面有一只猫。有什么想法吗?对不起,我关于它是一个bug的评论是不正确的,所以 您可以通过创建一个临时映像并使用,如下面的代码所示。请注意,它支持除黑色以外的半透明正方形 from PIL import Image from PIL import ImageDraw from io import BytesIO from urllib.request import urlopen url = "https://i.ytimg.c
from PIL import Image
from PIL import ImageDraw
from io import BytesIO
from urllib.request import urlopen
url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
file = BytesIO(urlopen(url).read())
img = Image.open(file)
img = img.convert("RGBA")
draw = ImageDraw.Draw(img, "RGBA")
draw.rectangle(((0, 00), (img.size[0], img.size[1])), fill=(0,0,0,127))
img.save('dark-cat.jpg')
以下是将其应用于测试图像的结果:
如果您只想使整个图像变暗,有一种更简单的方法:
from PIL import Image, ImageDraw
from io import BytesIO
from urllib.request import urlopen
TINT_COLOR = (0, 0, 0) # Black
TRANSPARENCY = .25 # Degree of transparency, 0-100%
OPACITY = int(255 * TRANSPARENCY)
url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
with BytesIO(urlopen(url).read()) as file:
img = Image.open(file)
img = img.convert("RGBA")
# Determine extent of the largest possible square centered on the image.
# and the image's shorter dimension.
if img.size[0] > img.size[1]:
shorter = img.size[1]
llx, lly = (img.size[0]-img.size[1]) // 2 , 0
else:
shorter = img.size[0]
llx, lly = 0, (img.size[1]-img.size[0]) // 2
# Calculate upper point + 1 because second point needs to be just outside the
# drawn rectangle when drawing rectangles.
urx, ury = llx+shorter+1, lly+shorter+1
# Make a blank image the same size as the image for the rectangle, initialized
# to a fully transparent (0% opaque) version of the tint color, then draw a
# semi-transparent version of the square on it.
overlay = Image.new('RGBA', img.size, TINT_COLOR+(0,))
draw = ImageDraw.Draw(overlay) # Create a context for drawing things on it.
draw.rectangle(((llx, lly), (urx, ury)), fill=TINT_COLOR+(OPACITY,))
# Alpha composite these two images together to obtain the desired result.
img = Image.alpha_composite(img, overlay)
img = img.convert("RGB") # Remove alpha for saving in jpg format.
img.save('dark-cat.jpg')
img.show()
考虑到每当我想用PIL绘制一个透明的矩形时,我总是回到这个问题上来,所以我决定给出一个更新 如果我只更改一件事,那么您的代码对我来说非常有用:将图像保存为PNG格式而不是JPEG格式 所以当我跑步的时候
img = Image.eval(img, lambda x: x/2)
我得到了这样一幅美妙的画面:
谢谢你的帮助。我刚刚创建了一个新问题:@ChaseRoberts这不是一个bug,这是对
Draw
应该做什么的误解。它不进行混合,而是用一组新的像素替换一组像素。显示这是一个两步过程,在空白画布上绘制不透明度,然后合成结果。正如回答的第二部分所示。@Markransem:的确,我的解决方法是这样做的(一般来说)。@MinhNghĩa:无可否认,文档在这个问题上非常简洁。也许维基百科上的文章会帮助你了解它的功能。@MinhNghĩa那么这是一件好事!ImageDraw的作者可能认为这是不言而喻的,因此没有必要对此进行解释。fill=int,抱怨tuple我正在运行Pizzle 8.2.0,在运行上述代码时,我没有看到任何抱怨。
from io import BytesIO
from urllib.request import urlopen
from PIL import Image
from PIL import ImageDraw
url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
file = BytesIO(urlopen(url).read())
img = Image.open(file)
draw = ImageDraw.Draw(img, "RGBA")
draw.rectangle(((280, 10), (1010, 706)), fill=(200, 100, 0, 127))
draw.rectangle(((280, 10), (1010, 706)), outline=(0, 0, 0, 127), width=3)
img.save('orange-cat.png')