Created
December 14, 2018 12:18
-
-
Save NicoLivesey/05203aacd7cfe952e1065058a27a8283 to your computer and use it in GitHub Desktop.
Simple version of deskewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from PIL import Image as im | |
from scipy.ndimage import interpolation as inter | |
input_file = sys.argv[1] | |
img = im.open(input_file) | |
# convert to binary | |
wd, ht = img.size | |
pix = np.array(img.convert('1').getdata(), np.uint8) | |
bin_img = 1 - (pix.reshape((ht, wd)) / 255.0) | |
plt.imshow(bin_img, cmap='gray') | |
plt.savefig('binary.png') | |
def find_score(arr, angle): | |
data = inter.rotate(arr, angle, reshape=False, order=0) | |
hist = np.sum(data, axis=1) | |
score = np.sum((hist[1:] - hist[:-1]) ** 2) | |
return hist, score | |
delta = 1 | |
limit = 5 | |
angles = np.arange(-limit, limit+delta, delta) | |
scores = [] | |
for angle in angles: | |
hist, score = find_score(bin_img, angle) | |
scores.append(score) | |
best_score = max(scores) | |
best_angle = angles[scores.index(best_score)] | |
print('Best angle: {}'.formate(best_angle)) | |
# correct skew | |
data = inter.rotate(bin_img, best_angle, reshape=False, order=0) | |
img = im.fromarray((255 * data).astype("uint8")).convert("RGB") | |
img.save('skew_corrected.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment