Using Imagemagick, save its variables per use - imagemagick

History: I try to apply a watermark on a series of images in a directory, at linux, putting that watemark on an extra created space / border below the image. Both Watermark and the extra space is resized / created depending on the height size on the image they are applied.
I do various calculations based on the height of the image that is processed each time that they are saved on some variables. However, I do not know how to a) Make a script to be applied to all the images on the directory, using the saved height calculations PER IMAGE, OR b) to be applied these calculations PER IMAGE, as "single terminal command" e.g. "for pic in DSC*.*; do height calculations....... and saved at that variable to be used for the next batch terminal command"
The variables of imagemagick. How I saved them to be used for the next use, per image?
height=`convert DSC__12.jpg -format "%w" info:`
yoff_p50=`convert xc: -format "%[fx:$height/50]" info:`
yoff_p100=`convert xc: -format "%[fx:$height/100]" info:`
yoff_p200=`convert xc: -format "%[fx:$height/200]" info:`
I tried the following linux terminal command: Is there a better elegant (shorter?) way?
for pic in DSC*.*;
do height=`convert "$pic" -format "%w" info:`;
yoff_p50=`convert xc: -format "%[fx:$height/50]" info:`;
yoff_p100=`convert xc: -format "%[fx:$height/100]" info:`;
yoff_p200=`convert xc: -format "%[fx:$height/200]" info:`;
convert -background black -gravity NorthWest -extent 0%x0%+0+${yoff_p50} -pointsize ${yoff_p100} -fill white -undercolor '#00000080' -gravity SouthWest -annotate +${yoff_p100}+${yoff_p200} "$(stat -c '%y' "$pic")" "$pic" "${pic//.*}-d.jpg";
done;
for pic in DSC*-d.*;
do height=`convert "$pic" -format "%w" info:`;
yoff_p50=`convert xc: -format "%[fx:$height/50]" info:`;
yoff_p100=`convert xc: -format "%[fx:$height/100]" info:`;
yoff_p200=`convert xc: -format "%[fx:$height/200]" info:`;
convert /home/elias/Data/PHOTOS/watermark_0.png -resize ${yoff_p50}x${yoff_p50}^ /home/elias/Data/PHOTOS/res_watermark_0.png;
composite -dissolve 100% -gravity SouthEast /home/elias/Data/PHOTOS/res_watermark_0.png "$pic" "${pic//.*}-marked.jpg";
done;
ps. How I can check what values are saved per imagemagick variable?
i found out how to check the variable value in linux terminal: echo ${height}
UPDATE: Solution: The solution by Mark Setchell worked.
Finally, i run: ..and it did the job from linux terminal very fine.
for pic in DSC*.* ; do
#Save either the date of File Creation or Modification on filename
exiftool "-FileName<CreateDate" -d "${pic//.*}_%Y%m%d_%H%M%S.jpg" "$pic" && exiftool "-FileName<FileModifyDate" -d "${pic//.*}_%Y%m%d_%H%M%S.jpg" "$pic" ;
done;
for pic in DSC*.* ; do
# Determine offsets and sizes
read w y1 y2 y3 < <(identify -format "%w %[fx:w/50] %[fx:w/100] %[fx:w/200]" "$pic");
ts=$(stat -c '%y' "$pic");
convert -size ${w}x${y1} xc:black -gravity SouthWest \
-pointsize ${y2} -fill white -undercolor '#00000080' -annotate +${y2}+${y3} "$ts" \
\( /home/elias/Data/PHOTOS/res_watermark_0.png -resize "${y1}x${y1}^" \) -gravity East -composite \
"$pic" +swap -append "${pic//.*}-marked.jpg";
done

It can probably be improved further, but I would go with something more like this:
#!/bin/bash
for f in DSC* ; do
# Determine offsets and sizes
read w y1 y2 y3 < <(identify -format "%w %[fx:w/50] %[fx:w/100] %[fx:w/200]" "$f")
# Get timestamp - different on macOS. You want: ts=$(stat -c '%y' "$f")
ts=$(stat -f "%Sm%n" "$f")
convert -size ${w}x${y1} xc:black -gravity SouthWest \
-pointsize ${y2} -fill white -undercolor '#00000080' -annotate +${y2}+${y3} "$ts" \
\( watermark.png -resize "${y1}x${y1}^" \) -gravity East -composite \
"$f" +swap -append watermarked-"$f".jpg
done
Things to note:
The line beginning read w y1... gets all the calculated values in a single go - this will be quicker.
The line beginning ts=$(...) gets the timestamp. I abstracted it out because I am on macOS and it is different. You can re-integrate it, if you want.
The final line is where the action is. I create a black annotation bar the right size first, then write in the timestamp in white on the left. Then, on the next line I load the watermark and resize it and splat it into the right corner of the black annotation bar. Then, on the next line, I load the main image, and swap the order so the annotation bar is at the bottom and the image is at the top before appending.
Remember when debugging ImageMagick scripts, you can add in -write "debug.png" at any position to see how something looks after resizing , or before compositing or wherever.

Another way to save variables in one ImageMagick command is using declare.
declare `convert xc: -format "yoff_p50=%[fx:$height/50]\nyoff_p100=%[fx:$height/50]\nyoff_p200=%[fx:$height/200]\n" info:`
Example:
declare `convert rose: -format "ww=%[fx:w]\nhh=%[fx:h]\n" info:` echo "ww=$ww; hh=$hh;"
ww=70; hh=46;

Related

How to scale and crop a folder of images?

I've been looking at the documentation for imagemagick and looking at examples of scripts others have made, but I haven't been able to get this working.
The goal is to have imagemagick scale, crop, and save (appending the dimensions to the file names) multiple resized images of various aspect ratios.
For example, a folder containing Image1.png and Image2.png would result in:
Image1_1571x2646.png, Image1_1350x2150.png, Image1_1281x2039.png
Image2_1571x2646.png, Image2_1350x2150.png, Image2_1281x2039.png
Visual aid:
The animation above shows the simplest examples: a 1:1 square, a vertical rectangle, and a horizontal rectangle.
The images should scale to fit the longest dimension of the rectangle, and then crop any leftover pixels. The scaling and cropping should be done relative to the image centers.
Here's what I have so far (using macOS Terminal) but it doesn't work:
convert *.png -path /Users/user/Resized \
\( +clone -resize "1571x2646^” -gravity center -crop 1571x2646+0+0 +repage resultimage -write 1571x2646.png +delete \) \
\( +clone -resize "1350x2150^” -gravity center -crop 1350x2150+0+0 +repage resultimage -write 1350x2150.png +delete \) \
-resize "1281x2039^” -gravity center -crop 1281x2039+0+0 +repage resultimage 1281x2039.png
I'm not sure if I should use mogrify or convert, but if I use mogrify clone gives an error. I'm also not sure if multi-line instructions need to be put into a .sh file or something. The ^ denotes the dimension that should take priority (the larger one). I believe -gravity center is supposed to keep scaling and cropping relative to the image centers.
With Imagemagick, you must use convert. Mogrify cannot handle the parenthesis process and clones, nor can it write multiple outputs for a given input. The ^ is the correct way and -gravity center is correct. You will have to loop over each input image. I do not think you can use wild cards to process more than one image at a time with this type of command. I think -path is only for mogrify.
I would write a loop over each of your input images (bash unix syntax):
cd
cd /Users/user/Resized/
list=`ls`
for img in $list; do
name=`convert "$img" -format "%t" info:`
convert "$img" \
\( -clone 0 -resize "1571x2646^" -gravity center -crop 1571x2646+0+0 +repage +write ${name}_1571x2646.png +delete \) \
\( -clone 0 -resize "1350x2150^" -gravity center -crop 1350x2150+0+0 +repage +write ${name}_1350x2150.png +delete \) \
\( -clone 0 -resize "1281x2039^" -gravity center -crop 1281x2039+0+0 +repage +write ${name}_1281x2039.png +delete \) \
null:
done
The above assumes that your input images have no spaces in their names.
I have changed from +clone to -clone 0, since I am not sure if you change aspect ratio from output to output whether that will cause problems. You can try both ways and see which looks best.

Merge two Imagemagick convert commands

I'm trying to Create a thumbnail from GIF 1st command and then merge another image on top of it 2nd command.
1st command:-
convert -thumbnail 398x398 -auto-orient -quality 85 giphy.gif[0] output.jpg
2nd command:- convert -size 1920x1080 xc:none output.jpg -blur 5x4 -composite out1.png -gravity center -composite outfinal.png
I've tried using like this but didn't work out:
convert -thumbnail 398x398 -auto-orient -quality 85 giphy.gif[0] output.jpg | convert -size 398x398 xc:none output.jpg -blur 5x4 -composite out1.png -gravity center -composite outfinal.png
Image extracted from gif:-
2nd image:
Final output as merge two images :
Thank you:)
This is a pretty easy way to do it:
convert -gravity center background.jpg -blur 5x4 \
\( yinyang.png -resize 130x78\! \) \
-composite result.jpg
I put a spurious -resize 130x78\! in there so you can see where to do extra operations that only affect the little "yin yang" picture in the middle - you can obviously remove it.
Maybe I can explain the logic... the first line of the command deals with the background, the second line deals with the overlay. So, first, load up the background picture and make all the edits you want to it, i.e. blurring. Then start some "aside-processing" in parentheses which loads up the "yin yang" and applies some edits to that exclusively without affecting the background image. Then, when you are happy with the overlay, exit the parentheses and take the result of the "aside-processing" and overlay it on top of the background. It overlays into the centre because I set the gravity beforehand.
Hope that helps.
Added in response to your comment... if you want to do the extraction as well, just do that in the "aside-processing":
convert -gravity center \
giphy.gif[0] -thumbnail 398x398 -auto-orient -blur 5x4 \
\( yin yang -resize 300x100\! \) \
-composite result.jpg

Convert multiple Imagemagick commands into 1 command (trim, resize, square)

Currently, I am using multiple imagemagick commands to trim, resize (if width or height > 5000) and square. Is it possible to combine into 1 single command?
step 1: convert input_file.tif -fuzz 1% -trim output_file_trim.tif
step 2: get new image width and height using identify command from output_file_trim.tif
step 3: get max dimension from image width and height
step 4: if max dimension > 5000 then
convert output_file_trim.tif -resize 5000x5000 output_file_trim.tif
Step 5: Finally, finish the image conversion
convert output_file_trim.tif -flatten -gravity center -background white -extent "$max_dimension"x"$max_dimension" -format jpg output_file_final.jpg
#fmw42. Is the following single command correct to achieve this requirement:
convert `input_file.tif` -fuzz 1% -trim +repage \( +clone -rotate 90 +clone -mosaic +level-colors white \) +swap -flatten -gravity center -extent 105x105% -composite -format jpg `output_file_final.jpg`
This command will read the input image and trim it. Then it resizes it to fit in a 5000x5000 box if it's larger than 5000x5000. Then it re-dimensions the canvas to a square with both dimensions being the larger of the width or height. It finishes by placing the image in the center of that square canvas with a white background.
convert input_file.tif -fuzz 1% -trim +repage -resize "5000x5000>" \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]" -virtual-pixel white \
-distort affine "0,0 %[fx:h>w?(h-w)/2:0],%[fx:w>h?(w-h)/2:0]" \
output_file_final.jpg
Putting your 5 steps into one command can only be done in IM 7 as follows (unix syntax):
magick -quiet input_file.tif -fuzz 1% -trim +repage \
-resize "5000>" \
-flatten -gravity center -background white \
-extent "%[fx:max(w,h)>5000?5000:max(w,h)]x%[fx:max(w,h)>5000?5000:max(w,h)]" \
output_file_final.jpg
In IM 6, you need to do it in two command. First find the larger of the max(w,h) and 500 as dim and save a temp image from your step one. Then do another command to finish it using that dim
dim=$(convert -quiet input_file.tif -fuzz 1% -trim +repage \
+write output_file_final.jpg -format "%[fx:max(w,h)>5000?5000:max(w,h)]" info:)
convert output_file_final.jpg -resize "5000>" \
-flatten -gravity center -background white \
-extent ${dim}x${dim} output_file_final.jpg
I do not understand your last command. It does not relate to the steps you outlined.

How to find out the average color (rgb) of masked area of an Image

I would like to get the average color in rgb of the mask area of a png image.
the following just outputs the average color of the whole image
convert demo.png -mask demo_mask.png -resize 1x1 txt:-
In ImageMagick, -scale 1x1! will ignore the transparent pixels and give you the average of the opaque pixels. So if you want the average of the masked region, you can put the mask into the alpha channel and then use -scale 1x1! to average it down to one pixel. The mask should be white where you want to get the average and black where it should be transparent and ignore those pixels in the average. So this should do it.
convert image.png mask.png -alpha off -compose copy_opacity -composite -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:
For example if I make the logo: image transparent where it is white and then get the average, I get
convert logo: -transparent white -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:
srgb(100,81,99)
You can show that this works, by doing it the long way. Multiply the mask by the image, then get the average of each channel of the product. Then get the average of the mask. Then compute the ratios scaled to the range 0 to 255.
convert logo: -transparent white logot.png
convert logot.png -alpha extract mask.png
declare `convert \( logot.png -alpha off \) mask.png -compose multiply -composite -format "IR=%[fx:mean.r]\nIG=%[fx:mean.g]\nIB=%[fx:mean.b]\n" info:`
echo "IR=$IR; IG=$IG; IB=$IB"
IR=0.0651798; IG=0.0529989; IB=0.0641607
MM=`convert mask.png -format "%[fx:mean]\n" info:`
echo "MM=$MM"
MM=0.165872
convert xc: -format "srgb(%[fx:round(255*$IR/$MM)],%[fx:round(255*$IG/$MM)],%[fx:round(255*$IB/$MM)])\n" info:
srgb(100,81,99)
Result is the same as above.
ASIDE: Note that
convert \( logot.png -alpha off \) mask.png -compose multiply -composite ...
in this case is the same as just
convert logot.png -alpha remove ...
But I show it the long way, if the user has a separate mask and image with no transparency.
You should be able to get very nearly what you want with a command something like this...
convert image.png mask.png -compose copyopacity -composite -resize 1x1! txt:-
To output only the color information you can try something like this...
convert image.png mask.png \
-compose copyopacity -composite -resize 1x1! -format "%[pixel:p]" info:
I haven't tried it, but you may have to "-negate" your mask image depending on which version of IM you're using because there have been changes in the way the alpha channel is handled.
You'll get a very slightly different result if you "-trim" before the "-resize".
If you don't want to output the alpha channel information, you can add "-alpha off" after the "-resize".
Perhaps...
convert demo.png -mask demo_mask.png -trim -fx mean -extent 1x1 txt:- |\
tail -1 | cut -d ' ' -f 4
This would work because -trim will reduce the masked image down to the MBR of the ROI. Fx operator -fx mean will convert all pixels into the overall average. And finally the -extent 1x1 will isolate the first pixel in the image. The rest is basic unix utilities.
Another option with better performance...
MEAN=$(convert demo.png -mask demo_mask.png -trim -format '%[fx:mean]' info:-)
convert null: -depth 8 -format "%[pixel:$MEAN]" info:-
Or from the Quantization documentation...
convert demo.png -mask demo_mask.png -trim -scale 1x1\! '%[pixel:s]' info:-

Image magick to put images in row and then generate reflection

I was wondering if I can use imagemagick to produce a result image like this
My initial image will be like this
I am able to create the dark image to be put in the background like this -
convert -brightness-contrast -20x10 GARDENS-ILLUSTRATED_JAN-14.jpg out_lighter2.jpg
But I do not know how to put them in line like that and then generate a reflection.
I have got something close, but have no more time, so maybe you can fiddle with it and get it to what you want:
#!/bin/bash
input=input.png
# Calculate width and height
w=$(convert $input -ping -format "%w" info:)
h=$(convert $input -ping -format "%h" info:)
# Calculate how much to chop off bottom and height of reflection
chop=$(echo $h*60/100|bc)
refl=$(echo $h*20/100|bc)
convert $input -alpha on \
\( +clone -flip -size ${w}x${refl} gradient:gray40-black -alpha off -compose CopyOpacity -composite \) \
-append -background white -compose Over -flatten -gravity South -chop 0x${chop} output1.png
# Darken and reduce for second layer
convert output1.png -brightness-contrast -25x8 -resize 90% output2.png
# Darken and reduce for third layer
convert output2.png -brightness-contrast -25x8 -resize 90% output3.png
# Make big new output canvas
neww=$(echo $w*1.6|bc)
newh=$(echo $h*1.6|bc)
# Splat images onto it with offsets
convert -size ${neww}x${newh} xc:transparent -page +0+80 output3.png \
-page +40+40 output2.png \
-page +80+0 output1.png -flatten output.png
My basic idea is to generate the bright, frontmost image first in output1.png, then darken and reduce it into output2.png and again into output3.png. Then composite them all together into output.png with a little offset.

Resources