PNG-Preview of animated GIF with ImageMagick is distorted - imagemagick

I am creating preview images of all JPG/PNG/GIF that are uploaded to our server.
For this I use ImageMagick with:
convert -format jpg -quality 90 -strip -background white -flatten -alpha off +repage -resize '255x255>' ".$locationtmp." -write '".$preview."'");
The parameters added are to flatten the background (make it white instead of black).
However, when using the code to create a preview of an animated GIF, the result is distorted.
Example of a distorted result:
I tried to add the +repage parameter but it did not help.
How to get a nice preview of the GIF as PNG? It could also be just the first frame.

In Imagemagick, if you want a preview image for all formats including gif and want just the first frame of the animation for the preview, then just add [0] to the filename, such as animation.gif[0]. This will take the first frame of the animation and the only frames of the other formats such as JPG and PNG that only allow one frame. The appended [0] does not hurt the other formats.
So use
image.suffix[0] in place of just image.suffix
for generating a single image preview

Related

Imagemagick reset scene for each frame in an animated gif

I am trying to generate a gif in the command line.
My source image has tiles like this:
When I create a gif with the command convert source.png -crop 80x80 +repage test.gif I get the following result:
In this image each image is drawn on top of the ones before it. So how can we reset the scene on each image draw so each tile in the source image becomes a frame itself?
I think you need to set the "disposition":
convert -background none cartoon.png -crop 80x80 +repage -delay 80 -set dispose background result.gif

ImageMagick convert format, crop and resize

I have a large number of research pdf figures, and I need to preform the following actions in ImageMagick:
convert all pdf to png
crop png from left/top corner x:334/y:244; from right/bottom corner x:214/y:340;
original size 2100x2100, cropped size 1552x1552 pixels
resize cropped png to 240x240 pixels
Here is how it should be cropped for point 2, the pink area is what I want to have:
I was only be able to get 1st action done with my knowledge:
mogrify -format png -density 300 -flatten *.pdf
How can I do the 2nd and 3rd actions please? And do I need to run three separated commands or could they be combined into one command?
I do not know what exact order you need for mogrify as I do not use it. I also do not know why you need flatten Try:
mogrify -format png -density 300 -crop 1552x1552+344+244 +repage -resize 240x240 *.pdf

Convert PNG with transparency to JPG

Edit: The original title of this question was "Drawbacks of using -flatten option". However, after this question was answered, I decided to change its title, to make it easier found on Google. Also, new title is actually better describes what is told below.
As I discovered today, to convert PNG with transparency into JPG, we need to use -flatten option.
Try it yourself: download Google logo and convert it from PNG to JPG with the following line
convert google.png google.jpg
the picture will be messed:
With -flatten option, it works fine:
convert google.png -flatten google.jpg
I'm wondering are there any drawbacks of using -flatten permanently, for all conversions between PNG, JPG/JPEG and GIF.
The problem with converting PNG to JPG is when the PNG has transparency. JPG does not allow transparency and any transparent areas will show what color is underneath the transparency, which is often black. So you should use -flatten to properly do that conversion. But you should specify -background somecolor before -flatten, if you do not want the default background color. GIF only allows binary transparency -- fully transparent or fully opaque. PNG allows 8-bit transparency (partial transparent). I know of no significant issues using -background xx -flatten when converting PNG or GIF to JPG. However, the background color you use will change the appearance in transparent areas from that of the underneath color. Here is what is happening:
Input:
Turn alpha off:
convert google.png -alpha off google_aoff.jpg
The stripes are from the underneath color below the alpha channel.
Alpha Channel (nicely antialiased):
convert google.png -alpha extract google_alpha.jpg
Simple Flatten (default background is white):
convert google.png -flatten google_flatten.jpg
Flatten with black background:
convert google.png -background black -flatten google_flatten_black.jpg
Often one will reprocess the original transparent PNG image so that it has some constant color underneath the alpha channel so that later one can remove the alpha channel and not have odd colors showing. It will look the very same as the original PNG.
convert google.png -background white -alpha background google_bg_white.png
However, if you simply remove the alpha channel the JPG will show aliasing since only the fully transparent pixels' background colors were changed to white. You have a nice clean background, but the image is still aliased (as it was in the original when the alpha channel was remove).
convert google_bg_white.png google_bg_white.jpg
So one still needs to flatten the result, so that the antialiasing of the alpha channel will smoothly blend the colors near the boundaries.
convert google_bg_white.png -flatten google_bg_white_flatten.jpg
An alternate method to -flatten is to use -alpha remove, which is discussed http://www.imagemagick.org/Usage/masking/#alpha_remove. So starting with the original PNG, we do
convert google.png -background white -alpha remove google_alpharemoveoff.jpg
The result is the same as -background white -flatten. We do not need the -alpha off mentioned in the reference, since JPG does not support any alpha channel. The reference says this is more efficient and is the preferred method.
#John C wrote:
1st approach:
convert google.png -flatten google_flatten.jpg
2nd approach:
convert google.png -background white -alpha background google_bg_white.png
convert google_bg_white.png -flatten google_bg_white_flatten.jpg
3rd approach:
convert google.png -background white -alpha remove google_alpharemoveoff.jpg
More properly, these should be
1st approach
convert google.png -background white -flatten google_flatten.jpg
2nd approach
convert google.png -background white -alpha background -flatten google_bg_white_flatten.jpg
3rd approach
convert google.png -background white -alpha remove -alpha off google_alpharemoveoff.jpg
In case 1: -background white is the default. But if you want some other background color you need to specify it.
In case 2: there is no need to save to an intermediate file
In case 3: you will need -alpha off if you save to PNG. JPG does not support transparency, so turning alpha off is not needed.

Cropping images with ImageMagic produces wrong size images - why?

There is a bunch of images that represent the pages, scanned from a book and already cropped with ImageMagic. They are all uniform, 629x625 pixels. I need to process them further to prepare for creation of a PDF: split them into odd and even pages. They are named canvas-00.png through canvas-53.png
When I use the following command to get the odd pages first, I get completely butchered images 275x563 pixels extracted from a wrong horizontal offset:
convert canvas-??.png -crop 314x625+0+0 ~/Pictures/odd/canvas.png
What am I doing wrong?
PS: Originally the images were larger, contained extra margins with scanner's lid visible, and I successfully cropped them to 629x625 using a very similar command, but of course with non-zero offset of +140+71. So it baffles me that a visibly more trivial command with zero offset does not work at all.
I suspect you have page offsets "remembered" in your PNG files and you need to clear them.
As an example, let's create an image 500x300 pixels:
convert xc:red xc:lime +append \
\( xc:blue xc:magenta +append \) -append -resize 500x300\! start.png
Now let's crop out a piece towards the bottom right corner, which I intentionally made magenta so you can see which piece I got:
convert start.png -crop 200x100+250+150 z.png
But if we now look at it with identify you can see it "remembers" where it was in the original picture:
identify z.png
Output
z.png PNG 200x100 500x300+250+150 16-bit sRGB 51.1KB 0.000u 0:00.000
This will then potentially affect (i.e. mess up) anything you do in future with the image. The solution is to use +repage to reset all the page geometry and layout so it forgets. You can either do this when cropping, or when using the cropped image later - depending whether you want to retain the crop information or not. (Sometimes you do want it when you are doing jigsaw puzzle processing and maybe want to reassemble the pieces later).
convert start.png -crop 200x100+250+150 +repage z.png
identify z.png
z.png PNG 200x100 200x100+0+0 16-bit sRGB 51.1KB 0.000u 0:00.000
In your case, if you already cropped and saved without repaging, you can load your images and repage prior to actual use:
convert canvas-??.png +repage -crop 817x1040+0+0 ~/Pictures/odd/canvas.png

Imagemagick: animated gif background flatten

I'm wondering how to remove and replace transparency in an animated GIF with ImageMagick.
My goal is to take several pages with different aspect ratios and make an animated GIF that cycles through them.
document1.png
I am using
convert -delay 50 -resize 212 -dispose Previous -page 212x275 -crop 212x275+0+0 +repage document1.png document2.png documents.gif 2>&1
to create the GIF.
This sizes my documents correctly, but leaves the empty space from the short images transparent, but I would like that to be white. How can I change the transparency to white?
doc.gif
I have tried
convert doc.gif -background black -alpha off doc2.gif
convert doc.gif -background black -flatten doc2.gif
-alpha doesn't seem to do anything, and -flatten removes the animation

Resources