1

I have a 2D array and I need to save it as an image. What's the best way to do it without rescaling? I want to read the image afterwards and check that the values have been saved correctly. I am saving it as a bmp so to avoid compression issues, but other formats should also be fine.

albus_c
  • 6,292
  • 14
  • 36
  • 77

1 Answers1

1

To save an image you can use SciPys imsave function.

imsave(path, image)

EDIT: To Save an image as bmp just choose the file extension in path accordingly.

EDIT2: To prevent intensity normalization you can use

scipy.toimage(image, cmin=0, cmax=255, mode='I').save("image.png")

You can use mode'I'to save your image in a specific format. Just be sure that your input is of type uint16.

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
  • `imsave(path, image)` does not work; I am using `scipy.misc.imsave(path, image)` instead. When I compare data obtained reading the image with the original data, I have that the do not coincide... are you aware of any saving procedure that could explain that? – albus_c Nov 04 '13 at 09:05
  • Do you save your image as a bmp? In what way do they differ? – Mailerdaimon Nov 04 '13 at 09:07
  • It seems like scipy does some range normalization. You can use `scipy.toimage(image, cmin=0, cmax=255).save("image.png")` to prevent that – Mailerdaimon Nov 04 '13 at 09:09
  • In this way the maximum value is rescaled to 255, right? What if I want to save values higher than that without rescaling? – albus_c Nov 04 '13 at 09:10
  • Just choose the scales according to your needs. – Mailerdaimon Nov 04 '13 at 09:16
  • Perfect. And is there a way to save in 16 bits? Something like `scipy.toimage(image, cmin=0, cmax=255).save("image.png").astype(np.uint16)` – albus_c Nov 04 '13 at 09:17
  • I edited my answer. You can use `mode='I'` in `scipy.toimage` to save a `uint16` Matrix as 16 Bit image. – Mailerdaimon Nov 04 '13 at 09:24
  • 1
    Well, this answer is from 2013 and still valid for older scipy Versions so I will not change it. For replacement in new Versions see this question: https://stackoverflow.com/q/57545125/2927205 – Mailerdaimon Nov 20 '20 at 19:56