How do I crop an animated gif using ImageMagick? - imagemagick

There's plenty of information about cropping images, but attempting to crop (or trim) animations produces strange results. Sometimes they flicker, or come with extra frames, or some frames crop correctly and others become offset. How do I prevent all this from happening?

convert input.gif -coalesce -repage 0x0 -crop WxH+X+Y +repage output.gif
Animated gifs are often optimised to save space, but imagemagick doesn't seem to consider this when applying the crop command and treats each frame individually. -coalesce rebuilds the full frames.
Other commands will take into consideration the offset information supplied in the original gif, so you need to force that to be reset with -repage 0x0.
The crop itself is straightforward, with width, height, x offset and y offset supplied respectively. For example, a crop 40 wide and 30 high at an x offset of 50 = 40x30+50+0.
Crop does not remove the canvas that it snipped from the image. Applying +repage after the crop will do this.

Even with the coalesce and repage, I could not get ImageMagick to crop and resize animated gifs very well.
I found a program called Gifsicle and it works great for manipulating animated gifs.
gifsicle --crop 0,0-100,100 --output out.gif in.gif
It can also do all sorts of other operations. Check it out!

Animations are often optimized, which means that some frames are smaller than others. So in ImageMagick you probably want to coalesce the animation before cropping.
convert in_animation.gif -coalesce -crop WxH+X+Y +repage -layers optimize out_animation.gif
You may need to add a -dispose method before reading the input animation to avoid a flicker. Also set the -delay and -loop at the end, if you want to make changes.
See
http://www.imagemagick.org/Usage/anim_basics/#dispose
http://www.imagemagick.org/Usage/anim_basics/#coalesce
http://www.imagemagick.org/script/command-line-options.php#layers

The following line worked with me on Mac
convert -dispose 2 input.gif -trim -layers TrimBounds animation.gif
Here is the source

Related

Blur part of an image only

I would like to blur only a part of an image. The part to blur is always rectangular, so that I can easily use the following command:
magick source.jpg -region 1000x1000+0+500 -blur 0x20 result.jpg
This works, but is pretty slow for large images. Since I have to process thousands of files again and again, this will simply take too long.
Therefore, I decided to do the blurring by downscaling and upscaling the image back to the original size. However, since this will blur the full image, I have tried to accomplish the task using the following steps:
take the original image as background
create a copy of the original image
blur the copy using down-/upscaling
crop the desired region from the blurred copy
compose the original and the blurred&cropped copy
I am already pretty close (I hope), but when composing the two images, the cropped image will always be positioned in the top-left corner of the original image - instead of the original position from the source image. This is my current command:
magick source.jpg ( -clone 0 -resize 5% -resize 2000% -crop 1000x1000+0+1000 ) -composite result.jpg
I have read in the documentation that the original canvas size will be retained when using the -crop operation, and that this size and position will be used when using -composite. However, this doesn't seem to work in my case. Does anyone have an idea why?
I have tried to use -repage, -extent and other options to define the size and position of the cropped image, but to no avail so far.
I would try -flatten in your command as that is used for layers.
You can do it with a mask image (of any shape) in ImageMagick. Though I am not sure if that will be faster than your scaling method.
Input:
Mask:
(note: blurring occurs where mask is black)
magick lena.jpg -write-mask mask.png -blur 0x3 +write-mask lena_blurred.png
Result:

Resizing animated GIF in image creates artifacts

I'm trying to add a blank area around some animated GIFs using imagemagick, but in many cases the resulting larger animations have artifacts. For example, if I start with this image
And run the following command (which has the dispose and coalesce flags which I've thrown in after seeing them online in the hopes they'll help)
convert input.gif -gravity center -dispose previous -extent 500x500 -coalesce output.gif
The result is the following
Is there a different command I can run that wouldn't gave the green splotches invade the original image?
You have your syntax wrong for ImageMagick and also need to use -dispose none. You are adding a new color to your animation which likely already used 256 colors.
Input:
convert -dispose none input.gif -coalesce -gravity center -background green -extent 500x500 -layers optimize output.gif
Change the background color as desired, which you left out of your command.

Resize an image with 54x54 squares (540x540) to 54x54pixel lossless

I've got an 540x540 image of 54x54 color squares (same sizes).
When I resize it to 54x54px it looks horrible (blurred), shouldn't a resize like this be perfectly done with imagemagick?
is I possible to get it perfect?
I've tested convert source.png -resize destination.png and -adaptive-resize but the result is the same..
I see what your confusion is now... the problem is not that the process is lossy, rather it is because the -resize is doing more sophisticated processing than you want in order to make an attractive job that you would want for, say, photographs. You want a very simple point sampling process which will produce simple blocks of pure, uncombined colour.
I'll make a start image:
magick -size 10x10 xc:red +noise random -scale 540x540 start.png
And scale it down, by taking a point sample in each block:
magick start.png -sample 10x10 small.png
And back up:
magick result.png -scale 540x540 reincarnated.png

How can I reduce the dimensions of gif images with ImageMagick?

What flag in magick.exe convert <flags> will reduce the dimensions of my image?
This is the command I'm using: magick.exe input.png[0] -depth 8 -type Grayscale -dresize 400x300.
The [0] after the source-image filename is meant to strip the first frame of any animated gifs.
How can I do reduce my output gif sizes? My file sizes are too large: my outputs need to be less than 100k. Any methods other than reducing the dimensions are also welcome.
convert test.gif -fuzz 10% -layers Optimize result.gif
Adding a -fuzz 2% produced a better optimization, but still not very good. At -fuzz 15% It isolated the differences for frame optimization to just the visible color band changes I noted before.
At 25% the differences were almost to just the text changes.
Finally at a massive 30% fuzz factor (ignore color changes below that figure, did it optimize to just the text changes.
You can try gifsicle:
gifsicle -O3 old.gif -o new.gif
If it's an animation, you could try skipping frames (see how it works).
magick.exe convert -resize 100x100 .\step1.jpg .\step2.jpg -delay 100 -loop 0 animation.gif
Thanks to Nate.
//IMPORTANT: -resize should come first.
//BY DEFAULT: the aspect ratio will be locked with the longest dimension being set to 100px.
//GENERALIZATION: this order `magick convert 1st<input_file(s)> 2nd<switch(es)> 3rd<output_file>`.
//IMPORTANT-RELATED: `-delay` will not work on second brush! Use `-set delay` for existing files.
Please, also be sure to refresh your folder view to ensure that you're not viewing old output files: Windows Explorer (F5).
Try to use the option:
-type Palette
It might help to reduce your gif file sizes smaller, oh and I believe -depth 8 can only be used for png images.

How to pixelate/blur an image using ImageMagick?

I want to pixelate and/or blur an image.
I've found the command for the blurring:
$convert image.jpg -blur 18,5 newimage.jpg
to work but I cannot blur the image any more.
And how do I pixelate the image? I couldn't find a sound example around the net.
Thx
To get a proper square pixellation, try:
convert -scale 10% -scale 1000% original.jpg pixelated.jpg
This worked nicely for me, gives a sort of cross between pixelating and blurring:
convert -resize 10% image.jpg newimage.jpg
convert -resize 1000% newimage.jpg newimage.jpg
You can be sure that the data cannot be retrieved, should that be important to you.
Changing the %ages will change the amount of pixelation/blur
I don't know anything about ImageMagick, but you can try resizing the image using bicubic to a much smaller dimension, then resizing the image back to a bigger one.
The trick works using .net's System.Drawing object.

Resources