PIL Overlay a translucent square overlay on the image

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

It gives me a huge black square. I want it to be a translucent black square with a cat. Any ideas?

+10
source share
2 answers

Sorry, the comment I made about the error was incorrect, so ...

You can do this by creating a temporary image and using Image.alpha_composite()as shown in the code below. Please note that it supports translucent squares other than black.

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

Here is the result of applying it to your test image:

picture of a cat with blacken square rectangle superimposed on it

+16
source

, :

img = Image.eval(img, lambda x: x/2)
+1

Source: https://habr.com/ru/post/1675646/


All Articles