Page 1 of 2
Making sure image has at least given dimensions after resize
Posted: 2018-01-31T14:33:58-07:00
by Mihamix
Hi,
I have to provide a windows batch script to handle the autocropping, resizing, potentially rotating and finally applying watermark image on of 5000+ images.
The problem is that the watermark logo is sometimes bigger than the autcropped & resized image, resulting in hideously cropped watermarked images. This is primarily if the item on the image is elongated and aligned with the X or the Y axis.
Is there any way I can - after the autocrop and resize - enforce a certain size (or ratio) (and fill the background with white)?
My command is:
Code: Select all
mogrify -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" -gravity "center" -draw "image src-over 0,0 0,0 'Watermarks\wm.png'" -path "TestTmp\bn\m" "Source\Original\*.jpg"
what option shall I be adding?
Many thanks in advance
My ImageMagick version is below, running on Windows Server 2008R2:
Code: Select all
Version: ImageMagick 7.0.7-21 Q16 x64 2018-01-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib jng jp2 jpeg lcms lqr open
exr pangocairo png ps raw rsvg tiff webp xml zlib
Re: Making sure image has at least given dimensions after resize
Posted: 2018-01-31T15:47:44-07:00
by fmw42
With mogrify, you will have to preprocess the background image first to do the trim, resize and rotate. Then get its dimensions. The resize the overlays image to some fraction of the size of the processed background image.
In IM 7, if you write a script loop over each image to be processed, then you can use magick rather than mogrify (which in IM 7 should be magick mogrify) with inline computations to get the size of the processed image and scale the overlay all in one magick command.
For example the magick command would be something like the following to make the watermark image 20% of the max dimension of the processing background image. Adjust the 20 as desired.
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" -set option:size="%[fx:20*max(w,h)/100]" ( Watermarks\wm.png -resize "%[size]" ) -gravity "center -compose over -composite TestTmp\bn\m\yourimage
Modify as needed to put into a bat script. E.G. some % need to be doubled as %%, such as -fuzz 10%%. Sorry I am not a Windows programmer.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T04:54:47-07:00
by Mihamix
fmw42 wrote: ↑2018-01-31T15:47:44-07:00
With mogrify, you will have to preprocess the background image first to do the trim, resize and rotate. Then get its dimensions. The resize the overlays image to some fraction of the size of the processed background image.
In IM 7, if you write a script loop over each image to be processed, then you can use magick rather than mogrify (which in IM 7 should be magick mogrify) with inline computations to get the size of the processed image and scale the overlay all in one magick command.
Sorry, I think I wasn't clear enough and therefore got misunderstood.
Since the website the images are to be produced for supports images with ratio 3:5 - 5:3, these ultra wide images aren't really doing well and looking awkward. Hence (and the fact that the watermark also doesn't look well on these images) the decision is to enforce a minimum size for both dimensions.
So, the idea is:
input image -> trim -> resize -> rotate "90<" -> ??? (enforce size) -> draw "image src-over..." -> output image
I've tried to use 'extent' already to enforce the size, but it was _always_ enforcing that size, not just when the image was smaller than 650x420, despite I've added the "<" to the end of the geometry setting.
My current command:
Code: Select all
mogrify -print "%f -> %i [medium]\n" -trim -fuzz "20%" -background "white" -resize 650x650 -gravity center -rotate "90<" -gravity center -extent "650x420<" -draw "image src-over 0,0 0,0 'Watermarks\watermark_m.png'" -path "TestTmp\m" "Source\Original\*.jpg"
I'm very close to what we need to achieve here, just can't seem to be able to overcome this last hurdle...
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T11:08:04-07:00
by fmw42
I think you are going to have to post an example set of input and output images and explain what is wrong and what you need. We need to see the problem to understand better. You can upload images to any free hosting service that does not change the image or format, such as dropbox.com, and put the URL here.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T14:17:41-07:00
by Mihamix
fmw42 wrote: ↑2018-02-03T11:08:04-07:00
I think you are going to have to post an example set of input and output images and explain what is wrong and what you need. We need to see the problem to understand better. You can upload images to any free hosting service that does not change the image or format, such as dropbox.com, and put the URL here.
Oh, I didn't think that was an option - many thanks for offering!
Here is the link, there are two folders, original and converted. Included one image (
465153-5004S@04.jpg) with correct look, the other 4 images are examples for the problem.
Dropbox folder
Many thanks in advance for looking into my issue!
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T15:46:05-07:00
by fmw42
Can you provide the watermark image?
How do you want it to fit to the size of the background. That is, what do you want to happen when the aspect ratio of the watermark image is different from that of the background image? Should it distort to fit exactly to the background size, enlarge so that max dimension fits the background and the smaller dimension will be padded (transparent?), or enlarged so that the smaller dimension fits the background and then center cropped to the size of the background image? From your examples it looks like you want it to enlarge to fit the maximum dimension (or perhaps only the width) and padded with transparency.
When I get your watermark, I can test it. But try the following and let me know if that does what you want:
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" -set option:size "%wx%h" ( Watermarks\wm.png -resize "%[size]" ) -gravity "center -compose over -composite TestTmp\bn\m\yourimage
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T16:26:45-07:00
by Mihamix
fmw42 wrote: ↑2018-02-03T15:46:05-07:00
Can you provide the watermark image?
How do you want it to fit to the size of the background. That is, what do you want to happen when the aspect ratio of the watermark image is different from that of the background image? Should it distort to fit exactly to the background size, enlarge so that max dimension fits the background and the smaller dimension will be padded (transparent?), or enlarged so that the smaller dimension fits the background and then center cropped to the size of the background image? From your examples it looks like you want it to enlarge to fit the maximum dimension (or perhaps only the width) and padded with transparency.
When I get your watermark, I can test it. But try the following and let me know if that does what you want:
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" -set option:size "%wx%h" ( Watermarks\wm.png -resize "%[size]" ) -gravity "center -compose over -composite TestTmp\bn\m\yourimage
The way I have to present is the images which are too low (height isn't big enough) needs to be changed to have a white border on the top and the bottom to "pad" the image up to the required height, so that the watermark could fit.
Tried the command on one image, got this error message:
Code: Select all
magick: invalid argument for option '-resize' '356864B' at CLI arg 18 @ error/operation.c/CLISimpleOperatorImage/3171.
Uploaded the watermark image to the previous shared folder.
Many thanks for your help, I really do appreciate!
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-03T17:55:56-07:00
by fmw42
Is your output on another volume?
Did you post your watermark image so I can use it to test. If I pad with white, then it will cover your background image. So I pad with transparency
Try this
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -set option:dim "%wx%h" -resize "%[dim]" -rotate "90<" ( Watermarks\wm.png -resize "%[dim]" -background none -gravity center -extent "%[dim]" ) -gravity center -compose over -composite TestTmp\bn\m\yourimage
or
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" ( Watermarks\wm.png -resize "650x650" -background none -gravity center -extent "650x650" ) -gravity center -compose over -composite TestTmp\bn\m\yourimage
Does the code work if you have your input images and output images in the same directory?
Are you running the command in bat file? If so, some changes might be needed such as some % to %%. Sorry I am not a Windows user.
Again if you post your watermark image, I can test the equivalent command in unix.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T00:50:42-07:00
by Mihamix
fmw42 wrote: ↑2018-02-03T17:55:56-07:00
Is your output on another volume?
Did you post your watermark image so I can use it to test. If I pad with white, then it will cover your background image. So I pad with transparency
Again if you post your watermark image, I can test the equivalent command in unix.
Sorry, I've uploaded it, but didn't share... Thought if it is in a folder I already shared, it should be visible...
Anyway, here is the
watermark image.
Many thanks
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T01:05:18-07:00
by Mihamix
fmw42 wrote: ↑2018-02-03T17:55:56-07:00
Try this
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -set option:dim "%wx%h" -resize "%[dim]" -rotate "90<" ( Watermarks\wm.png -resize "%[dim]" -background none -gravity center -extent "%[dim]" ) -gravity center -compose over -composite TestTmp\bn\m\yourimage
or
Code: Select all
magick Source\Original\yourimage -print "%f\n" -trim -fuzz "10%" -background "white" -resize 650x650 -rotate "90<" ( Watermarks\wm.png -resize "650x650" -background none -gravity center -extent "650x650" ) -gravity center -compose over -composite TestTmp\bn\m\yourimage
Tried both.
First resulted in an image with dimensions 4171 x 2222 (but otherwise generally looked OK)
Second resulted in a truncated image, very much similar to the one I've initially posted (dimensions were 650 x 346)
fmw42 wrote: ↑2018-02-03T17:55:56-07:00
Does the code work if you have your input images and output images in the same directory?
Are you running the command in bat file? If so, some changes might be needed such as some % to %%. Sorry I am not a Windows user.
Again if you post your watermark image, I can test the equivalent command in unix.
I have a folder structure set, and the input and output files are in different folders - the image preprocessing is just a beginning for another process, which'll then publish the files.
Command _will_ be in bat file, but for now I'm testing it in plain old command line.
Files folder is on network share, hence why I'd prefer the mogrify command, as to my understanding, that only reads the watermark image once and uses that on several files.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T11:21:51-07:00
by fmw42
Mogrify will not allow parenthesis processing nor -set option: arguments. So you cannot do inline computations.
One last time, please upload your watermark image. I cannot test further without it.
I tested with some other image and it appeared to work for me.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T12:03:54-07:00
by Mihamix
Mihamix wrote: ↑2018-02-04T00:50:42-07:00
fmw42 wrote: ↑2018-02-03T17:55:56-07:00
Is your output on another volume?
Did you post your watermark image so I can use it to test. If I pad with white, then it will cover your background image. So I pad with transparency
Again if you post your watermark image, I can test the equivalent command in unix.
Sorry, I've uploaded it, but didn't share... Thought if it is in a folder I already shared, it should be visible...
Anyway, here is the
watermark image.
Many thanks
Is there any problem accessing the watermark image I uploaded
above? It works for me testing with a different account, I can open the file...
Here is the link again:
watermark image.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T12:25:17-07:00
by fmw42
Sorry, I did not see that you added the link to the watermark. My apologies. I will test with it.
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-04T13:08:49-07:00
by fmw42
You have several issues. First the -fuzz needs to come before -trim. Then you need +repage after -trim. But the main issue is that your watermark image does always have an appropriate aspect ratio to conform to the trimmed background image. If you need it to fill the width as well, then the resize for the watermark must distort the result.
Here is my command in unix syntax.
Code: Select all
magick /Users/fred/desktop/PZImages/original/17201-33020@02.jpg -print "%f\n" \
-fuzz "10%" -trim +repage -background "white" \
-resize "650x650" -rotate "90<" -set option:dim "%wx%h" \
\( /Users/fred/desktop/PZImages/WM_m.png -resize "%[dim]" \
-gravity center -background none -extent "%[dim]" \) \
-gravity center -compose over -composite /Users/fred/desktop/PZImages/fred_results/17201-33020@02.jpg
Here are my results:
In Windows syntax it would be something like the following. You need to provide your paths
Code: Select all
magick path2originals\17201-33020@02.jpg -print "%f\n" -fuzz "10%" -trim +repage -background "white" ^
-resize "650x650" -rotate "90<" -set option:dim "%wx%h" ^
( path2towatermark\WM_m.png -resize "%[dim]" -gravity center -background none -extent "%[dim]" ) ^
-gravity center -compose over -composite path2output\17201-33020@02.jpg
Re: Making sure image has at least given dimensions after resize
Posted: 2018-02-06T10:27:26-07:00
by Mihamix
fmw42 wrote: ↑2018-02-04T13:08:49-07:00
You have several issues. First the -fuzz needs to come before -trim. Then you need +repage after -trim. But the main issue is that your watermark image does always have an appropriate aspect ratio to conform to the trimmed background image. If you need it to fill the width as well, then the resize for the watermark must distort the result.
Thanks a lot for the above, I'm fixing the command line...
I think however, that there is still some misunderstanding between us
What I actually have to achieve is like this:
As I've stated above in the thread:
Mihamix wrote: ↑2018-02-03T16:26:45-07:00
The way I have to present is the images which are too low (height isn't big enough) needs to be changed to have a white border on the top and the bottom to "pad" the image up to the required height, so that the watermark could fit.
Can you help me what would I have to do to get that?
Kind regards,
Attila