A
A
Alexander2020-05-18 17:02:36
Python
Alexander, 2020-05-18 17:02:36

How to convert image to numpy array?

There is a very entertaining video about neural networks:
What is a neural network?
The author on this video shows a picture - this is an array of floating point numbers from 0 to 1:
5ec29401b9589660163149.png
I have such a picture: The
5ec294959c174525410914.png
question is how can I convert it to the same array as the author in the video.
Here is the code

from PIL import Image
import numpy

def converting_image_to_array():
    image = Image.open("image.png")
    array = numpy.array(image)
    print(array)

def main():
    converting_image_to_array()

if __name__ == '__main__':
    main()

I don't know what to do next
PS picture 28x28 array should consist of 784 numbers from 0 to 1 grayscale

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2020-05-18
@sanya84

For example:

import numpy as np
import cv2

...
color_image = cv2.imread(FILE)
gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)

n_image = np.around(np.divide(gray_image, 255.0), decimals=1)


If you don't need to round, then ZY is enough. Didn't watch the video
np.divide(gray_image, 255.0)

A
aRegius, 2020-05-18
@aRegius

Take OpenCV, read the image in Grayscale mode (to get an array of values ​​0-255) and divide by 255. To read in GS mode, simply set the numerical mode value to zero:
grayscale_image = cv2.imread(your_image, 0)

Z
zyivan356, 2020-05-18
@zyivan356

also very interesting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question