Skip to content

Instantly share code, notes, and snippets.

@zeratax
Last active May 3, 2017 17:49
Show Gist options
  • Save zeratax/d82060a5944ff9a9761ac852a2c06f23 to your computer and use it in GitHub Desktop.
Save zeratax/d82060a5944ff9a9761ac852a2c06f23 to your computer and use it in GitHub Desktop.
Converts a line in a bitmap to 2d coordinates
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
# Edit these values
file = "quecksilber_graphonly.BMP"
line_color = np.array([0, 192, 192])
# colors in rgb
black = np.array([0, 0, 0])
white = np.array([255, 255, 255])
im = Image.open(file)
pixels = np.array(im)
last = np.array([0, 0])
with open(file + '.csv', 'w') as f:
for y, rows in enumerate(pixels):
for x, row_pixel in enumerate(rows):
# checks wheter the pixels are part of the line
# and not to close to eachother
if not np.array_equal(row_pixel, line_color) or np.allclose(last, np.array([x, y]), 0.005):
pixels[y][x] = white
else:
pixels[y][x] = black
last = np.array([x, y])
f.write("{},{}\n".format(x, y))
plt.imshow(pixels, interpolation='nearest')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment