ImageMagick: delete input files when appending? - imagemagick

When I try
magick mogrify +append 1.jpg 2.jpg 3.jpg
I get an error saying: "mogrify: unable to open 3.jpg". Is there a way to automatically delete the two input files?

Just collating the comments together for clarity...
You can append 3 images side-by-side, and remove the input images if the command succeeds like this:
magick 1.jpg 2.jpg 3.jpg +append result.jpg && rm [123].jpg
By the way, +append is quite long in the tooth and there is a more recent, more useful operator called +/-smush which works exactly the same as +/-append but allows you to create a gap with the +smush version and an overlap with the -smush version:
magick 1.jpg 2.jpg 3.jpg -background black +smush 10 result.jpg && rm [123].jpg

Related

Combine multiple command line commands into one single command (pipeline)

How do I combine the following ImageMagick command line commands into a single instruction:
convert -trim "C:\test\test.webp" -quality 95 "C:\test\testMaxNew.webp"
convert "C:\test\testMaxNew.webp" -resize 750x750 "C:\test\testMediumNew.webp"
convert "C:\test\testMediumNew.webp" -thumbnail 100x100^ "C:\test\testSmallNew.webp"
After some trial and error I came up with this:
convert -trim C:\test\test.webp -quality 95 -write mpr:XY +delete \( mpr:XY +write C:\test\testLargeNew.webp +delete \) \( mpr:XY -resize 750x750 +write C:\test\testMediumNew.webp +delete \) \( mpr:XY -resize 100x100^ -gravity center -extent 100x100 +write C:\test\testSmallNew.webp +delete \)
This does the trick but the following errors are reported in the command line prompt:
> convert: unable to open image '\(': No such file or directory #
> error/blob.c/OpenBlob/2695. convert: no decode delegate for this image
> format `' # error/constitute.c/ReadImage/508. convert: unable to open
> image '\)': No such file or directory # error/blob.c/OpenBlob/2695.
> convert: no decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\)':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: no images defined `\)' #
> error/convert.c/ConvertImageCommand/3235.
Can someone explain why I am getting these errors and if my code is correct?
The reason I am trying to combine multiple commands is to minimize processing time.
Version: ImageMagick 7.0.2-0 Q16 x64 on Windows 10
These should do what you want.
Best to read the input right after convert.
I am not sure why you need to resize and thumbnail.
So in Imagemagick try one of the following:
convert "C:\test\test.webp" -trim +repage -quality 95 -resize 750x750 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
or just
convert "C:\test\test.webp" -trim +repage -quality 95 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
Just adding an answer to clarify a couple of things that are not a good fit for comments.
You already have some excellent advice from Fred (#fmw42) as regards reading your input image immediately after convert because that way your command will continue to work when you upgrade to ImageMagick v7 which has already been available a couple of years.
You have added a command that works to your question, but that is a bit clumsy inasmuch as it creates an MPR which you don't need and also creates and destroys images unnecessarily - adding to system load which is undesirable if you have many images to process. I think you can see the following is simpler to understand and maintain, makes fewer copies and deletes and demands on memory, and should achieve the same effect as your command:
convert INPUT.webp -trim +repage -gravity center -quality 95 ^
+write LARGE.webp ^
-resize 350x350 +write MEDIUM.webp ^
-resize 100x100^ -extent 100x100 SMALL.webp

Getting Improper image header error in ImageMagick 7.0.7

I am running ImageMagick on Mac OS High Sierra
The following commands work fine with ImageMagick 6.9.0
convert -define stream:buffer-size=0 png:- png:- -alpha off -compose copy-opacity -composite png:-
compare -define stream:buffer-size=0 -fuzz 17% -metric AE png:- png:- png:-
I updated ImageMagick 6.9.0 to ImageMagick 7.0.7-28 then both of these commands started failing with the below error messages
magick -define stream:buffer-size=0 png:- png:- -alpha off -compose copy-opacity -composite png:-
magick: improper image header `/var/folders/tb/11n2czg57ts9dzxypdsmlqk99c46m9/T/magick-26635byT1dtHsAuT3' # error/png.c/ReadPNGImage/4231.
magick compare -define stream:buffer-size=0 -fuzz 17% -metric AE png:- png:- png:-
For input string: "compare: improper image header `/var/folders/tb/11n2czg57ts9dzxypdsmlqk99c46m9/T/magick-26363xwcFdjrv3CT0' # error/png.c/ReadPNGImage/4231."
Can you please guide me what I have to change in those commands?
If I store png files in a temporary folder and run the above commands by replacing png:- with temporary file path then both the commands run fine. But I want the commands to work with InputStream.
Thanks in advance!
I'm not sure you can stream two PNG files concatenated together as PNGs into ImageMagick's input stream. Essentially, you are trying to do this:
{ convert xc:red png:- ; convert xc:blue png:- ; } | convert png:- png:- -append result.png
which doesn't work, because, I think, the final convert will read the entire input stream when it encounters the first png:- and there will be nothing left for the second png:-.
I think the only safe way to send multiple concatenated images through a single channel is by using the MIFF "Magic Image File Format" which does support multiple images:
{ convert xc:red miff:- ; convert xc:blue miff:- ; } | convert miff:- -append result.png

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

resize images in a folder with imagemagick and move to another with specific name for file

I need to take file from folder Images
and move it to folder test
and have them renamed
image.jpg -> image_big.jpg
i do that: mogrify -resize 200 -path images/../test/ images/*.*
works great!
when i try to change the name of the file like this
mogrify -resize 200 -format %t_big.%e -path images/../test/ images/*.*
i get the file name something like image._big.big
i tried with convert (but i will have 3000 images and i read that it uses the ram and not doing it like mogrify
convert images/*.jpg -resize 200 images/../test/
what can i do?
For mogrify you an use the -set & -fromat options.
mogrify -resize 200x -set filename:f "big.%e" \
-format "%[filename:f]" -path test/ images/*
This will give destination filenames of "image.big.jpg"
For convert, use bash to iterator over all 3000 files
for infile in `ls images/*.jpg`
do
convert $infile -resize 200x -set filename:f "%t_big.%e" test/%[filename:f]
done
This will give you filenames like "image_big.jpg"

Resources