-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add text to GIF? #3128
Comments
I was surprised that I couldn't find a simpler way, but here is what I came up with - from PIL import Image, ImageDraw, ImageSequence
import io
im = Image.open('Tests/images/iss634.gif')
# A list of the frames to be outputted
frames = []
# Loop over each frame in the animated image
for frame in ImageSequence.Iterator(im):
# Draw the text on the frame
d = ImageDraw.Draw(frame)
d.text((10,100), "Hello World")
del d
# However, 'frame' is still the animated image with many frames
# It has simply been seeked to a later frame
# For our list of frames, we only want the current frame
# Saving the image without 'save_all' will turn it into a single frame image, and we can then re-open it
# To be efficient, we will save it to a stream, rather than to file
b = io.BytesIO()
frame.save(b, format="GIF")
frame = Image.open(b)
# Then append the single frame image to a list of frames
frames.append(frame)
# Save the frames as a new image
frames[0].save('out.gif', save_all=True, append_images=frames[1:]) See the following parts of the docs if you would like more information -
|
Thank you! 😃 Works like a charm. But if I try to save the final result into a BytesIO, I always get an empty file: my_bytes = BytesIO()
frames[0].save(my_bytes, format='gif', save_all=True, append_images=frames[1:]) And I need to place a white text on the image. If I use the
|
I would conclude that you need to However, you probably will want to convert the image to RGB - which I've found incidentally also changes the image away from being multiple frames. So the temporary save operation is no longer required. Here is code for that, as well as saving the final result into BytesIO. Let me know if it does not work for you. from PIL import Image, ImageDraw, ImageSequence
import io
im = Image.open('Tests/images/iss634.gif')
frames = []
for frame in ImageSequence.Iterator(im):
frame = frame.convert('RGB')
d = ImageDraw.Draw(frame)
d.text((10,100), "Hello World", fill=(255,255,255))
del d
frames.append(frame)
my_bytes = io.BytesIO()
frames[0].save(my_bytes, format="GIF", save_all=True, append_images=frames[1:])
print(my_bytes.getvalue()) |
This one gave me an error unfortunately:
|
Actually changing the mode to RGBA solved my problem. 😃 Works like a charm now. Thanks for all your amazing help! And for the BytesIO thing, it was a noob thing,,, I forgot to do a |
For the record, I've since realised a simpler way of converting a multiframe image into a single frame image - |
How Can I add a custom text on an animated gif file?
The text was updated successfully, but these errors were encountered: