How can I programmatically create APNG files? - imagemagick

Generally I use PHP, and ImageMagick, but as far as I can tell it seems the ImageMagick group refuse to implement the ability to create/understand animated PNG files?
What tooling can I implement to take a dozen JPEG files and create an animated PNG out of them? I'd prefer to have a PHP api, but I can branch out into another language if required!
Thanks

As of ImageMagick 7.0.10-31, Animated PNG is supported.
magick -delay 100 -loop 0 frame1.png frame2.png APNG:myanimation.png
parameters:
-delay 100 specifies a duration of 100 centiseconds (aka one second) per frame before moving on to the next frame.
-loop 0 tells the animation to repeat forever.
APNG:___.png specifies the output should be animated png.
(thanks to #fmw42 for improvements)

Related

ImageMagick convert from pdf to image shrinks image and places it in bottom left corner

I am using this command to convert pages from a pdf to jpeg images:
magick convert -density 300 sample.pdf output.jpeg
I see a white background and the content of the PDF appears as a smaller image stuck to the bottom left corner of the white "canvas". Can anyone help with why this might be happening and how to prevent this "shrinking"?
My PDF has 14 pages. Here is the metadata for a few of those pages:
>magick identify sample.pdf
sample.pdf[0] PDF 2286x3600 2286x3600+0+0 16-bit sRGB 6458B 0.016u 0:00.017
sample.pdf[1] PDF 2286x3600 2286x3600+0+0 16-bit sRGB 6018B 0.016u 0:00.020
sample.pdf[2] PDF 2286x3600 2286x3600+0+0 16-bit sRGB 5732B 0.016u 0:00.023
And here are the actual and expected outputs for one of the pages:
actual output:
expected output:
edit: here is a sample PDF:
https://www.dropbox.com/s/0bzu5brfzbedd7i/sample.pdf?dl=0
I am not sure why you have that behavior. There is something in the PDF, perhaps a crop box, that Imagemagick/Ghostscript is not picking up. But you can get rid of the excess white using -trim
magick sample.pdf -trim sample_%d.jpg
Thanks for the example
> magick identify sample.pdf
> sample.pdf[0] PDF 2286x3600
Apears to be wrong as there is no match
from the PDF contents
/Width 1531
/Im0
/Height 2454
/MediaBox [0 0 1531 2454]
on
Page Size:
/CropBox [0 0 919 1473]
919 pt x 1473 pt
32.42 x 51.96 cm
12.76 x 20.45 inches
Therefore no problems when the images were inserted as # 120 dpi
We can check the image by copy when zoom to 100% in a viewer and paste into say paint, which agrees the image is 1531 x 2454 pixels.
As a result of comments with #fmw42, it was decided to see if GhostScript (which ImageMagick depends on for PDF handling) was having an affect, and certainly processing that PDF using GS v 9.55 without any special switches gave warnings and produced the output below left So the issue seems to be caused by recent GhostScript method of calling/scaling. since using simple GhostScript based image apps (Irfanview using GS plugin on the left) behave the same whilst other viewers have less of a problem even sister product MuPDF as previewed on the right. So the file Media Box as seen and probably used for scaling by Ghostscript seems to be the culprit, but was processed by two other PDF handlers during generation.
One solution would be to use a simpler method of extracting images as PNG thus look at Xpdf command line tools "pdftopng" which gives a good result but you need to calculate that the optimum resolution in this case is 120 (or 240), Typical windows command line does not need .exe but its best to use that when prefixing with a path for use from another location.
pdftopng.exe -r 120 -f 1 -l 1 sample.pdf

How can I avoid dropouts when using (imagemagick) `mogrify` to convert webp files to animated gif?

Using this code
mogrify -format gif *.webp
that I found in another forum
https://superuser.com/questions/1506337/batch-convert-webp-files-to-gif-files-offline/1506428 to convert a webp file, to an animated gif...
I was wondering if anyone else experienced patches of black and/or white "dropouts", when using this method? With many files, it works without incident, but with others, such as the attached, I have to use an online converter, to avoid this issue. Are there additional filters that I could apply, or other CLI methods for this type of conversion?
Some of your webp frames have transparency and others do not. The fix seems to be to set the dispose method to none. So this works for me on IM 6.9.10.90 Q16 Mac OSX Sierra using convert. I suggest you use the more flexible convert rather than mogrify. I am not sure you can do what you want with mogrify as it wants one output per one input. Here is my command:
convert -delay 10 -dispose none tumblr_e573d6d767dd3d65d21de47fa7d16d13_4d26286c_400.webp -loop 0 animation.gif
or better
convert -delay 10 -dispose none tumblr_e573d6d767dd3d65d21de47fa7d16d13_4d26286c_400.webp -coalesce -loop 0 -layers optimize animation.gif
Give that a try and let me know if it works for you. The file is too large to upload directly. If you need to see it and my command does not work for you, then I will post it elsewhere and put a link here.

ImageMagick convert is not creating an animated gif

I have a folder containing a series of PNG files named forGif001.png forGif002.png ... and I am trying to convert them to an animated gif. The command I am using is magick convert -delay 1000 *png -loop 0 newGif.gif where the delay value is supposedly in milliseconds (a delay of 1000 being 1 second therefore) but I have tried it with smaller values such as 1 just to be safe. However, the gif it creates is just a still image of forGif001 and it never cycles to any of the following pictures. I am not getting any errors or warnings. I installed image magick a few hours ago so I imagine the issue is not stemming from an outdated version.

Convert a bunch of png or webp images to a webp animation

How to convert a bunch of png or webp images into webp animation ?
I tried this:
convert mytiles.png -crop 100x100 +repage tmp.webp
But I just get a bunch of webp images instead of an animation.
Another solution would be to use gif2webp but the homebrew webp package doesn't contain this command unlike what's written in the official documentation.
Convert frames to WebP, then use webpmux
The webpmux program comes with Google’s reference library for WebP, namely libwebp, which you can install through your package manager if not present yet. Its man page reads:
webpmux — create animated WebP files from non-animated WebP images, extract frames from animated WebP images, and manage XMP/EXIF metadata and ICC profile.
If your individual frames are in another format, you’ll first need to convert them to WebP. For this task you can use ImageMagick (more options here—be sure to disable lossy compression unless you want it):
convert frame001.png -define webp:lossless=true frame001.webp
Then you can combine frames using webpmux. The syntax is the following, where the capitalized words are placeholders:
webpmux \
-frame frame001.webp +D1+X1+Y1+M1±b \
-frame frame002.webp +D2+X2+Y2+M2±b \
-frame frame003.webp +D3+X3+Y3+M3±b \
[-loop L] [-bgcolor A,R,G,B] \
-o animation.webp
Each frame expects a number of settings. All of them but the duration can be left implicit.
Di is the duration of the frame, in milliseconds.
(Xi, Yi) is the spatial offset of the frame in the canvas (counting from the top-left corner, X going right and Y going down).
Mi is the dispose method for this frame, in other words what to do once this frame has expired. It has two possible values:
0: do not dispose, leave the canvas as-is;
1: dispose, i.e. clear the canvas and fill it with background (consumes energy, for that matter).
±b is the blending method for this frame, which specifies how to superimpose this frame on top of the existing canvas (it is only relevant when the frame has an alpha channel, otherwise it overwrites what is below it anyway). This parameter has two possible values:
-b: do not blend, the frame overwrites the existing canvas;
+b: use alpha blending, the frame combines with the existing canvas.
Then come the optional settings that apply to the animation as a whole:
-loop L specifies the number L of times to play the animation. You’ll probably want it to be infinite, which is meant by -loop 0, which is the default value.
-bgcolor A,R,G,B specifies the background color. The values of the four components (alpha, red, green, blue) range from 0 to 255. Viewers may use this color as a background for the canvas, so that the canvas is opaque, but they are not required to; in practice the canvas is overlayed on top of other elements, as in a web page for example, and I have not seen this color used.
See also the WebP specification about animations.
Final tip: since the command line options for each frame are more than just the input file name, you cannot use a wildcard like frame*.webp directly and writing the command line proves cumbersome. Thankfully, you can use your shell to build the command line, e.g with Bash:
frames=( )
for f in frame*.webp ; do
frames+=( -frame "$f" +100+0+0+0+b )
done
webpmux "${frames[#]}" -o animation.webp
The example above creates an animation whose frames are all images matching frame*.webp, have a duration of 100 ms, have no spatial offset, and combine with the previous frames (use alpha blending for new frames and do not dispose of them afterwards).

With ImageMagick, how can a JPEG image be reduced in quality until it is below a certain file size?

A JPEG image can be reduced in quality and, thus, in file size using ImageMagick in a way such as the following:
convert -quality 85% image.jpg image_small.jpg
How can the quality of an image be reduced such that the resulting image is below a certain file size (e.g. 3 MB)? A scenario in which this would be useful would be preparing a large number of images for upload to a site like Imgur.
I thought there was a post about this last week but can not find it. There was another here: ImageMagick: scale JPEG image with a maximum file-size
Anyway you want to use: -define jpeg:extent=400KB https://www.imagemagick.org/script/command-line-options.php#define
Out of interest your code is wrong as the input comes after convert and your quality goes before the output image.
So you want something like this:
convert image.jpg -define jpeg:extent=3000KB image_small.jpg
If you have an old version of Imagemagick it may not work that is why it is always good to include your imagemagick version in your question.
The results are a bit hit and miss on exact filesize.

Resources