How to composite two unique series of transparent PNGs? - imagemagick

I have the following two unique series of transparent PNGs:
folder1/*-0.png, folder1/*-1.png, folder1/*-2.png, etc.
folder2/*-0.png, folder2/*-1.png, folder2/*-2.png, etc.
And I want the output to be the combined images in the parent directory as:
*-0.png, *-1.png, *-2.png, etc.
Meaning *-0.png would be folder1/*-0.png overlaid on top of folder2/*-0.png.
Here's the script I successfully used to do it with a single pair, but couldn't with a batch:
convert folder1/*-0.png folder2/*-0.png -gravity center -composite -format png -quality 100 output.png

I still don't understand, but think you want a bash script like this:
#!/bin/bash
cd folder1
# Iterate over all PNGs
for f in *.png; do
this="$f"
that="../folder2/$f"
result="../$this"
echo Combining $this with $that to produce $result
echo convert "$this" "$that" -gravity center -composite "$result"
done
You would save that as merge in your HOME directory, then start Terminal and run this command to make it executable (only necessary once):
chmod +x merge
Then you can use cd to go to the parent directory, e.g.:
cd where/the/images/are
and run the command with:
$HOME/merge

Related

Merge a sequence of JPEG images into a grid losslessly with FFmpeg

I have a sequence of images that are blocks of a larger image, which together make up the whole image. The blocks are the result of splitting the original image along evenly spaced horizontal and vertical lines, so they don't have weird dimensions.
Is there a way to combine them with FFmpeg (or something else like ImageMagick) without re-encoding the images?
This answer suggests the hstack or vstack FFmpeg filter, but my image blocks aren't necessarily the full width or the full height of the original image.
Like this:
Perhaps this could be achieved with multiple FFmpeg commands using hstack or vstack (I'd prefer just one command though). Or with a complex filter?
e.g.
Edit: I tried using filter_complex with FFmpeg:
ffmpeg -i 0.jpg -i 1.jpg -i 2.jpg -i 3.jpg -i 4.jpg -i 5.jpg \
-filter_complex "[0][1]hstack=inputs=2[row 0]; \
[2][3]hstack=inputs=2[row 1];
[4][5]hstack=inputs=2[row 2];
[row 0][row 1][row 2]vstack=inputs=3[out]" \
-map "[out]" -c copy out.jpg
but it can't filter and copy streams at the same time.
You can do that with ImageMagick. However, it will decompress and recompress your jpg files. You will lose some quality.
Unix Syntax for IM 6:
convert \
\( 0.jpg 1.jpg +append \) \
\( 2.jpg 3.jpg +append \) \
\( 4.jpg 5.jpg +append \) \
-append \
mona_lisa.jpg
If using Windows remove the \ from the parentheses and replace the end of line \ with ^.
If using IM 7, replace convert with magick.
I found an interesting patch from 2010 against libjpeg which adds this feature to jpegtran. It won't split blocks, so your images will need to be multiples of 8 or even 16 pixels in each axis.
Unfortunately it's against the libjpeg 6b as distributed for ubuntu10. This includes some patches from 8d and doesn't really correspond neatly to any official libjpeg version (as far as I can see).
You need to download the ubuntu10.04 sources, extract their libjpeg, and patch that. The steps are:
wget http://old-releases.ubuntu.com/releases/releases/10.04/release/source/ubuntu-10.04-src-1.iso
Now open that ISO and pull out the files from ubuntu/pool/main/libj/libjpeg6b. Archive Manager can do this, perhaps there's some scriptable tool as well.
Now run:
# original sources
tar xf libjpeg6b_6b.orig.tar.gz
cd jpeg-6b
# apply ubuntu patches
zcat ../libjpeg6b_6b-15ubuntu1.diff.gz | patch
# apply jpegtran patches
patch < ../append-jpeg6b.patch
./configure
make
sudo make install
That will install the modified jpegtran to /usr/local.
Given 0.jpg and 1.jpg:
You can run it like this:
jpegtran -appright 1.jpg 0.jpg > join.jpg
To make:

Run convert from ImageMagick script file

I am trying to stitch (thousands) of images together via coordinate system using ImageMagick. Because the command is so long, I can't pass it via CLI, but have to save it to a script to load later.
The script looks like this:
script.txt
convert
( foo.png -repage +0+0 )
( bar.png -repage +0+37 )
-layers merge
out.png
I then run magick.exe -script script.txt
But get the following error:
magick.exe: unable to open image 'convert': No such file or directory # error/blob.c/OpenBlob/3457.
magick.exe: no decode delegate for this image format `' # error/constitute.c/ReadImage/512.
The -script command is for ImageMagick 7. You must explicitly -write the output in the script command. See http://www.imagemagick.org/Usage/bugs/IMv7_Scripting.txt
For example, here is a simple script file that I call testscript
#!/path/to/magick -script
rose: -scale 200% -write big_rose.png
To run it you simple use:
magick -script testscript
So for your command, try the script as
#!/path/to/magick -script
( foo.png -repage +0+0 ) ( bar.png -repage +0+37 ) -layers merge -write out.png
(Note I have written it as one line. If you want to use multiple lines, then you must use Windows line ending character, ^, at the end of each line to show continuation to the next line)
Then call it as
magick -script yourscript

Resize indexed PNG image with ImageMagick while preserving color map

I am using custom batch script to make resized copies (33% and 66%) of all PNG images in folder. Here is my code:
for f in $(find /myFolder -name '*.png');
do
sudo cp -a $f "${f/%.png/-3x.png}";
sudo convert $f -resize 66.67% "${f/%.png/-2x.png}";
sudo convert $f -resize 33.33% $f;
done
It works fine, except when the original image is indexed. In this case the smaller version of the image is RGB (so even larger file size then original image).
I have try several versions but not worked. One that I guess supposed to sort this out was fallowing:
for f in $(find /myFolder -name '*.png');
do
sudo cp -a $f "${f/%.png/-3x.png}";
sudo convert $f -define png:preserve-colormap -resize 66.67% "${f/%.png/-2x.png}";
sudo convert $f -define png:preserve-colormap -resize 33.33% $f;
done
But it doesn't work.
EDIT:
This is updated co, but it still doesn't work as it supposed to (see the attached image-left is original, right is resized):
for f in $(find /myFolder -name '*.png');
do
sudo cp -a $f "${f/%.png/-3x.png}";
numberOfColors=`identify -format "%k" $f`
convert "$f" \
\( +clone -resize 66.67% -colors $numberOfColors -write "${f/%.png/-2x.png}" +delete \) \
-resize 33.33% -colors $numberOfColors "$f"
done
Original image:
Scaled version:
Use "-sample" instead of "-resize" to preserve the color set. This causes the resizing to be done by nearest-neighbor color selection rather than any kind of interpolation.
Otherwise, the colormap ends up with more than 256 colors and the png encoder can't preserve it, due to the 256-color limit on the size of a PNG PLTE chunk. I cannot guarantee that you'll like the appearance of the result, though.
Also, be sure you are using a recent version of ImageMagick.
I'm not observing this problem with the current release (6.9.3-7). Your script works fine and produces clean -2x and -3x images.
There are several things to address here...
find vs glob
You say you want to process all files in a folder, then you use find which will search down into sub-directories as well. If you just want to process files in the current directory, you can let bash do the globbing directly for you. So, instead of
for f in $(find . -name "*.png"); do
you can just do:
shopt -s nullglob
for f in *.png; do
Performance
You run convert twice and load the original image twice, and that is not very efficient. You can run a single process that loads a single image and resizes to two different sizes and writes both to disk. So, instead of
for ...; do
convert ...
convert ...
done
you can write the following to start one convert, read the image once, clone it in memory and write it out, delete the spare copy in memory and then resize the original image and re-save that.
for ...; do
convert "$f" \
\( +clone -resize 66.67% -write "${f/%.png/-2x.png}" +delete \) \
-resize 33.33% "$f"
done
Palette
It seems you actually only want to output palettised (indexed) images with "any" colormap rather than with a "specific" colormap. Glenn's answer is perfect if you want to retain a specific colormap. However, if any colormap is ok, you can use -colors to reduce the colours in the resulting image to a level where the PNG library can make the decision to create a palettised image. Glenn knows a lot more than me about that as he wrote it! However, I think if you reduce the colours to 250 (or so) you will probably get a 256 entry colormap and if you reduce the colours to around 60 or so, you will get a 64 entry colourmap. So, you would do:
shopt -s nullglob
for f in *.png; do
sudo cp ... ...
convert "$f" \
\( +clone -resize 66.67% -colors 250 -write "${f/%.png/-2x.png}" +delete \) \
-resize 33.33% -colors 250 "$f"
done
You can try experimenting with other numbers of colours and see how that affects filesize - the number you need will depend on your images.

ImageMagick: Watermark image and keep file name

I have this code but I would like to edit it so the exported file name is the same as the file name used to make it
mogrify -composite -gravity center ~/Desktop/civmap-client-master/data/watermark.png ~/Desktop/civmap-client-master/data/nowm/*.png ~/Desktop/civmap-client-master/data/wm/*.png
It just throws an error when I used *.png as my exported file name. Please note I have multiple files in the /nowm/ directory. I am using a Mac.
Thanks alot :)
The mogrify command does not accept -composite as an operation as it takes just 2 images. You will have to use a loop:
cd ~/Desktop/where/the/input/images/are
for f in *.png; do
echo $f
convert -gravity center "$f" "~/Desktop/path/to/watermark.png" -composite "~/Desktop/path/to/output/$f"
done

Trim command /imagemagick in command line to remove white space from iamges in /uploads folder

I can't work out the syntax.
Client has loaded 8000 images, all with huge whitespace around them.
how do i run a imagemagick trim command from the command line, that will trim all images in the /uploads folder and also from all files in subdirectories
Is the whitespace always the same size? If yes determine it and adapt the crop options. Moreover you will probably need to filter on file name.
find . -name *.jpg -type f -print0 | xargs -0 -I{} convert {} -crop 40x30+10+10 {}
Use -trim argument with the ImageMagick's mogrify utility.
mogrify -trim /uploads/*
Repeat for sub-directories, or use find utility.

Resources