-1

Images such as this one (https://i.stack.imgur.com/hLGls.jpg) should be cropped into individual cells. Sadly, the vertical distance is not static, so a more complex method needs to be applied. Since the cells have alternating background colors (grey and white, maybe not visible at low contrast monitors), I thought it might be possible to get the coordinates of the boundaries between white and grey, with which accurate cropping can be accomplished. Is there a way to, e.g., transform the image into a giant two dimensional array, with digits corresponding to the color of the pixel ?... so basically:

enter image description here

Or is there another way?

cheesus
  • 1,111
  • 1
  • 16
  • 44

1 Answers1

1

Here's a snippet that shows how to access the individual pixels of an image. For simplicity, it first converts the image to grayscale and then prints out the first three pixels of each row. It also indicates where the brightness of the first pixel is different from that pixel in that column on the previous row—which you could use to detect the vertical boundaries.

You could do something similar over on the right side to determine where the boundaries are on that side ( you've determined the vertical ones).

from PIL import Image

IMAGE_FILENAME = 'cells.png'
WHITE = 255

img = Image.open(IMAGE_FILENAME).convert('L')  # convert image to 8-bit grayscale
WIDTH, HEIGHT = img.size

data = list(img.getdata()) # convert image data to a list of integers
# convert that to a 2D list (list of lists of integers)
data = [data[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]

prev_pixel = WHITE
for i, row in enumerate(range(HEIGHT)):
    possible_boundary = ' boundary?' if data[row][0] != prev_pixel else ''
    print(f'row {i:5,d}: {data[row][:3]}{possible_boundary}')
    prev_pixel = data[row][0]
martineau
  • 119,623
  • 25
  • 170
  • 301