converting with max size

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mopi42
Posts: 5
Joined: 2016-02-08T02:15:48-07:00
Authentication code: 1151

converting with max size

Post by mopi42 »

Hey,
Sorry for my english.
Actually, I'm using 2 command line to resize images (jpg to jpg or png to jpg depend of original type).

Code: Select all

mogrify -resize 50% /home/repository_destination *.jpg
or

Code: Select all

convert -background white -flatten -resize 50% img_src.png img_dest.jpg
1) For the second command line, how to convert a lot of .png file top .jpg 50% like the first command line ?
2) Is it possible to reduce size not with 50% but with a max size like 250ko (IMPORTANT : If origin size is > 250ko for example 5Mo, the new image must be 250ko but if origin size is < 250ko for example 200ko, the new image must be the same 200ko

This is for a website, I need to have max 250ko image (quick loading) but if origin Image is small I don't want to resize it to stay beautifull :)
And if it's possible to have just one command for png and jpg I take it :D
Thank's a lot !!!
Last edited by mopi42 on 2016-02-08T12:35:12-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: converting with max size

Post by snibgo »

To reduce the number of pixels to make the output less than X bytes needs a script. You can directly reduce the quality so the output is less than X bytes:

Code: Select all

mogrify -background White -alpha remove -format JPG -define jpeg:extent=1KB *.png
See http://www.imagemagick.org/script/comma ... php#define etc.
snibgo's IM pages: im.snibgo.com
mopi42
Posts: 5
Joined: 2016-02-08T02:15:48-07:00
Authentication code: 1151

Re: converting with max size

Post by mopi42 »

OK thank's but is there any way to also resize image (because I would like maximum 1500*1500)
So something like -maxsize 1500*1500 (and keep ratio so for example make an image 1500*1354)
But if the picture is less than 1500*1500 don't resize it.
So first step is resize near from 1500*1500 and second step is extend=250Ko (to have an image less than 250Ko).
Thank's a lot !
mopi42
Posts: 5
Joined: 2016-02-08T02:15:48-07:00
Authentication code: 1151

Re: converting with max size

Post by mopi42 »

I found this:

Code: Select all

-define jpeg:size=128x128
But I would like to keep ratio ..
Last edited by mopi42 on 2016-02-08T12:35:57-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: converting with max size

Post by snibgo »

For the first step, use ">", eg:

Code: Select all

-resize 1500x1500>
See http://www.imagemagick.org/script/comma ... p#geometry
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting with max size

Post by fmw42 »

If you are on a relatively current IM version then -define jpeg:size has been change to -define jpeg:extent. See http://www.imagemagick.org/Usage/formats/#jpg_write

For the ">", see http://www.imagemagick.org/script/comma ... p#geometry
mopi42
Posts: 5
Joined: 2016-02-08T02:15:48-07:00
Authentication code: 1151

Re: converting with max size

Post by mopi42 »

Thank's for your help.
I used :
for jpg images :

Code: Select all

mogrify -resize 1500x1500> -path /home/result *.jpg
and for png images :

Code: Select all

mogrify -background White -alpha remove -format JPG -resize 1500x1500> -path /home/result *.png
But 2 problems :
1) The path /home/result (destination) doesn't work and the original image changed !
2) All of images are setting to 1500 (for witdh or height depending on higher value) even if original image is 300*200 (so I don't want to resize it else it's not very beautifull I prefer to keep it small)
I don't know if my english is clear sorry if you don't understand.
Thank's again for your help
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: converting with max size

Post by snibgo »

In most shells, ">" is a special character that redirects output. So you need to escape it, with ^ in Windows or \ in bash.

Code: Select all

-resize 1500x1500^>
-resize 1500x1500\>
snibgo's IM pages: im.snibgo.com
mopi42
Posts: 5
Joined: 2016-02-08T02:15:48-07:00
Authentication code: 1151

Re: converting with max size

Post by mopi42 »

Yes it works thank you very much ! You have an answer for all questions thank you !
Post Reply