1

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")

enter image description here

Step 2: Greyscale the image:

grayImage = srcImage.convert('L')

enter image description here

Step 3: Binarize the image:

binarizedImage = grayImage.point(lambda x: 0 if x<128 else 255, '1')

enter image description here

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")
Olimjon
  • 143
  • 9
  • You can transform your image from a `PIL.Image` to a `numpy.array` using `numpy.array(image)` and then applying your lambda using [apply_over_axes](https://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_over_axes.html) (If you want to retransform it into an image, [here's a nice way to do it](https://stackoverflow.com/a/10967471/8583681)) (If this helped you, i can make an answer to the post out of this comment) – Nenri Mar 14 '19 at 16:01
  • Thanks, I will check this to ensure if this works. I will be glad if you posted it as answer, so that I can accept to make it easier for others to find the solution for similar conditions. – Olimjon Mar 14 '19 at 16:06

1 Answers1

1

You can use numpy library for this

from PIL import Image
from scipy.ndimage import zoom
import numpy as np
srcImage = Image.open("src.jpg")
grayImage = col.convert('L')
array = np.array(grayImage)
array = zoom(array, 310/174)
np.savetxt("binarized.txt", array<128, fmt="%d")

there np.array convert PIL Image to numpy array format, zoom interpolate array with given scale, array < 128 create binary array and fmt="%d" set that result will be saved as integer

Grzegorz Bokota
  • 1,341
  • 7
  • 14
  • you can add the `Image.fromarray()` solution if he wants an image instead of a `.txt` – Nenri Mar 14 '19 at 16:08
  • But method to get binary image is in question. – Grzegorz Bokota Mar 14 '19 at 16:10
  • +1, it works, but white points are indicated as 1, whereas black are 0. I can complement them anyway. What about the second part - saving image as larger 0 and 1 image? – Olimjon Mar 14 '19 at 16:14
  • I forgot about interpolation. I edited response and add this steep. You should provide valid scalar. Also reverse `array > 128` to `array < 128` (I`m not sure if you need ` – Grzegorz Bokota Mar 14 '19 at 16:21
  • Thanks a lot, now there is no need to complement. :) – Olimjon Mar 14 '19 at 16:30
  • OK, I found a solution to convert text to image file here: https://stackoverflow.com/questions/29760402/converting-a-txt-file-to-an-image-in-python – Olimjon Mar 14 '19 at 16:36