I'm trying to create like a bitmap for an image of a letter but I'm not having the desired result. It's been a few days that I started working with images. I tried to read the image, create a numpy array of it and save the content in a file. I wrote the code bellow:
import numpy as np
from skimage import io
from skimage.transform import resize
image = io.imread(image_path, as_grey=True)
image = resize(image, (28, 28), mode='nearest')
array = np.array(image)
np.savetxt("file.txt", array, fmt="%d")
I'm trying to use images like in this link bellow:
I was trying to create an array of 0's and 1's. Where the 0's represent the white pixels and the 1's represent the black pixels. Then when I save the result in a file I can see the letter format.
Can anyone guide me on how to get this result?
Thank you.
Check this one out:
from PIL import Image
import numpy as np
img = Image.open('road.jpg')
ary = np.array(img)
# Split the three channels
r,g,b = np.split(ary,3,axis=2)
r=r.reshape(-1)
g=r.reshape(-1)
b=r.reshape(-1)
# Standard RGB to grayscale
bitmap = list(map(lambda x: 0.299*x[0]+0.587*x[1]+0.114*x[2],
zip(r,g,b)))
bitmap = np.array(bitmap).reshape([ary.shape[0], ary.shape[1]])
bitmap = np.dot((bitmap > 128).astype(float),255)
im = Image.fromarray(bitmap.astype(np.uint8))
im.save('road.bmp')
The program takes an rgb image and converts it in a numpy array. It then splits it in 3 vectors, one for each channel. I uses the color vectors to create a gray vector. After that it comperes elements with 128, if lower than writes 0(black) else is 255. Next step is reshape and save.