Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a black and white image to array of numbers?

The image is 28 pixels by 28 pixels. They can interpret this as a big array of numbers: Like the image above suggests, how can I convert the image to the left into an array that represent the darkness of the image between 0 for white and decimals for darker colours closer to 1? as shown in the image usingpython 3`?

Update: I have tried to work abit more on this. There are good answers below too.

# Load image 
filename = tf.constant("one.png")
image_file = tf.read_file(filename)

# Show Image
Image("one.png")

#convert method
def convertRgbToWeight(rgbArray):
    arrayWithPixelWeight = []
    for i in range(int(rgbArray.size / rgbArray[0].size)):
        for j in range(int(rgbArray[0].size / 3)):
            lum = 255-((rgbArray[i][j][0]+rgbArray[i][j][1]+rgbArray[i][j][2])/3) # Reversed luminosity
            arrayWithPixelWeight.append(lum/255) # Map values from range 0-255 to 0-1

    return arrayWithPixelWeight



# Convert image to numbers and print them
image_decoded_png = tf.image.decode_png(image_file,channels=3)
image_as_float32 = tf.cast(image_decoded_png, tf.float32)

numpy.set_printoptions(threshold=numpy.nan)
sess = tf.Session()
squeezedArray = sess.run(image_as_float32)

convertedList = convertRgbToWeight(squeezedArray)

print(convertedList) # This will give me an array of numbers. 
like image 739
SamTheGoodOne Avatar asked Jul 06 '18 07:07

SamTheGoodOne


2 Answers

I would recommend to read in images with opencv. The biggest advantage of opencv is that it supports multiple image formats and it automatically transforms the image into a numpy array. For example:

import cv2
import numpy as np

img_path = '/YOUR/PATH/IMAGE.png'
img = cv2.imread(img_path, 0) # read image as grayscale. Set second parameter to 1 if rgb is required 

Now img is a numpy array with values between 0 - 255. By default 0 equals black and 255 equals white. To change this you can use the opencv built in function bitwise_not:

img_reverted= cv2.bitwise_not(img)

We can now scale the array with:

new_img = img_reverted / 255.0  // now all values are ranging from 0 to 1, where white equlas 0.0 and black equals 1.0 
like image 92
Tim Avatar answered Sep 17 '22 07:09

Tim


Load the image and then just invert and divide by 255.

Here is the image ('Untitled.png') that I used for this example: https://ufile.io/h8ncw

import numpy as np
import cv2
import matplotlib.pyplot as plt

my_img = cv2.imread('Untitled.png') 
inverted_img = (255.0 - my_img)  
final = inverted_img / 255.0

# Visualize the result
plt.imshow(final)
plt.show()

print(final.shape)
(661, 667, 3)

Results (final object represented as image):

final image

like image 43
seralouk Avatar answered Sep 18 '22 07:09

seralouk