Page 1 of 2
For each image in animated GIF, do this, save to JPG images
Posted: 2012-02-18T17:48:48-07:00
by nokee
Hi! This is giving me quite some trouble.
What I need to do is extract all images from an animated GIF and save them as separate JPG files.
However before each image is saved I first need to modify it a bit.
I want to do this in one single convert command, splitting it up into several calls to convert will not be efficiant on the server where it will be used.
Here's the closest I have come to what I want to accomplish, but it is alas totally wrong:
Code: Select all
convert -respect-parentheses clean.gif ( +clone ( -clone 0 -gravity NorthWest -crop 61x12+3+263 -blur 0x3 -gravity SouthEast -background transparent -extent 64x275 ) -composite ) -quality 0 +adjoin img/IMG%%06d.jpg
What that code does is output 49 JPG images (the GIF has 48), all of them exactly as they are in the GIF except for the last JPG -- it's a dupe of the final image of the GIF except it has a part of the image blurred out in the lower left corner.
What I'm trying to accomplish is to output 48 JPG images where all of them has the same part of the image blurred out.
I want to be able to tell convert this: "For each image in the GIF, blur the corner and then save the edited image as a JPG file."
If there's a wizard out there I'd be very grateful if you can have a look at my problem.

Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-02-18T18:04:13-07:00
by fmw42
Try this as a test (as I don't have your image)
Create 3 frame gif image:
convert logo: logo: logo: logo3.gif
Blur only a given region and output each frame as jpg
convert logo3.gif -coalesce -gravity northwest -region 150x130+60+40 -blur 0x5 logo3_%02d.jpg
It works for me to blur only the area around the word "Image" in the IM internal logo: image
see
http://www.imagemagick.org/script/comma ... php#region
http://www.imagemagick.org/script/comma ... p#coalesce
http://www.imagemagick.org/Usage/anim_basics/#coalesce
P.S. You can also create an arbitrary shaped mask and use that to control where the blur occurs.
See
http://www.imagemagick.org/script/comma ... #clip-mask
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-02-19T08:02:12-07:00
by nokee
He was the wizard of a thousand kings
And I chance to meet him one night wandering
Thank you very much man, it worked perfect! My code was a joke compared to yours.

Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-02-19T11:57:11-07:00
by fmw42
No problem. I learned from the master (Anthony) and have several years to practice.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-03T06:53:33-07:00
by nokee
Hello again, I must ask: If I also want to perform crop on the gif afterwards, how would I do it?
Like if I have a 100x100 animated GIF image and I want to blur the top left 10x10 pixels and then do a crop so that only the top left 20x20 pixels remains, and then output all of the frames of the animation to separate files.
I've tried around and the best I can come up with so far is this:
Code: Select all
convert clean.gif -gravity northwest -region 61x12+3+263 -blur 0x5 -region 471x277+0+0 -crop 61x62+3+213 -quality 90 img/00i%%05d.png
That blurs a section of the image and then crops out the image, only problem is that the crop is overlayed on top of the full image (that has a blurred area). That is, each outputted frame is a big image with a section of itself repeated in the top corner.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-03T15:58:38-07:00
by fmw42
Why do you have two -regions?
Can you put your example in terms of the numbers you talk about or put numbers in the example as you desire. Your description and code do not seem to match. So I am having trouble understanding what you want here.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T04:19:59-07:00
by nokee
Ah sorry about that.
In short I want to first blur a section of the frame, then crop the frame, then save the frame. Repeat for each frame in the GIF image. And I want to do it with only one convert call.
Here's the code changed to fit the example.
Code: Select all
convert clean.gif -gravity northwest -region 10x10+0+0 -blur 0x5 -region 100x100+0+0 -crop 20x20+0+0 -quality 90 img/00i%%05d.png
In this example, say that clean.gif is 100x100.
Region 10x10+0+0 is the top left corner. Making only 10x10 pixels "active". I'm blurring that.
Region 100x100+0+0 is the entire image.
I thought that I had to make the entire image "active" again for the cropping of "20x20+0+0" to work (thought that otherwise it would only look in the 10x10 region).
Something makes sense because it does in fact crop the image like I want, however it overlays the cropped image into the full image. It saves as a cut-out-image inside an image. I only want it to save the cut-out-image.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T04:52:16-07:00
by anthony
To turn off regions either run a list operation (like the implicit write of the final filename), or use
+region.
See IM Examples, Masks, Regions
http://www.imagemagick.org/Usage/masking/#regions
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T06:12:11-07:00
by nokee
It works! Could have sworn I tried that but I must have goofed it up.
Thanks a million! ImageMagick truly is the king.
Edit: Worked only on my testing machine where I have "ImageMagick-6.7.1-Q16" installed.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T06:29:21-07:00
by nokee
I'm getting error '8000ffff' from using +region. Was it added recetly to ImageMagick...? The version installed where this shall be used is "ImageMagick-6.5.8-Q16".
Can what I'm trying to accomplish be used without +region? Perhaps using clipping masks?
ImageMagickObject is being used on the live machine (where I'm getting the error). During testing I'm simply using convert from the command prompt.
Edit: I found a dead anchor link in your posts's link anthony. In the sentance "In summary... Clipping Masks will perform operations [...]" the word "Clipping Masks" leads to #clipping_masks which doesn't exist. Perhaps it should be #clip_mask?
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T07:39:07-07:00
by nokee
Also tried to run these as admin from the command prompt on the live machine.
Works (images are written to the img folder):
Code: Select all
convert animation.gif -coalesce -gravity northwest -region 61x12+3+263 -blur 0x5 -quality 90 img/image%05d.png
Code: Select all
convert animation.gif -coalesce -gravity northwest -crop 61x62+3+213 -quality 90 img/image%05d.png
Doesn't work (no images are written, there's no output, and there's no error either):
Code: Select all
convert animation.gif -coalesce -gravity northwest -region 61x12+3+263 -blur 0x5 +region -crop 61x62+3+213 -quality 90 img/image%05d.png
The program doesn't terminate immediately, it appears to be doing some kind of work but nothing is written (no files or text).
Looks like my two choises are:
1) Do blur and crop separately (more pressure put on the HDD and it takes more CPU).
2) Upgrade to newer version of ImageMagick (must put machine offline for a while, adds a risk of stuff breaking).
Do you have any recommendations?
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T12:14:08-07:00
by fmw42
when cropping and writing to png, you have to remove the virtual canvas that is left of the full size image by adding +repage after your crop. Jpg does not save virtual canvas info, so if saving to jpg, you can leave +repage off. However it does not hurt to leave it in.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T15:24:38-07:00
by nokee
I'm afraid that didn't solve the problem.
Tried this:
Code: Select all
convert animation.gif -coalesce -gravity northwest -region 61x12+3+263 -blur 0x5 +region -crop 61x62+3+213 +repage -quality 90 img/image%05d.png
The same thing happened, two seconds passes (while ImageMagick is working) and then it terminates without writing any file and without giving any output message or error.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T17:46:18-07:00
by anthony
nokee wrote:Edit: I found a dead anchor link in your posts's link anthony. In the sentance "In summary... Clipping Masks will perform operations [...]" the word "Clipping Masks" leads to #clipping_masks which doesn't exist. Perhaps it should be #clip_mask?
Yes At teh time 'region' examples were written (actually moved from another location in IM Examples) the clipping masks area (actually write masks) was only raw notes with no formatting. That has now improved.
NOTE: my feeling is that write_masks, should be able to be defined in a number of different ways. 'write masks' (what pixels are writable) and 'protect masks' (a negated write mask that defines what pixels should not be writable). also I feel the mask should come from in memory, and not from an external source. Both of which I plan to fix with IMv7.
Re: For each image in animated GIF, do this, save to JPG ima
Posted: 2012-03-04T17:54:14-07:00
by anthony
nokee wrote:Code: Select all
convert animation.gif -coalesce -gravity northwest -region 61x12+3+263 -blur 0x5 +region -crop 61x62+3+213 +repage -quality 90 img/image%05d.png
The same thing happened, two seconds passes (while ImageMagick is working) and then it terminates without writing any file and without giving any output message or error.
I can't seem to re-produce.
Code: Select all
convert rose: -gravity northwest -region 20x20+3+3 -blur 0x5 +region -crop 20x20+13+13 +repage t.png
Can you provide a link to you "animation.gif" ?
Also remove options until you get the smallest command that re-produces the problem.