Answer the question
In order to leave comments, you need to log in
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:
I have such a picture: The
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()
Answer the question
In order to leave comments, you need to log in
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)
np.divide(gray_image, 255.0)
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question