You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all thank you for this nice package!
I was trying to export an animation as a gif with a transparent background. Unfortunately, this was not possible using imageio (at least based on my googeling). There is a solution using PIL (see below).
However, this requires implementing a custom save function. Would you be interested in adding alternative support for PIL to save gifs to allow this solution natively? I would be willing to implement this properly. If not, I hope this issue serves as potential information for others who ran into the same issue I did.
importdrawSvgasdrawfromPILimportImagefromdrawSvgimportDrawing, render_svg_framesdefconvert_to_transparent_frame(im):
"""Modified from https://stackoverflow.com/questions/46850318/transparent-background-in-gif-using-python-imageio"""alpha=im.getchannel("A")
# Convert the image into P mode but only use 255 colors in the palette out of 256im=im.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=255)
# Mark all pixels with alpha=0 as transparentmask=Image.eval(alpha, lambdaa: 255ifa==0else0)
# Paste the color of index 255 and use alpha as a maskim.paste(255, mask)
# The transparency index is 255im.info["transparency"] =255returnimdefsave_video(frames, file, **kwargs):
""" Save a series of drawings as a GIF or video. Arguments: frames: A list of `Drawing`s or a list of `numpy.array`s. file: File name or file like object to write the video to. The extension determines the output format. align_bottom: If frames are different sizes, align the bottoms of each frame in the video. align_right: If frames are different sizes, align the right edge of each frame in the video. bg: If frames are different sizes, fill the background with this color. (default is white: (255, 255, 255, 255)) duration: If writing a GIF, sets the duration of each frame. fps: If writing a video, sets the frame rate in FPS. **kwargs: Other arguments to imageio.mimsave(). """ifisinstance(frames[0], Drawing):
frames=render_svg_frames(frames, **kwargs)
kwargs.pop("align_bottom", None)
kwargs.pop("align_right", None)
kwargs.pop("bg", None)
frames= [
convert_to_transparent_frame(Image.fromarray(frame, "RGBA")) forframeinframes
]
frames[0].save(
file,
save_all=True,
append_images=frames[1:],
disposal=2, # This "cleans" the canvas after every drawoptimize=False, # This is critical for transparent background**kwargs,
)
# Draw a frame of the animationdefdraw_frame(t):
d=draw.Drawing(2, 6.05, origin=(-1, -1.05))
d.setRenderSize(h=300)
# d.append(draw.Rectangle(-2, -2, 4, 8, fill="white")) # Commented out, as we want to have transparent backgroundd.append(draw.Rectangle(-1, -1.05, 2, 0.05, fill="brown"))
t= (t+1) %2-1y=4-t**2*4d.append(draw.Circle(0, y, 1, fill="lime"))
returndwithdraw.animate_video(None, draw_frame) asanim:
# Add each frame to the animationforiinrange(20):
anim.draw_frame(i/10)
foriinrange(20):
anim.draw_frame(i/10)
foriinrange(20):
anim.draw_frame(i/10)
save_video(anim.frames, "test.gif", duration=0.05)
The text was updated successfully, but these errors were encountered:
This looks great. I think it would be a nice addition to drawSvg.
My suggestion would be to make code like this work:
withdraw.animate_video('example6.gif', draw_frame, duration=0.05,
gif_transparency=True, gif_alpha_threshold=128
) asanim:
# Add each frame to the animation
...
Or maybe a separate function like with draw.animate_gif('test.gif', duration=0.05, alpha_threshold=0) as anim:.
Let me know if you have questions implementing this or making a pull request.
Sry for the silence. Unfortunately, live came in the way of the project I was using this for. At the moment, I don't know when I will have time to look into this again.
First of all thank you for this nice package!
I was trying to export an animation as a gif with a transparent background. Unfortunately, this was not possible using imageio (at least based on my googeling). There is a solution using PIL (see below).
However, this requires implementing a custom save function. Would you be interested in adding alternative support for PIL to save gifs to allow this solution natively? I would be willing to implement this properly. If not, I hope this issue serves as potential information for others who ran into the same issue I did.
The text was updated successfully, but these errors were encountered: