Zooming to a smaller file 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
eldiener

Zooming to a smaller file size

Post by eldiener »

I need to take a .jpg image and zoom it to a smaller file size of xxxK bytes ( or a little less ) programatically using Magick++. My algorithm for doing this was to take the number of row and column pixels in the original image as a total, use the ratio of the larger file size to my xxxK size to calculate a new total number of pixels for the new image, and then calculate the new row and column pixels based on the new total number while keeping the rows and columns in the same proportion as the original image so that no distortion would occur. I then used Image::scale to scale down the image to the new row and column values.

While all of this works as far as scaling down the image properly to the new row and column values and keeping the image proportionally correct, the larger the original image, the further off I am from getting my smaller file size value, with the new file size being larger in kilobytes than my xxxK value.

So evidently merely reducing the row and column pixels using the correct ratio does not account for the total file size of the image in permanent storage.

Does anybody know what other factors I need to take into account in order to scale down to the correct file size while keeping the pixel proportions correct ?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Zooming to a smaller file size

Post by anthony »

Final file size is dependant on depth, quality, image profiles, format, internal format (or type), number of colors, compression factors, etc etc etc ..

Often a major difference is either profiles, or quality settings.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
eldiener

Re: Zooming to a smaller file size

Post by eldiener »

anthony wrote:Final file size is dependant on depth, quality, image profiles, format, internal format (or type), number of colors, compression factors, etc etc etc ..

Often a major difference is either profiles, or quality settings.
For a JPEG image is there any known algorithm for reducing the file size to a maximum amount while keeping the ratio of row and column pixels the same so as not to produce any distortion in the new image ?

I am given the task of reducing an JPEG image of XXX file size to a file size of at maximum YYY if XXX is larger than YYY.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Zooming to a smaller file size

Post by anthony »

There is no algorithm as the result is such a complex interaction.
Even just halving the number of pixels would not be linear as complexity of image data then grows causing the compression to not work as well as it does initially.
And profiles can make things so much worse as they don't reduce in size (for thumbnails they are usally stripped).

Generally when an image becomes less than 100 pixels, a image format change is probably the better solution, generally to PNG or GIF.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
eldiener

Re: Zooming to a smaller file size

Post by eldiener »

anthony wrote:There is no algorithm as the result is such a complex interaction.
Even just halving the number of pixels would not be linear as complexity of image data then grows causing the compression to not work as well as it does initially.
And profiles can make things so much worse as they don't reduce in size (for thumbnails they are usally stripped).

Generally when an image becomes less than 100 pixels, a image format change is probably the better solution, generally to PNG or GIF.
Thanks ! I guess I will have to do it by trial and error, using a binary reduction algorithm to approximate the maximum size I want.
Post Reply