0

The title says it all.

I have many large matrices which I want to save as JPG images under the condition, that the size of the image is small enough. Therefore I want to determine or estimate the size of the final JPG image before converting the matrix to an JPG image.

I really would like to prevent converting every matrix to an JPG image and than check the size.

How can I do that in Python?

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
  • Possible duplicate of [Python memory usage of numpy arrays](https://stackoverflow.com/questions/11784329/python-memory-usage-of-numpy-arrays) – G. Anderson Oct 16 '18 at 18:35
  • size of what? matrix dimensions or resulting file size? – Sam Mason Oct 16 '18 at 18:35
  • The size of a JPEG is not easily determined, since it depends of the "information" in the image. For example an image that contains one simple color is very small, an image that contains random color dots, is typically quite large. JPEG uses *Fourrier analysis* to perform *lossy* compression. – Willem Van Onsem Oct 16 '18 at 18:37
  • @SamMason I want to determine / estimate the resulting file size if the matrix were already a .jpg image. – Gilfoyle Oct 16 '18 at 18:51
  • @WillemVanOnsem Are there Python tools that use Fourrier analysis that I can use to estimate the size or the "information" before converting the matrix into an jpg image. – Gilfoyle Oct 16 '18 at 18:52
  • @G.Anderson I don't want to know the dimensions or the number of bytes of the matrix. I am rather interested in the information stored in the matrix. – Gilfoyle Oct 16 '18 at 18:55
  • 3
    @Samuel: fourrier analysis is only one of the many steps: first it makes small blocks of 8x8, then it performs 2d fourrier analysis, then it divides certain components with a certain factor, and then it performs an encoding where zeros are compressed. So it makes it horribly hard to estimate it. I would suggest you simply save it (to a file or in memory), and then take a look what the file size is. Since estimating will require approx. the same amount of work then actually serializing it. – Willem Van Onsem Oct 16 '18 at 18:55
  • 1
    why not just compress the image then? it's probably not going to take much longer, jpeg compression is a pretty heavily optimised operation… empirically the size is strongly dependent (i.e. within 50%) on the number pixels and encoding parameters. it might be enough to just threshold your total pixel count – Sam Mason Oct 16 '18 at 18:58

0 Answers0