I need to convert an image to text or image that consists of only 0 and 1. Is there any way to do this programmatically, preferably on Python?
Here is my try:
Step 1: Open the image:
from PIL import Image
srcImage = Image.open("src.jpg")
Step 2: Greyscale the image:
grayImage = srcImage.convert('L')
Step 3: Binarize the image:
binarizedImage = grayImage.point(lambda x: 0 if x<128 else 255, '1')
Now, I am stuck converting black points to 1, and white points to 0 and save this to text file with image height converted to lines (in this example: 174 pixels to 174 lines) and image width converted to text length (in this example: 310 pixels to 310 character length) or larger image with 0 instead of white points and 1 instead of black points.
Solution to both cases would have been appreciated a lot.
Full binarization code (modified version of the PIL way of binarizing):
from PIL import Image
srcImage = Image.open("src.jpg")
grayImage = srcImage.convert('L')
binarizedImage = grayImage.point(lambda x: 0 if x<128 else 255, '1')
binarizedImage.save("binarized.png")