3

I am loading an image using skimage.io.imread and saving the image as numpy array. Following is the inferance:

Original image: (256x512x3)

enter image description here

Following is the code that I execute:

img = io.imread(img_file) # 48.1 kB
i1, i2 = img[:, :256], img[:, 256:]
np.save('i1', i1) # 196.7 kB
np.save('i2', i2) # 196.7 kB
final_image = np.empty([1, 2, 256, 256, 3])
final_image[0, 0], final_image[0, 1] = i1, i2
np.save('final', final_image) # 3.1 MB

Can anyone explain why such a huge difference in the size of the image?

EDIT: dtype of i1, i2, final_image is np.float64

5
  • Well, for starters, final_image is of dtype np.float_, which is likely 8 bytes per item. Commented Dec 19, 2017 at 0:08
  • The dtype of i1 and i2 is np.float_ as well. But I don't see such a surge in the size of the saved array Commented Dec 19, 2017 at 0:10
  • 1
    Um, pretty sure it isn't. It should be defaulting to reading in as np.uint8, at least it does on my system. Commented Dec 19, 2017 at 0:14
  • Can you actually print out final_mage.shape, final_image.dtype Commented Dec 19, 2017 at 0:15
  • Yes, that's right, i1 and i2 have dtype of uint8 whereas final_image has float64 Commented Dec 19, 2017 at 0:18

1 Answer 1

2

numpy.empty will default to whatever np.float_ is on your system, however, your image should have been read in as np.uint8, so provide the corresponding dtype to empty:

final_image = np.empty([1, 2, 256, 256, 3], dtype=np.uint8)
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to solve the problem. Even though specifying dtype to be of uint8, it changes to float64
It's changing at least on my system
Sorry, my bad, I was making changes to the code in a different file and executing another file. I am sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.