How to perform Batch processing to Merge channels - batch-file

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:

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.

ffmpeg output Filename from multiple source

I want to have the output file to have same name from source ( for eg first filename from list.txt), how can I do it?
This is the code I have which converts the all the .avi in folder to a single files and renames it to output.avi.
Instead of output.avi I want the filename to be same as first filename from source folder or first filename from list.txt.
for %%f in (*.avi) do (
echo file %%f >> list.txt
)
ffmpeg -f concat -safe 0 -i list.txt -c copy output.avi
del list.txt
One more thing I want to know, is it possible to flip video horizontal or mirror it without re-encoding?
I don't think ffmpeg supports "backreferencing" the input filenames. I suggest handling that in bash or some through other means of scripting. E.g. you can use the first line of list.txt as the output filename like this (untested):
ffmpeg -f concat -i list.txt -c copy output/$(head -1 list.txt)
In this example if the first line is foo.avi, the output will be saved at output/foo.avi.
is it possible to flip video horizontal or mirror it without re-encoding
Apparently you can set rotation metadata without re-encoding to hint video players to play it back with the given rotation. Maybe there are metadata flags for mirroring or flipping, but I couldn't find any.
If this does not work for you, I don't believe there is a built-in solution to do this without re-encoding.
Is it possible to flip video horizontal or mirror it without re-encoding?
No, not when using filters (such as hflip, vflip, rotate, etc). Filtering requires re-encoding.
Rotation metadata as mentioned in another answer may suffice this use case, but support among players is not universal. There is no metadata for mirroring or flipping: only rotation.
If that is not acceptable then the player itself may have mirroring/flipping capabilities. Example:
mpv -vf hflip video.mp4

Automating multiple imagemagick commands

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.

Get image dimensions using batch script (ImageMagick)

What I'm trying to do is use ImageMagick to create a border on an image.
It's a simple process, I can do it manually with hard-coded values, but I want it to be smart.
What it needs to do, is to be able to simply define what image you want the border on, then the script is to run through, get the dimensions of the image, then resize the border image I have to those dimensions, and slap the border on the top.
To do so, from my basic understanding, I'll have to store the width and height from the image as variables. The closest I've gotten (actually getting the info) is
identify "image.png"
That spits out the info that I need, but I need to store the width/height into variables to use them in later processes.
I found the following code from https://superuser.com/questions/323970/how-can-i-get-the-image-dimensions-of-a-targa-file-through-a-batch-script
(NOTE: My .bat file is called "imagecheck.bat"
identify -format "imagecheck2.bat %w %h" image.png > temp.bat
temp.bat
From what the poster of that code said, it will get the width and height (%w and %h) from image.png, then store it in imagecheck2.bat as variables that I can access as %1 (for %w) and %2 (for %h)
So, with that being said, inside imagecheck2.bat, I had
#echo off
echo The parameters were: %1 and %2
pause
And it prints out
The parameters were: and
This should be such a simple thing; any ideas what I'm missing?
Thanks in advance.
Here is a way that this can be done without the use of any auxiliary files.
for /f "tokens=1-2" %%i in ('identify -ping -format "%%w %%h" logo:') do set W=%%i & set H=%%j
echo width: %W%
echo height: %H%
Replace "logo:" with the filename of your image.
That said, I strongly recommend not using MS Batch, as the more complex your program gets, the more ridiculous the code will get.
By the way, the use of the -ping flag in that identify command greatly speeds up the operation because it tells identify not to load the whole image into memory, just enough of it to figure out the dimensions.
Rather than using ImageMagick, you can simply pull the width and height out of the PNG. They are in known locations and each is a 4-byte integer in network byte order (most significant byte first). The offsets from the beginning of the file are
signature: bytes 0-7
IHDR length: bytes 8-11
"IHDR": bytes 12-15
width: bytes 16-19
height: bytes 20-23

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