Hello.
I have searched the boards, read the FAQ and the guides, but am still unable to locate an answer to what I thought was a simple problem:
I have a number of jpg and png files that I need to somehow resize in such a way that they can fit within a file size constraint of 256 KB (final file size must be <256.000 bytes, to be on the safe side). The files are of various sizes and bit depths (from various sources).
Additionally:
* While I would like to have them always be below 256 KB in size, I would also like to preserve as much quality as possible.
* The image file type must remain the same (jpg must remain jpg, png must remain png), because the files will be referenced by their original names.
* I would like to remove any excess bit depth that cannot be displayed on screen, as the final images will be used exclusively for web.
* Original aspect ratio should be preserved.
The only way I have found so far is to resize all the images to a very tiny, nearly thumbnail, size. Because if I relax the constraint even a little bit, a few of the images end up being a bit larger than the 256 KB.
Is this possible with greater knowledge of either imagemagick or picture formats than mine? I suspect it should be possible to define a "perfect format" that will enforce all of this, but I have nowhere near the know-how to find it.
Resizing images within a specific file size constraint
Re: Resizing images within a specific file size constraint
See http://www.imagemagick.org/script/comma ... php#define for jpg file size but I think there is ony an option for jpg and depends on your IM version.
Fred has a script to do a similar thing: http://www.fmwconcepts.com/imagemagick/ ... /index.php
I would use -thumbnail for resizing as it automaticaly strips the EXIF data: http://www.imagemagick.org/script/comma ... thumbnail/ the resize options are shown on the Image geometry link.
Depth, quality etc. can also be set.
Fred has a script to do a similar thing: http://www.fmwconcepts.com/imagemagick/ ... /index.php
I would use -thumbnail for resizing as it automaticaly strips the EXIF data: http://www.imagemagick.org/script/comma ... thumbnail/ the resize options are shown on the Image geometry link.
Depth, quality etc. can also be set.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Resizing images within a specific file size constraint
For PNG. the format is normally non-lossy. As such the best idea is to just try and make PNG as small as posible.
You at least a -quality setting of 9? The '9' is compression option. the '? can be variable and you can get a smaller image size with different values.
See http://www.imagemagick.org/Usage/formats/#png
Better still just save your PNG and use "optipng" to find the 'smallest non-lossy PNG.
PNG will not compress as much as a lossy JPG, converting to JPG will compress it more than trying to preserve PNG status.
However if you are willing to to sacrifice colors you can try PNG8:{filename} which will cut down the number of colors in much the same way that GIF does. There are lots of methods and techniques for this color reduction. Different amounts, different dithers, and that is on top of the normal PNG quality control!
You may also like to try "pngquant" which also does similar color reduction to make an PNG file smaller (with loss of quality).
You at least a -quality setting of 9? The '9' is compression option. the '? can be variable and you can get a smaller image size with different values.
See http://www.imagemagick.org/Usage/formats/#png
Better still just save your PNG and use "optipng" to find the 'smallest non-lossy PNG.
PNG will not compress as much as a lossy JPG, converting to JPG will compress it more than trying to preserve PNG status.
However if you are willing to to sacrifice colors you can try PNG8:{filename} which will cut down the number of colors in much the same way that GIF does. There are lots of methods and techniques for this color reduction. Different amounts, different dithers, and that is on top of the normal PNG quality control!
You may also like to try "pngquant" which also does similar color reduction to make an PNG file smaller (with loss of quality).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Resizing images within a specific file size constraint
Thank you both for your help. The "downsize" script fits my purposes very nicely, although I did have some initial difficulties because the script is incompatible with the version of ImageMagick that is currently in the CentOS rpm repository.
Once I downloaded and installed the lastest rpm manually, however, the script worked as expected.
Once I downloaded and installed the lastest rpm manually, however, the script worked as expected.