Reputation: 427
I am trying to extract GIF frames using ImageMagik convert
command. The GIF images are optimized to only overlay animated changes (I believe they call it mipmap) and this is ideal for my extraction due to performance limitations. If I use -coalesce
I lose the optimization since I'm using the GIF frames in a spritesheet so I need to extract it as is but when I do extract it with the standard convert xxx.gif xxx.png
the animation frames are extracted without offsets and the canvas sizes are different to the base image (0).
I need all the images extracted to match the first frame with retained offsets from the GIF meta-data. I have no idea how to accomplish this in ImageMagik and want to know if this is possible?
Then if I get them matching sizes with offsets applied I can add them to the sprite sheet easily.
Would appreciate all the help I can get! Thanks.
Pro challenge! Do you accept?! :)
Upvotes: 2
Views: 697
Reputation: 207465
If you extract to GIF, rather than PNG, it seems fine:
convert animated.gif frame%d.gif
identify frame*
frame0.gif GIF 307x465 307x465+0+0 8-bit sRGB 256c 61.8KB 0.000u 0:00.000
frame1.gif[1] GIF 278x372 307x465+16+61 8-bit sRGB 256c 19.9KB 0.000u 0:00.000
frame10.gif[2] GIF 278x371 307x465+17+62 8-bit sRGB 256c 17.9KB 0.000u 0:00.000
frame11.gif[3] GIF 278x370 307x465+17+63 8-bit sRGB 256c 17.4KB 0.000u 0:00.000
frame12.gif[4] GIF 275x371 307x465+20+62 8-bit sRGB 256c 17.2KB 0.000u 0:00.000
frame13.gif[5] GIF 276x370 307x465+19+63 8-bit sRGB 256c 17.9KB 0.000u 0:00.000
frame14.gif[6] GIF 275x369 307x465+19+64 8-bit sRGB 256c 16.8KB 0.000u 0:00.000
If you want to overlay these onto a blank canvas, you can do it something like this. I haven't thought about it too much and there may be an easier way, but for now...
#!/bin/bash
# Extract individual frames
convert animated.gif frame%d.gif
# Make a blank canvas same size as original
convert -size 307x465 xc:none canvas.gif
i=0
for f in frame*; do
geom=$(identify -format "%X%Y" $f)
convert canvas.gif $f -geometry $geom -composite +repage g_$i.gif
((i++))
done
Or maybe you mean you want all the frames in their correct relative positions but one after the other in a long sequence in a single file?
#!/bin/bash
# Extract individual frames
convert animated.gif frame%d.gif
# Make a blank canvas same size as original
convert -size 307x465 xc:none canvas.gif
# Paste individual frames onto blank canvas and append frames into long sequence
for f in frame*; do
geom=$(identify -format "%X%Y" $f)
convert canvas.gif $f -geometry $geom -composite +repage miff:-
done | convert - +append sequence.gif
Upvotes: 3