1

I'm using PIL library to compress some images. But i dont see in the docs a form to compress based in desired bits.

For example, if my input image have 1,5MB and i need final image with exacly 100kb, is any way to do that, Independent of quality or library...?

MagicHat
  • 379
  • 1
  • 6
  • 28
  • You can always add extra metadata sections to the file to pad it out after compressing to make it smaller than you need. – Charles Duffy Mar 16 '22 at 16:07
  • _In general_, I wouldn't expect someone writing a compression algorithm to implement this support, because it would be quite a bit of work with limited value. The easiest ways to implement it would be using a progressive encoding and then cutting off the quantization data whenever the desired file size is met, but the easy/naive implementation would leave some parts of the image looking better than others depending on where the cutoff point sits. – Charles Duffy Mar 16 '22 at 16:09
  • Is downscaling the image an option? See https://stackoverflow.com/questions/31082904/downscaling-part-of-image-in-python. Of course you will need to do some math to get your desired final size. – Carlos Horn Mar 16 '22 at 16:13
  • @CharlesDuffy tks for atention, i dont have a deep knowlege about image processing, I suspect it is necessary... I imagined that some library would have... but ok , i'll spend some time on it to try implement something on this goal... – MagicHat Mar 16 '22 at 16:39
  • @CarlosHorn tks for atention, i gona see that tip... – MagicHat Mar 16 '22 at 16:40
  • See Figure 9.24 in the book Practical Signal Processing by Mark Owens to see what I'm talking about (page 208 on the second edition). Once you've divided the image into low-frequency and high-frequency signals enough times, you can start discarding high-frequency data until you get down to the file size you want. – Charles Duffy Mar 16 '22 at 16:56
  • 2
    Not sure what you are trying to do exactly, but you can make a JPEG no larger than a given size like this with Python https://stackoverflow.com/a/52281257/2836621 or like this in Terminal https://stackoverflow.com/a/54713865/2836621 and then pad with comments as suggested by @CharlesDuffy – Mark Setchell Mar 16 '22 at 17:35
  • @MarkSetchell tks for atention, i gona see all tips to solve my target – MagicHat Mar 16 '22 at 18:24
  • 1
    Note that the comment will be introduced by a 2-byte marker and a 2-byte length, so you'll get 4 more bytes than the basic length of the comment. Note that you cannot add more than 64kB of comments. – Mark Setchell Mar 16 '22 at 18:58
  • 1
    Note that `jhead` and `exiftool` are likely to cause less *"collateral damage"* than **ImageMagick** for adding comments without affecting other stuff. – Mark Setchell Mar 16 '22 at 19:00

0 Answers0