2

How can I convert an image to an array of float numbers? img = cv2.imread('img.png')

and now convert img to float so I get for print(img[0,0]) something like "[ 4.0 2.0 0.0] instead of [4 2 0]

Do you have an idea? Thank you very much!

2 Answers 2

3

You can convert the list of the integers to list of floats as [float(i) for i in values] with list comprehension.

An other option is to convert the img variable as numpy.ndarray to an other numpy.ndarray which contains float values:

img = img.astype(float)

After this assignment the results will contain float values.

Sign up to request clarification or add additional context in comments.

2 Comments

This answer needs an update. This is no longer working in Python 3
This works for me with Python 3. You might have to do img = img.astype(float)/255 if you want to convert integer values from 0 to 255 to a float in the range 0 to 1.
3

You can also use Skimage's img_to_float() function.

image = io.imread('imagefilepath')
image = skimage.img_as_float(image)

Comments

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.