Quality option with -write in ImageMagick - imagemagick

I am using the CLI of imagemagick to export cropped images of a unique picture:
convert -quiet a.jpg -write mpr:uniquePicture \( mpr:uniquePicture -crop 10x10+5+5 +repage -write b.jpg +delete \) null:
In my script there are a lot of "\( \)" with different crop parameters.
This works perfectly but I would like to control the quality of the exported picture ("b.jpg" in my example). If I had "-quality 90" inside the "\( \)" there is an error…
So, is there a way to use the jpg quality inside the "\( \)"?
Thierry

This works fine for me on IM 6.9.12.50 Q16 Mac OSX Monterey.
convert -quiet lena.jpg -write mpr:uniquePicture \( mpr:uniquePicture -crop 10x10+5+5 +repage -quality 90 +write lena_sub.jpg +delete \) null:
I suspect there is something wrong with your command line. But also what is your Imagemagick version and platform.
Note I have use +write, but -write works also. I generally use +write to avoid any issues with format confusion.

Related

Combined Multiple Commands in Imagemagick

I'n very new to imagemagick and i need to learn how could i combined these two commands to make it work.
I need to add background image to a transparent image. I tried to combine these two commands but was not successful.
magick mogrify -path Output_Path -trim -filter Triangle -define filter:support=2 -thumbnail 450x450 -gravity center -extent 500x500 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip Transparent_Image_Path
magick Background_Image.png Transparent_Image.png -composite output.jpg
Result Should Be Like This :
Image Reference
Thanks in advance!
There are several possible syntaxes, depending on how your brain likes to work ;-)
What you need to bear in mind when trying to understand this is that all processing operations, e.g. -crop or -resize apply to all loaded images. So, you either need to load images that need processing and process them before loading images you don't want affected, or you need to restrict processing to certain images only... using parenthesised "aside" operations.
You can proceed like this:
load transparent foreground image first and process it before loading the background image, then
load background image, then
exchange the order and composite
That looks like this:
magick FOREGROUND.PNG -resize ... -crop ... \
BACKGROUND.PNG \
+swap -composite RESULT.JPG
Alternatively, you could:
load the background image, then
load the foreground image and process it "aside" within parentheses so that the background image is not affected
composite
That looks like this:
magick BACKGROUND.PNG \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
If you need to independently process both background and foreground images prior to compositing, use:
magick \
\( BACKGROUND.PNG -resize ... -crop ... \) \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
Note that if you use Windows:
the backslashes at line-ends become carets (^) and may not have trailing spaces after them,
the backslashes preceding opening and closing parentheses must be omitted,
most single quotes probably need replacing with double quotes - probably not applicable to this specific question.
Note that your command is probably unnecessarily complicated, I would recommend omitting quite a lot of it and seeing how you get on. For example, all the define png:XXX=YYY settings are irrelevant if you aren't creating a PNG as output.
You can carry forward the JPEG-related parameters (quality, interlace, upsampling) and put them in anywhere you like, probably at the start like this, but put the colorspace and strip at the end to ensure both input files are stripped:
magick -quality 82 -define jpeg:fancy-upsampling=off \
BACKGROUND.PNG ... \
FOREGROUND.PNG ... \
-composite -colorspace sRGB -strip RESULT.JPG

How can I combine these two imagemagick convert commands?

How can I combine the following two imagemagick convert commands?
convert inverted.png -transparent white -negate temp.png
convert standard.png temp.png -composite final.png
I can't figure out the syntax, though I'm sure I'll kick myself when I see the answer.
You have a couple of options.
First, you can load the inverted image first and negate it, then load the second and swap the order:
convert inverted.png -transparent white -negate standard.png +swap -composite result.png
Or, you can load them in the correct order, but use parentheses to ensure only the second is negated:
convert standard.png \
\( inverted.png -transparent white -negate \) \
-composite result.png
By the way, this is old v6 syntax, you should be using v7 by now which would be:
magick inverted.png -transparent white -negate standard.png +swap -composite result.png

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.

Remove background, reflection and shadows with Image Magick

I have images where I would like to make the background transparent, remove the shadow and remove the product reflection.
So what I want to do is
Remove reflection
Remove shadow
Remove background
Test image
In Imagemagick 6, you can threshold the image and get the bounds of the black area. Then crop the original to those bounds.
convert image.png -threshold 50% +write image_thresh50.png -format "%#" info:
229x367+39+0
convert image.png -crop 229x367+39+0 +repage image_cropped.png
If using Imagemagick 7, change convert to magick.
Is this what you want?
To follow up on Bonzo's comment, in Unix ImageMagick 6, I could do:
cropvals=$(convert image.png -threshold 50% +write hAfUS_thresh50.png -format "%#" info:)
convert image.png -crop $cropvals +repage image_cropped.png
And in Unix, Imagemagick 7, I could do:
magick image.png \( +clone -threshold 50% -set option:cropvals "%#" +delete \) -crop "%[cropvals]" +repage image_cropped2.png
The reason I did not post these was because I did not know what OS/Platform the OP was using. It always helps when asking questions about Imagemagick to post its version and the platform it is run on.
Sorry I do not know how to do this in Windows syntax

How to blend + translate simultaneously with Imagemagick (or some other scriptable software like Photoshop)?

How can I blend AND translate at the same time ?
Something like this : http://www.imagemagick.org/Usage/layers/#flatten but in such a way that the images are transparent.
I was trying :
composite -blend 90 -page +0+0 input01.jpg -page +500+0 input02.jpg -resize x400 outputSimpleMosaicBlend01.
but this did not work.
So if I have two input images:
Then how can I get an image that looks like the composite image below ?
Any suggestions how to do this programatically (not manually) with ImageMagick ? Or some other tools ?
I would like to create several thousands of composite images like that (for an animation) and I would like to automate the process.
The problem is that I can find examples that overlay images and that translate images but I cannot find examples that do these two operations simultaneously.
This is the main goal of this question, to give such code/script examples, how to do that with image manipulation tools like ImageMagick programmatically.
EDIT:
Things that I tried and did not work:
convert a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
gives:
I tried
convert -background none a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
too which gives the same result.
I got this :
with this
convert -background none input01.jpg input02.jpg -geometry +1200+0 -compose blend -define compose:args=50 -composite result.jpg
command.
It's getting close ! Thanks Mark!
A slightly different way of doing this is to set the width of the output image using -extent and then to overlay the right hand image using -gravity East to align it to the right edge - seems a fraction more intuitive to me - but go with whatever works for you!
convert a.jpg -background white -extent 2800x \
\( b.jpg -resize 150% -alpha on -channel A -evaluate set 50% +channel \) \
-gravity east -composite result.jpg
Thanks to Snigbo, the following command :
convert input02.jpg \( input01.jpg -resize 150% -alpha Opaque -channel A -evaluate Multiply 0.5 +channel -set page +1200+30 \) -background White -layers merge a.jpg
produces:

Resources