Using parallel and Imagemagick to convert images to gif - imagemagick

I have used a python script which generates plots using a data file. Now because of huge amount of data, I am getting images in the range of 5000 and using ImageMagick's convert on a sequential processing is taking a lot of time.
I referred to this question, where GNU Parallel has been used along with ImageMagick. Is it possible to use a similar code for making a gif?
The command for converting images to gif that I am using is:
convert -delay 0.2 -loop 0 slice_VOF2* slice_VOF.gif
Thanks in advance
Vishwesh

The example you are referring to downscales the individual images in parallel. It it probably a good idea to prepare the images, so that they are the right size before converting them into an animgif.
parallel convert {} -resize 640x480 {.}.png ::: slice_VOF2*jpg
Then you can convert the images in groups into each their own animgif and finally combine these animgifs:
ls slice_VOF2*.png | parallel -X convert {} {#}-animpart.gif
ls *-animpart.gif | sort -n | parallel -Xj1 convert {} anim.gif

Related

Video Morph Between Two Images, FFMPEG/Minterpolate

I am trying to make a quick and easy morph video using two frames (png images) with ffmpeg's minterpolate filter, in a bash script on Ubuntu Linux. The intent is to use the morphs as transitions between similar video in a different video editor later.
It will work on 3+ frames/images, but fails using just 2 frames/images.
First the code that works: 3 frames
This is using three 1080p png files:
test01_01.png
test01_02.png
test01_03.png
input01="test01_%02d.png"
ffmpeg -y -fflags +genpts -r 30 -i $input01 -vf "setpts=100*PTS,minterpolate=fps=24:scd=none" -pix_fmt yuv420p "test01.mp4"
This takes a bit of processing time, then creates a 414kb, roughly three second mp4 video of a morph starting with the first frame, morphing to the second, then morphing to the third.
The code that fails: 2 frames
This is using just two of the same 1080p png files:
test02_01.png
test02_02.png
input01="test02_%02d.png"
ffmpeg -y -fflags +genpts -r 30 -i $input01 -vf "setpts=100*PTS,minterpolate=fps=24:scd=none" -pix_fmt yuv420p "test02.mp4"
This almost immediately creates a 262 byte corrupt mp4 file. There are no differences except the number of frames.
Things I've tried:
I have tried this with the Ubuntu default repo version of ffmpeg, and the static 64bit 5.0 and git-20220108-amd64 versions, all with the same result.
I have also tried with a 2-frame mp4 file as the input, with the same result.
Thoughts?
Is this a bug in ffmpeg or am I doing something wrong?
I am also open to any suggestions for creating a morph like this using other Linux-compatible software.
Thank you for any insight!
It is not documented, but it looks like minterpolate filter requires at least 3 input frames.
We may create a longer video using 5 input frames, and keep the relevant part.
For getting the same output as applying Minterpolate filter with only two input images, we may use the following solution:
Define two input streams:
Set test02_01.png as the first input and test02_02.png as the second input.
Loop each image at least twice, using -stream_loop
(test02_01.png is repeated twice and test02_02.png is repeated 3 times).
Set the input frame rate to 0.3 fps (it is equivalent to -r 30 and setpts=100*PTS).
The input arguments are as follows: -r 0.3 -stream_loop 1 -i test02_01.png -r 0.3 -stream_loop 2 -i test02_02.png.
Concatenate the two input streams using concat filter.
Apply minterpolate filer to the concatenated output.
The output of the above stage is a video with few redundant seconds at the beginning, and few redundant seconds at the end.
Apply trim filter for keeping the relevant part.
Add setpts=PTS-STARTPTS at the end (as recommended when using trim filter).
Suggested command:
ffmpeg -y -r 0.3 -stream_loop 1 -i test02_01.png -r 0.3 -stream_loop 2 -i test02_02.png -filter_complex "[0][1]concat=n=2:v=1:a=0[v];[v]minterpolate=fps=24:scd=none,trim=3:7,setpts=PTS-STARTPTS" -pix_fmt yuv420p test02.mp4
Sample output (as animate GIF):
test02_01.png:
test02_02.png:

Pipe Image Stream into Identify Command

I'm trying to find the unique amount of colors in an image and I've come across ImageMagick's identify tool that is used at the command line.
identify -unique-colors sunset.png
This has worked well in my tests, giving me much more accurate results compared to other tools.
My problem is the images I'd like to incorporate this into a script to examine images that aren't written to the hard drive. Is it possible to pipe a stream of data into the command? Something like:
echo xxx | identify -unique-colors
I think I have understood your use case. Effectively, you want to do this with PHP:
cat IMAGE.PNG | magick - -unique-colors txt:
or
cat IMAGE.JPG | magick - -unique-colors txt:
The - tells ImageMagick to read from its stdin.

How to use mogrify (ImageMagick) to convert from NEF to *.jpg and retain EXIF data?

I'm converting about 9000 photos from .NEF to .jpg.
I'd like to retain all EXIF data, most importantly Date and time created, Latidude and Longitude.
I'd like the JPGs to be at the highest possible quality.
I've just gotten started using ImageMagick from the command line. I also have Exiftool installed. I'm using the mogrify command because it handles batches nicely.
When I run
mogrify -format jpg *.NEF
All of my .NEF files are successfully converted to JPGs, but all EXIF data are lost.
I've searched around quite a bit to try and find a solution to this and it seems like I may have to install ufraw, but if possible I'd like a solution that uses software I already have - ImageMagick and Exiftool.
Thanks in advance for any advice you have about how to do this.
Update:
The images I converted using mogrify are slightly smaller (~ 1-2 MB) than those output by my colleague using picasa to convert NEF to JPG. But when I specify -quality 100 in ImageMagick the image sizes gain about 45 MB! Why?
The code exiftool -tagsfromfile %d%f.NEF -ext jpg -overwrite_original . adds the exif information to the JPGs.
Think twice before doing this - you really are discarding a lot of information - and if you don't want it, why not shoot JPEG instead of RAW in the first place?
FWIW, you can use ImageMagick to get the JPEG:
convert somefile.NEF somefile.jpg
Then you can copy the tags across from the original to the file newly created by ImageMagick:
exiftool -tagsfromfile somefile.NEF -all:all somefile.jpg
If you have thousands of images, and are on macOS or a decent Linux/Unix-based OS, I would recommend GNU Parallel like this and it will keep busy all those lovely cores that you paid Intel so dearly for:
parallel --dry-run 'convert {} {.}.jpg; exiftool -tagsfromfile {} -all:all {.}.jpg' ::: *nef
Sample Output
convert a.nef a.jpg; exiftool -tagsfromfile a.nef -all:all a.jpg
convert b.nef b.jpg; exiftool -tagsfromfile b.nef -all:all b.jpg
and if that looks good remove the --dry-run so it actually runs the command.
If you are on Windows, you will have to do some ad-hoc jiggery-pokery to get it done in any reasonable time frame. You can use the mogrify command and get all the conversions done to JPEG and then do all the exiftool re-embedding of the EXIF data later. If your files are named with some sort of system with incrementing numbers, you can start two or three copies of mogrify in parallel - say one doing files whose names end in [0-4] and another one doing files whose names end in [5-9]. I don't speak Windows, but that would probably look like these two commands each running in its own Command Prompt:
mogrify -format jpg *0.NEF *1.NEF *2.NEF *3.NEF *4.NEF
mogrify -format jpg *5.NEF *6.NEF *7.NEF *8.NEF *9.NEF
Then you would do the exiftool stuff when they had all finished but you would have to use a FOR loop like this:
FOR %%G IN (*.NEF) DO (
exiftool -tagsfromfile %%G -all:all %%~dpnG.jpg
)
The %%~dpnG part is a guess based on this answer.

Batch converting .eps to .jpg in imagemagick and making .mkv

I am trying to convert multiple .eps files into .jpg ones. By looking at answers here in SO, I was able to do it for single separate files.
The problem is that, when I'm trying to do it for all the files, they don't show any .jpg file.
I am currently using Imagemagick with the command
convert -density 300 outputs-000.eps -flatten outputs-000.jpg
I believe the problem is because my files are written as
outputs-000.eps
outputs-001.eps
outputs-002.eps
outputs-003.eps
...
outputs-145.eps
...
and so on. I tried putting %d (as in outputs-%d.eps and outputs-%d.jpg), but with no success.
Apart from that, I intent to get all those files and "convert" them into an .mkv or .gif or similar type (they are images of the time configuration of a particle collision system, so each image is a frame, so the goal is to make it into a 10sec movie). If there is a way to do that directly from the .eps, even better. Any help is welcome, since I've been trying to do this for several hours now. Thank you.
You should be able to make an animated GIF in one go like this:
convert -density 300 outputs-*eps -delay 200 animated.gif
Failing that, you should be able to convert all your eps files to, say PNG with:
mogrify -density 300 -format png outputs-*eps
Be careful with mogrify - it overwrites your input files unless you specify -path for an output directory, or you change format - like I just did to PNG.
For anyone who lands here trying to figure out how to work around ImageMagic's convert: not authorized without reverting the change that was made to the system-wide security policy to close a vulnerability, here's how you'd use Ghostscript to do a batch EPS-to-JPEG conversion directly without bringing ImageMagick into the mix:
gs -dSAFER -dEPSCrop -r300 -sDEVICE=jpeg -o outputs-%03d.jpg outputs-*.eps
-dSAFER puts Ghostscript in a sandboxed mode where Postscript code can only interact with the files you specified on the command line. (Yes, the parts of EPS, PS, and PDF files that define the page contents are in a turing-complete programming language.)
-dEPSCrop asks for the rendered output to be cropped to the bounding box of the drawing rather than padded out to whatever size page Ghostscript expects you to be printing to. (See the manual for details.)
The -r300 requests 300 DPI (-r600 for 600 DPI, etc.)
The -sDEVICE specifies the output format (See the Devices section of the manual for other choices.)
-o is a shorthand for -dBATCH -dNOPAUSE -sOutputFile=
This section of the Ghostscript manual gives some example formats for for multi-file filename output but, for the actual syntax definition, it points you at the documentation for the C printf(3) function.
Once you've got your JPEGs, you can follow the instructions in this answer over on the Video Production Stack Exchange to combine them into an MKV file.
The TL;DR is this command here:
ffmpeg -framerate 30 -i outputs-%03d.jpg -codec copy output.mkv
Check out the other answers if you want something that performs inter-frame compression rather than aiming to avoid transcoding the JPEGs again.
(If you want the best compromise, have Ghostscript output PNGs and then let ffmpeg handle switching to lossy compression.)

What is the fastest way to convert PostScript to GIF?

I am using the ImageMagick convert utility right now. I have a PostScript file that takes about 90 seconds to convert to GIF.
I am looking for a faster way to do this perferably by modifying the options to "convert".
When I say "fast", ideally a few seconds but I'll take any significant speed up. Something suitable for an interactive GUI.
I only need this in black and white or greyscale (specifically it is is an image of seismic data "wiggle traces" so B&W is fine.)
Other acceptable formats are BMP, GIF, JPEG, JPG, PCX, PGM, PNG, PNM, PPM, RAS, TGA, TIF, or TIFF.
Trying to stick with ImageMagick as that is already installed and trying to avoid selling my boss on anything new. Still happy to hear other suggestions.
My suggestion is: Use Ghostscript.
Since you have a working ImageMagick already installed, that means Ghostscript is also there: because ImageMagick cannot convert PDF or PostScript to raster images all by its own -- it has to call Ghostscript as its delegate to do this anyway.
Ghostscript can directly convert PDF/PostScript input to TIFF/TIF/TIFFg4, JPEG, PBM, PCX, PNG, PNM, PPM, BMP raster image output.
The advantages are: you don't need to have ImageMagick involved. So it's faster and also gives you more direct control over the conversion parameters. If you run Ghostscript via ImageMagick that's a level of indirection which isn't always required. (Sometimes it may be required to add some fine-tuning and post-processing manipulations to the raster image data that Ghostscript generated -- but that doesn't seem to be the case for you.)
The only disadvantage is: Ghostscript cannot produce GIF. If you required GIF (which you don't seem to), you need ImageMagick for post-processing the raster output of Ghostscript to GIF.
You can see how ImageMagick calls Ghostscript (and which parameters it uses for the call -- look for a printed line on stderr containing gs, gsx or gswin32c or gswin64c) by running for example:
convert -verbose some.pdf[0] some.gif
Update
I did run a very, very un-scientific 'benchmark', running the following two commands 100 time each, which convert the randomly picked page 333 of the official PDF specification (ISO version for PDF-1.7) to GIF, measuring the time consumed. I run these commands in concurrently parallel, so both should have had to deal with the same overall system load, making the results better comparable:
'Comfortably' using ImageMagick's convert to directly produce GIF:
time for i in $(seq -w 1 100); do
convert \
PDF32000_2008.pdf[333] \
p333-im-no_${i}.gif ;
done
Using Ghostscript to create from the same page grayscale PNGs, piping Ghostscript's output to ImageMagick's convert in order to get GIFs:
time for i in $(seq -w 1 100); do
gs \
-q \
-o - \
-dFirstPage=333 \
-dLastPage=333 \
-sDEVICE=pnggray \
PDF32000_2008.pdf \
| \
convert \
- \
p333-gs-no_${i}.gif ;
done
Timing esults for the first command (running the 'comfortable' convert to achieve the PDF->GIF transformation, which uses Ghostscript only 'behind our backs'):
real 2m29.282s
user 2m22.526s
sys 0m5.647s
Timing results for the second command (running gs directly + openly, piping it's output to convert:
real 1m27.370s
user 1m23.447s
sys 0m3.435s
One more thing:
The total size of the 100 'Ghostscript'-GIFs was 1,6 MByte -- but they were 8-bit grayscale.
The total size of the 100 'ImageMagic-direct'-GIFs was 1,2 MByte -- but they were 2-bit black+white.
I don't have the motivation currently to tweak the test commandline parameters more for even closer comparability of the resulting files.
This result (149 seconds vs. 87 seconds) gives me enough confidence into my guess that you can gain significant performance improvements when you follow my recommendation. :-)
I am using the ImageMagick convert utility right now. I have a
PostScript file that takes about 90 seconds to convert to GIF.
I am looking for a faster way to do this perferably by modifying the
options to "convert".
When I say "fast", ideally a few seconds but I'll take any significant
speed up. Something suitable for an interactive GUI.
I only need this in black and white or greyscale (specifically it is
is an image of seismic data "wiggle traces" so B&W is fine.)
You can start with GhostScript:
gs -dSAFER -dBATCH -dNOPAUSE \
-sDEVICE=pnggray -r300 -sOutputFile=seismic.png seismic.pdf
A very longer but interesting way would be to analyze exactly what is in those PDFs.
I had to do something similar with the PDF output of an EKG workflow. The original data were unavailable, we only had the PDF, but I discovered that the PDF was vector based and not raster. After a little hacking it was very easy to decode the labels, the legend and the single elementary lines making up the EKG diagram, and I came up with an option to recolor the tracks starting from what appeared a grayscale image. It did take several days, though.
It is possible that your PDF is generated in a similar way, and the data could be decoded (at first I had to use pdftk to get me a non-compressed PDF, then I found a library that I could use - it implemented the Deflate algorithm). It would be really cool to have output in SVG format :-)

Resources