0

I'm doing something really simple: using the io function in skimage to read in an image, and then using the np.save functionality to save the array. Something really weird happens: the original image is only of 2mb, while the saved numpy ndarray representation of it takes up 28 mb. Does anyone have any idea why this would be the case?

Jack Shi
  • 131
  • 5
  • There could be one of several things happening, but without details (like the code), we can just speculate. Two ideas: 1) What are the input / output formats (i.e. is the original compressed, and the re-saved uncompressed?) 2) Are the bit depths the same? – bogovicj Oct 01 '19 at 00:24
  • if the image is stored in a lossy format like JPEG, this is entirely expected. Even PNG is specifically geared towards storing images, so may do better than np.save. Try np.savez. – Stefan van der Walt Oct 01 '19 at 01:22
  • @StefanvanderWalt Thanks for the advice. I have indeed tried np.savez and it managed to get the image size down to 16 MB, but that is still 8 times larger than the original size. By any chance you know why this is the case? I have to crop each image into patches and save them in a list. That's why I can't really use PNG. – Jack Shi Oct 01 '19 at 02:00
  • Sorry, I don't quite understand why that impacts saving into PNG? You can easily map a series of PNGs into an array, even with delayed processing using dask. – Stefan van der Walt Oct 01 '19 at 03:00
  • Unless you take special steps, your `numpy` array will use 8 byte numbers (integer or float 64). The image might be smaller, like 2. `np.save` is saving a bytecopy of the arrays data buffer. `arr.nbytes` should I think be similar to the `save` file size. What's the `shape` of the array? – hpaulj Oct 01 '19 at 03:19
  • @hpaulj The shape of the array is [1000,108,108,4]. Sorry, what do you mean by arr.nbytes should I think be similar to the save file size? – Jack Shi Oct 01 '19 at 14:25
  • Is that 1000 patches of shape (108,108) with 4 channels? At 8 bytes per element I expect that to consume 373Mb. – hpaulj Oct 01 '19 at 17:02

0 Answers0