generate.py: improve text overlay

There were some strange artifacts in the text overlay that seem to
go away if I generate the text with a foreground and background di-
rectly instead of in two separate steps.
This commit is contained in:
Alan Orth 2024-03-01 17:07:45 +03:00
parent f3224bee2e
commit ea995417d3
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9

View File

@ -43,22 +43,16 @@ def create_thumbnail(animal):
600, height=600, linear=True, crop=animal["vips_smartcrop"] 600, height=600, linear=True, crop=animal["vips_smartcrop"]
) )
# Create a temporary image with text using Pango markup, which can use # Create a temporary image with text using Pango markup, which can use
# some HTML. This allows us to use consistent font sizes as opposed to # some HTML. This allows us to use consistent font sizes (as opposed to
# pyvips.Image.text's width and height which are maximums in pixels. # pyvips.Image.text's width and height parameters, which are maximums
# We also use RGBA to make the background transparent, which we need so # in pixels). Note that we need to use RGBA here or else the text image
# we can see the rectangle we will draw next. # shows up as a white block when we overlay it on the thumbnail.
# #
# See: https://docs.gtk.org/Pango/pango_markup.html # See: https://docs.gtk.org/Pango/pango_markup.html
text = pyvips.Image.text( text = pyvips.Image.text(
f'<span foreground="white" size="48pt">{animal["common_name"]}</span>', f'<span foreground="white" background="#702D3E" size="48pt">{animal["common_name"]}</span>',
rgba=True, rgba=True,
) )
# Draw a rectangle on top of our image with the same dimensions as our
# text and position it relative to the bottom using the height. The RGB
# values are ILRI red (#702D3E).
vips_thumbnail = vips_thumbnail.draw_rect(
[112, 45, 62], 0, 600 - text.height, text.width, text.height, fill=True
)
vips_thumbnail = vips_thumbnail.composite( vips_thumbnail = vips_thumbnail.composite(
text, "over", x=0, y=600 - text.height text, "over", x=0, y=600 - text.height
) )