-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
What did you do?
- I'm using Pillow to create many individual images which I then stitch together to create a lyrics video for a song.
- The app is online at https://www.rhymecraft.guru
- One of the features of the videos created by my app is that, while showing an entire line of lyrics on-screen at once, the currently-pronounced syllable can be highlighted.
- You can see an example here: https://www.youtube.com/watch?v=9u9ylseLAfU
- The way I achieve this is by:
- first calculating where the X, Y coordinates of the line would start if the entire line of lyrics was drawn at once
- then drawing the text preceding the highlighted syllable and recording the width of that text,
- then drawing the highlighted syllable, using the original X, Y coordinates and the width of the preceding text to calculate where the highlighted syllable's X, Y coordinates should start,
- then proceeding in the same way to draw the text following the highlighted syllable.
What did you expect to happen?
I expected that the current syllable would change color without any other visible changes.
What actually happened?
The text spacing always doesn't seem to work totally right, so the lyrics sometimes get spaced out.
You can see it:
- caused by the word 'of' at 0:47: https://www.youtube.com/watch?v=9u9ylseLAfU&t=47s
- in the word 'paradise' at 1:03: https://www.youtube.com/watch?v=9u9ylseLAfU&t=1m3s
What are your OS, Python and Pillow versions?
- OS: Windows 10 (local machine), Ubuntu (Digital Ocean)
- Python: 3.7
- Pillow: 5.0.0
Example code:
from PIL import ImageDraw, Image, ImageFont
img = Image.new('RGBA', (200, 60), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('arial.ttf', size=48)
first_font_color = (255, 0, 0)
second_font_color = (255, 255, 255)
draw.text((0, 0), 'paradise', first_font_color, font=font)
draw.text((0, 0), 'par', second_font_color, font=font)
par_width, par_height = draw.textsize('par', font=font)
draw.text((0 + par_width, 0), 'adise', second_font_color, font=font)
img.save('example.png')There's first a red 'paradise' created, then a white 'paradise' created in the same spot. If you zoom in you'll notice that the red one shows up underneath the white one starting with the 'a' and then that continues for the 'dise':
