Automating multiple imagemagick commands - batch-file

I have hundreds of PDFs containing powerpoint handouts (with 6 (2x3) slides on each page) that I want to convert to single slide per page PDF (or PPT, ultimate goal is importing to OneNote properly).
So far I have succeeded in finding the correct commands to create single, nicely cropped JPEG files for each slide:
C:\"Program Files"\ImageMagick-6.8.9-Q16\convert.exe -density 300 C:\Users\matt\Desktop\cmd\5.pdf -gravity Center -crop 80%x+0+0 -quality 100 -sharpen 0x1.0 C:\Users\matt\Desktop\cmd\output.jpg
This split up the PDF pages and crops away page numbers for easier trimming later.
C:\"Program Files"\ImageMagick-6.8.9-Q16\convert.exe -crop 2x3# C:\Users\matt\Desktop\cmd\output-0.jpg C:\Users\matt\Desktop\cmd\output-0%d.jpg
This divides slides up from one page.
C:\"Program Files"\ImageMagick-6.8.9-Q16\convert.exe -trim C:\Users\matt\Desktop\cmd\output-00.jpg C:\Users\matt\Desktop\cmd\output-00%d.jpg
This trims the borders of each slide.
How would I automate all this into a drag and drop script so I can quickly convert a PDF into a collection of JPEGs or from there a full PDF?

After tons of reading I figured it out. I wrote a .bat (batch?) first setting the working directory as the location the .bat was located in. Then I did all the imagemagick operations with proper use of leading zeros to get the right order (if you use *.jpg it will go through 1.jpg, 11.jpg, THEN 2.jpg). The operations basically took any .pdf in the folder and turned it in JPGs, manipulated them as I needed, then when finally in proper order, made a single PDF.
Simple in theory, tricky in practice.

Related

MLT melt slideshow on Windows: unable to control output video length and problems with using wildcards

I am using the following command within a batch script to, hopefully, eventually programmatically create simple video slideshows with transitions:
melt 131.jpg 132.jpg 133.jpg ttl=75 -attach crop center=1 -filter luma cycle=75 duration=25 -transition mix -consumer avformat:out.mp4 frame_rate_num=30 frame_rate_den=1
Most of this command is an adaptation for Windows of this command on the MLT website blog (with the exception of the part that scales and transforms the image). For some reason when I run this, however, the output video file is 25 minutes long!
I have two main questions:
a. How do I properly control the duration of each image in the video? I have experimented quite a bit with changing the parameters and I have a semi-decent understanding of what they all mean (I am a newbie to MLT but I figured that there's no way to do something like this easily in FFMPEG alone). The only way I have found to decrease the duration with any amount of control is to increase the output framerate to absurd numbers (which, of course, is not ideal as it's a massive waste of time and energy and still doesn't really solve the issue).
b. How do I use a wildcard to input all the .jpg files in a folder on Windows? I tried adding *.jpg but that didn't work and I don't know how else to do it within a batch script (I tried using the following code to get the file names as a variable, but I wasn't able to get string concatenation working correctly because it only outputs the final file name)
set files=
for /r %%i in (*.jpg) do (
echo %%i
set files=%files% "%%i"
)
echo %files%
Thank you for any suggestions!
When you specify a .jpg file, melt automatically chooses a producer internally. Depending on your environment and version, that producer will either be the qimage or pixbuf producer.
Both producers offer a "ttl" parameter to specify the duration of the image (in frames) for image sequences
https://mltframework.org/plugins/ProducerQimage/#ttl
https://mltframework.org/plugins/ProducerPixbuf/#ttl
In the example you linked, an image sequence is created by using the special syntax: photos/.all.jpg ttl=75
In your example, you specify a specific file name. So an image sequence is not created. Instead, a new producer is created for each file. The default length for a producer is 15000 frames.
https://github.com/mltframework/mlt/blob/master/src/framework/mlt_producer.c#L102
The length can be specified in the command line.
melt 131.jpg length=100 132.jpg length=100 133.jpg length=100
Change
set files=%files% "%%i"
to
CALL set "files=%%files%% "%%i""
This uses a subsidiary process to concatenate your filenames.
I have no idea about the solution to your other question.

How to perform Batch processing to Merge channels

I have two separate folders with equal number of images from microscopy (134 images in each folder). These are two different dyes for a specific cells. What i want to do is merging each image from one folder with its corresponding image from another folder i.e. first image of the folder a with first image of folder b and so on.
i have been trying to do this work, but since each image has a different name, i can not finish this job successfully by Batch processing. Can anyone help?
Thnkas
I need to merge channels from the two images on the left to get the image on the right by using ImageJ --> Image --> Color --> Merge channels
Using C1 (red) and C3 (blue) in the settings.
[1]: https://i.stack.imgur.com/OUKkg.jpg
As you didn't raise any objection to my suggestion of using ImageMagick, I will show you how to do it using that.
As you didn't provide any images, let's assume you have two directories called A and B with PNG files in A that you want to use as the red channel and PNG files in B that you want to use as the blue channel. I'll assume you want a zero/blank green channel.
./A/image1.png
./A/image2.png
./B/image1.png
./B/image2.png
Now, go into A and the basic command for one file is:
cd A
magick image1.png ( +clone -fx 0 ) ../B/image1.png -combine result.png
That says... "load image1.png, make a copy of it and fill the copy with zeroes, load ../B/image1.png and combine them assuming the first is red, the second is green and the third is blue, and save them as result.png".
Hopefully you can get that working. If it does what you want, we can work on a batch version. I don't use Windows, so I would write this on Linux:
#!/bin/bash
for f in *png ; do
echo "Combining $f (as Red), zero (as Green) and ../B/$f (as Blue) to make res-$f"
magick $f \( +clone -fx 0 \) ../B/$f -combine res-$f
done
I know a dangerously small amount of Windows BATCH script, so I'll do my best to guess how it will look. Save it as GO.BAT:
FOR %%G IN (*.png) DO (
ECHO %%G
magick %%G ( +clone -fx 0 ) ../B/%%G -combine res-%%G
)
If I load your "image" into Photoshop and cut out the salient parts and trim the 112 pixels off the second image to make it the same size as the first and then reverse the order and combine them using the commands suggested, I get:

Batch file script to convert PDFs and image files to TIFF

I need to convert multiple PDFs, and image files (JPG, GIF, etc.) within a certain folder to TIFF and then move the TIFF to a different folder and put the original in an "ARCHIVE" folder (within the first folder). This will run every 5-10 minutes using a scheduled task within Windows.
I have been using a program called Ghostscript which works great, the command line I am using for converting a PDF for example is:
gswin64.exe -o test03.tiff -r720x720 -g6120x7920 -sDEVICE=tiffg4 test.pdf
Can anybody help me out with a script to do the above?
To convert image to tiff without external binaries you can use convImg.bat.It still not completed (I need to add help message and some checks) but it is working.Accepts two arguments source image and the target one and the format is taken by the extension:
call convImg.bat "C:\putin_gay_clown.jpg" "C:\putin_gay_clown.tiff"
To complete your script you can use:
#echo off
::: !!! CHANGE THE LOCATIONS BELLOW !!!
set "pics=c:\pics"
set "tiff_folder=c:\tiffs"
for %%a in ("%pics%\*jpg" "%pics%\*gif" "%pics%\*png" "%pics%\*bpm") do (
call convImg.bat "%%~fa" "%tiff_folder%\%%~na.tiff"
)
As for the converting a pdf to tiff I don't think this is possible without external tool.

PDF to PNG ghostscript Batch file issue

I am trying to drag a pdf onto a batch file which will then convert the pdf to a png, in the same directory. It all works well for a single page pdf, I get the right conversion to png format, the problem is when I am converting a pdf with multiple pages. The output says "Processing pages 1, 2, 3 etc... But what I end up with is only the first page of the pdf. Could anyone steer me in the right direction, I would be much appreciated.
I have created a batch file with the code below. Thank's in advance.
path=%PATH%;C:\Program Files (x86)\gs\gs9.20\bin\
cd %~dp1
gswin32c.exe -sDEVICE=pngalpha -sCompression=lzw -r300x300 -dBATCH -sOutputFile=%1.png %1
You have to use %d as part of the output filename to specify the pattern for the page number in the pngs. -sOutputFile=%1_%%d.png in that case.

What can i use to automate video snapshots

What command line util can i use to create a thumbnail preview of a video i made? I do not want a single thumb that windows make, i want a large jpg file that has 30 or less shots so i can preview the movie w/o running it.
I either need an app that can do a batch at once or a command line util so i can loop through a folder or w/e i need and feed it to the util. I was thinking of hacking up ffplay to do this, i dont know what i am getting myself into so is that recommended? (i used SDL many times, never its YUV settings nor with ffmpeg)
You can use ffmpeg to perform frame extraction, here's how I've done something similar
ffmpeg -i my_input_video.flv -y -an -sameq -f image2 -r 1 my_frame_%05d.jpg
-i my_input_video.flv specifies the input file
-y overwrite output files
-an disables audio (we're transcoding a video to a series of jpegs, we don't need audio)
-sameq - use same quality as source
-f image2 sets the output format to image2 (an image sequence)
The -r parameter is the frames per second, so the above command will produce one jpeg per second.
Once you have your collection of jpegs, you can use ImageMagick montage command to build a montage image.
You could use MTN. It can scan whole directories and create thumbnails for every video found.
Use mplayer:
mplayer -vo jpeg -frames 1 your_file
Will extract the first frame.
Media Player Classic can make a collection (it's called save as thumbnail; with matrix i.e. 4x4), but only from a single file.
Potplayer can automatically generate the thumbnails for a batch of videos in folders or even in subfolders. It just takes a few clicks.
Add all the videos you want to get thumbnails from to the playlist (You can do this by go to menu "Open-Open Folder...")
Select all the videos in playlist and right click on them.
Select "Create Thumbnail Image(s)..." in pop-up menu.
Custom the output folder, number of Thumbnails, image size and other setting.
Click "OK".

Resources