ImageMagick - Can %d be used together with image property percent escapes? - imagemagick

I'm encountering behavior using ImageMagick's image property percent escapes that I can't explain. Consider this minimal example that exhibits the problem:
convert rose: 'rose_%d.png'
# Resulting filename: rose_0.png (as expected)
convert rose: -set filename:mysize "%wx%h" 'rose_%[filename:mysize]_%d.png'
# Resulting filename: rose_70x46_%d0.png
# Why is '%d' in the output filename?
# Expected: rose_70x46_0.png
convert rose: -set filename:mysize "%wx%h" 'rose_%d_%[filename:mysize].png'
# Resulting filename: rose70x46.png
# Expected: rose_0_70x46.png
Relevant documentation:
https://www.imagemagick.org/Usage/files/#save_escapes
Can %d be used together with image property percent escapes?

Can %d be used together with image property percent escapes?
Only if it comes before %[filename:. Looking at the source code; it appears to be a case statement. The order of precedence allow for field width & %d, but once %[ is trapped, it stops checking on closing brace ] -- or error.
Might be worth jumping over to the developer forums to discuss this behavior.
You can work around it by moving the index order over to the filename:mysize placeholder.
$ convert rose: -set filename:mysize "%wx%h_%p" rose_%[filename:mysize].png
#=> rose_70x46_0.png
$ convert rose: wizard: -set filename:mysize "%wx%h_%p" rose_%[filename:mysize].png
#=> rose_70x46_0.png rose_480x640_1.png

Related

Contrast feature of ImageMagick convert

I just want to use the convert command in a way, so that the pictures get in colorspace gray, negated and with a higher contrast.
The colorspace and negation are not a problem, but the contrast is one. I use the following bash script:
for i in *
do
echo $i
convert $i -colorspace gray gray/$i
convert gray/$1 -contrast -contrast contrasted/$1
convert contrasted/$i -negate negated/$i
done
I moved the contrast command in every possible position, but there always comes the following error:
convert-im6.q16: no decode delegate for this image format `' # error/constitute.c/ReadImage/504.
convert-im6.q16: no images defined `contrasted/' # error/convert.c/ConvertImageCommand/3258.
convert-im6.q16: unable to open image `contrasted/s5_rechts_abgeschnitten.png': Datei oder Verzeichnis nicht gefunden # error/blob.c/OpenBlob/2701.
convert-im6.q16: no images defined `negated/s5_rechts_abgeschnitten.png' # error/convert.c/ConvertImageCommand/3258.
I don't really know what to do, so would be cool if someone got a solution.

Can ImageMagick be prevented from overwriting an existing image?

When converting an image, ImageMagick's default behavior seems to be to overwrite any existing file. Is it possible to prevent this? I'm looking for something similar to Wget's --no-clobber download option. I've gone through ImageMagick's list of command-line options, and the closest option I could find was -update, but this can only detect if an input file is changed.
Here's an example of what I'd like to accomplish: On the first run, convert input.jpg output.png produces an output.png that does not already exist, and then on the second run, convert input.jpg output.png detects that output.png already exists and does not overwrite it.
Just test if it exists first, assuming bash:
[ ! -f output.png ] && convert input.png output.png
Or slightly less intuitively, but shorter:
[ -f output.png ] || convert input.png output.png
Does something like this solve your problem?
It will write to output.png but if the file already exists a new file will be created with a random 5 character suffix (eg. output-CKYnY.png then output-hSYZC.png, etc.).
convert input.jpg -resize 50% $(if test -f output.png; then echo "output-$(head -c5 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c5).png"; else echo "output.png"; fi)

imagemagick convert dng - changes date modified

Edit 26/04/2016 : This is a bug that has been fixed in ImageMagick-6.9.3-5 and 7.0.0-0
.
Converting DNG files, when I run
convert "my_img.dng" "my_img_converted.jpg"
Imagemagick changes the date modified of the original my_img.dng to the time when conversion happened.
.
Do you know how to avoid that and keep the original file intact?
I would say that is a bug! A workaround is as follows:
convert dng:- result.jpg < original.dng
Or, equivalently if you prefer reading left-to-right and don't mind superfluous processes:
cat original.dng | convert dng:- result.jpg

Add ken burn effect on video from list of images

I have created video from list of images using ffmpeg
system("ffmpeg -framerate 1 -pattern_type glob -i '*.jpg' -c:v libx264 out.mp4")
Now i want to add Ken burn effect, can i do it with ffmpeg or imagemagic or any command line tool on linux.
I can't speak of ruby-on-rails, linux, or ffmpeg technologies. But if you would like to create a panning effect made popular by Ken Burn, you would extract regions of an image, and animate them together.
#!/bin/bash
# A 16:10 ratio
WIDTH=64
HEIGHT=40
# Extract parts of an image with -extent operator
for index in $(seq 40)
do
TOP=$(expr 120 + $index)
LEFT=$(expr 150 + $index)
FILENAME=$(printf /tmp/wizard-%02d.jpg $index)
convert wizard: -extent "${WIDTH}x${HEIGHT}+${TOP}+${LEFT}" $FILENAME
done
# Replace this with your ffmpeg script
SLICES=$(ls /tmp/wizard-*.jpg)
RSLCES=$(ls /tmp/wizard-*.jpg | sort -rn)
convert $SLICES $RSLCES -set delay 15 -layers Optimize /tmp/movie.gif
Edited by Mark Setchell beyond this point... (just trying to help out)
Much as I hate editing other people's posts, the first part of Eric's code can equally be written this way if you find that easier to understand:
# Extract parts of an image with -extent operator
for index in {1..40}
do
((TOP=120 + $index))
((LEFT=150 + $index))
FILENAME=$(printf /tmp/wizard-%02d.jpg $index)
convert wizard: -extent "${WIDTH}x${HEIGHT}+${TOP}+${LEFT}" $FILENAME
done

imagemagick convert +append multipul files without using the *

I can not use the * wildcard, but I want to append multiple files into one output file. If there is a variation of the following line that will work I can't find it.
convert -append /home/file1.png /home/file2.png /user/file3.png /test/output.png
Thanks in advance.
waits for the traditional question down voting. :)
This may help:
montage balloon.gif medical.gif present.gif shading.gif \ -mode Concatenate -tile x1 montage_cat.jpg
Just use brace expansions
convert -append /home/file[123].png /test/output.png
or
convert -append /home/file_{1..3}.png /test/output.png

Resources