0

I have some python code that outputs many large images that need to be in 16-bit format. I want them to be:

1) View-able using photo viewing software (eg. Xnview is ok but not ImageJ as its GUI is too tedious).

2) File size needs to be small.

Due to requirement 1), I'm considering outputting the images as jp2 or png. Currently I'm using using OpenCV to save these 16 bit images. However the files seem to be significantly larger than a 8 bit jpg of the same resolution. A file size comparison is as follows:

9827 x 7900 pixels RGB image:

  • 8 bit jpg: 3.77mb
  • 8 bit jp2: 92mb
  • 16 bit png: 266mb
  • 16 bit jp2: 308mb

I'm wondering whether it is possible to save compressed .jp2 files using Python? If not, are there any other ways of saving 16 bit images so that they are not too much larger than x2 the size of a jpg image? I can accept lossy compression and other programming languages.

matohak
  • 535
  • 4
  • 19
  • Take a look here, may be toy will find some ideas: https://stackoverflow.com/questions/43489810/python-open-jp2-medical-images-scipy-glymur – Viktor Ilienko Jun 04 '19 at 16:29
  • Generally compression is difficult with 16bit images, as a lot of the lower bits tend to be mostly noise in practice. Also note that when you use lossy compression, you may loose most of the extra information that 16bit gives you. – Richard K. Wade Jun 04 '19 at 16:33
  • 2
    I feel that I might simply not be using opencv correctly and there must be some options to compress the image further. Why would a 8-bit jp2 file be so much larger than a 8-bit jpg file? – matohak Jun 04 '19 at 16:47
  • If you're willing to accept lossy compression, why not start by throwing away the lower 8 bits? – Cris Luengo Jun 04 '19 at 17:03
  • just happening to be dealing with some images where dynamic range is important but not spatial information. – matohak Jun 04 '19 at 17:12
  • You can use Pillow: https://pillow.readthedocs.io/en/5.1.x/handbook/image-file-formats.html – Masoud Jun 04 '19 at 22:21
  • Show the exact code used to write those images. E.g. if you're writing PNGs using `imwrite`, and you don't explicitly specify the compression level, it will by default use level 1, which is the worst there is (beyond not compressing at all). The output files will therefore be rather large.. – Dan Mašek Jun 05 '19 at 12:25
  • `opts.compression = [cv2.IMWRITE_PNG_COMPRESSION, opts.compression] if opts.ext.lower() == "png" else ([cv2.IMWRITE_JPEG_QUALITY, opts.compression] if opts.ext.lower() in ['jp2', 'jpeg', 'jpg'] else None)` `cv2.imwrite(str(savepath), img, opts.compression)` I've tried a range of values for compression level and format but the size only varied very little. – matohak Jun 05 '19 at 12:57

0 Answers0