Here is the code I am currently using:
from PIL import Image
import numpy as np
def save_np_img(np_img, path, name):
"""
To save the image.
:param np_img: numpy_array type image
:param path: string type of the existing path where to save the image
:param name: string type that includes the format (ex:"bob.png")
:return: numpy array
"""
assert isinstance(path, str), 'Path of wrong type! (Must be String)'
assert isinstance(name, str), 'Name of wrong type! (Must be String)'
im = Image.fromarray(np_img)
im.save(path + name)
return np_img
I would like to be able to save images which contain float
values, just like I can currently save int
valued-images.
I get a TypeError
message when I try to save my images which have been transformed through np.divide(img, 255.)
, and thus when trying to save numpy_arrays which contain floats.
(You can suggest other librairies, too.)
NOTE: The values, when not integers between 0 and 255, are floats between 0 and 1. It is crucial for me to remain as lossless as possible. My initial thought was that I could simply use np.multiply(img, 255)
but I wasn't sure if that would lose some precision (nor if it would return integers, actually).
EDIT: Basically, is this method a lossless convertion? That is, if I had a numpy_array of ints
, divided it by 255.
, and then reconverted into ints
, am I losing information? If so, how to avoid that?