How to plot an image from a series of bits inPython?
-
11-12-2020 - |
Question
I am given a dataset of 0s and 1s for some handwritten letters (below is a series for one image) and i'd like to visualize them in python. How can this be achieved?
'000000000000001010000000000000000000000000000111110000000000000000000000000001111000000000000000000000000000111100000000000000000000000000001111000000000000000000000000000111110000000000000000000000000011111100000000000000000000000000111111100000000000000000000000001111111000000000000000000000000011111110000000000000000000000000111111110000000000000000000000011111111100000000000000000000000111111111100000000000000000000001111111111000000000000000000000011111111110000000000000000000001111111111110000000000000000000011111111111100000000000000000000111111011111100000000000000000001111100111110000000000000000000111111000111110000000000000000001111100001111110000000000000000011111000001111100000000000000001111110000011111100000000000000011111000000111111100000000000000111110000000111111000000000000011111100000001111110000000000000111110000000001111110000000000001111100000000011111100000000000111111000000000011111100000000001111110000000000111111110000000011111100000001111111111111100000111111111111111111111111111100011111111111111111111111111111000111111111111111111111111111100001111111111111110000011111100000011111100000000000000111111000000111110000000000000001111110000011111100000000000000001111110000111110000000000000000011111100001111100000000000000000011111000111111000000000000000000111110001111110000000000000000001111110011111100000000000000000011111100111110000000000000000000011111001111100000000000000000000111110011111000000000000000000001111101111110000000000000000000011111111111100000000000000000000011111111110000000000000000000000111111111100000000000000000000001111111110000000000000000000000001110'
do i need to take it octet by octet to get a pixel value?
Solution
You could use at least these two options:
PIL library:
from PIL import Image img = Image.new('1', (width, height)) #width and height of the original image data=[int(x) for x in x] #x being your array img.putdata(data) img.save('my_handwritten_number.png') img.show()
You can see here an example with the whole process, from reading a hand written image file through converting it to an array and displaying it again to an image:
import numpy as np
np_img=np_img.reshape(28900, )
width = np.sqrt(len(np_img))
from PIL import Image
img = Image.new('1', (int(width), int(width)))
data=[int(np_img) for np_img in np_img]
img.putdata(data)
img.save('my_hand_written_digit.png')
img.show()
getting as a result this reconstructed digit:
nevertheless, I see your array is a 1D array whose square root is not an integer, so it seems it does not come from an square image; you need to be able to convert it to a matrix (via the Image.new method). If I do it with your array, this is what I get:
another option you have is with Keras:
import tensorflow.keras as keras keras.preprocessing.image.array_to_img(np_img.reshape(170, 170, 1), data_format=None, scale=True, dtype=None)