Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick resize by percentage and limit

I'm trying to resize various images using ImageMagick.

I have a requirement for this to be done in a single command without any supporting extra language.

I want to resize all images below a certain width to 115% (or the equivalent: resize all images to a certain size while restricting the maximum enlargement to 115% of the original).

I've tried mixing the different options but it doesn't seem to be supported.

Is this even possible?


Update:

I used the distort operator as a possible solution:

convert in.jpg +distort srt "%[fx:(w<1600)?1.15:1] 0" out.jpg

but when distorting the images >1600 pixels it still does an operation and rewrites the image with a slightly different pixel count.

like image 234
cyterdan Avatar asked Oct 19 '22 06:10

cyterdan


1 Answers

It might be easier just to use the existing system's commands along with convert to evaluate the high-level logic.

Example, I'll leverage test utility

test $(identify -format "%[fx:(w<1600)?1:0]" in.jpg) -eq 1 && \
     convert in.jpg -resize 115% out.jpg

In ImageMagick 7, the option -exit will be introduced, so it would be possible to terminate an ImageMagick command if a condition is not meet.

Update

Affine distortion may be a better approach. As the following affine matrix will not alter the image

| 1 0 0 |
| 0 1 0 |
| 0 0 1 |

But the following will resize by 115%

| 1.15 0    0 |
| 0    1.15 0 |
| 0    0    1 |
convert in.jpg +distort AffineProjection \
        '%[fx:(w<1600)?1.15:1],0,0,%[fx:(w<1600)?1.15:1],0,0' \
        +repage out3.jpg
like image 56
emcconville Avatar answered Oct 22 '22 21:10

emcconville