New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for negative signs and optimization of exclusions #76
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ def find_images(folder): | |
pass | ||
|
||
|
||
def rotate_image(input_file, output_file, angle=90): | ||
def rotate_image(input_file, output_file, angle): | ||
""" | ||
:param input_file: str | ||
Path to image to rotate | ||
|
@@ -76,14 +76,22 @@ def rotate_image(input_file, output_file, angle=90): | |
:return: void | ||
Rotates image and saves result | ||
""" | ||
|
||
if (angle == 0): | ||
return None | ||
elif (angle == -90): | ||
angle = 90 | ||
elif (angle == 90): | ||
angle = -90 | ||
|
||
print(ORANGE + '\t~: ' + RESET + 'Rotate image' + RESET) | ||
with WandImage(filename=input_file) as img: | ||
with img.clone() as rotated: | ||
rotated.rotate(angle) | ||
rotated.save(filename=output_file) | ||
|
||
|
||
def sharpen_image(input_file, output_file): | ||
def sharpen_image(input_file, output_file, angle): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see that you added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this with last commit, thanks for your input! 👍 |
||
""" | ||
:param input_file: str | ||
Path to image to prettify | ||
|
@@ -93,7 +101,7 @@ def sharpen_image(input_file, output_file): | |
Prettifies image and saves result | ||
""" | ||
|
||
rotate_image(input_file, output_file) # rotate | ||
rotate_image(input_file, output_file, angle) # rotate | ||
print(ORANGE + '\t~: ' + RESET + 'Increase image contrast and sharp image' + RESET) | ||
|
||
with WandImage(filename=output_file) as img: | ||
|
@@ -167,15 +175,20 @@ def remove_noise(img): | |
|
||
|
||
def deskew(image): | ||
output = [] | ||
coords = np.column_stack(np.where(image > 0)) | ||
angle = cv2.minAreaRect(coords)[-1] | ||
|
||
print(ORANGE + '\t~: ' + RESET + 'Get rotation angle:' + str(angle) + RESET) | ||
|
||
# (h, w) = image.shape[:2] | ||
# center = (w // 2, h // 2) | ||
# M = cv2.getRotationMatrix2D(center, angle, 1.0) | ||
# rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) | ||
return image | ||
|
||
output = [image, int(angle)] | ||
|
||
return output | ||
|
||
|
||
def main(): | ||
|
@@ -212,9 +225,9 @@ def main(): | |
img = grayscale_image(img) | ||
img = remove_noise(img) | ||
img = deskew(img) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of [output, angle] = deskew(img) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this with last commit, thanks for your input! 👍 |
||
cv2.imwrite(tmp_path, img) | ||
cv2.imwrite(tmp_path, img[0]) | ||
|
||
sharpen_image(tmp_path, tmp_path) | ||
sharpen_image(tmp_path, tmp_path, img[1]) | ||
run_tesseract(tmp_path, out_path, config.language) | ||
|
||
i = i + 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move those conditions out for separation of concerns:
the function should just rotate the image unconditionally; the logic whether to rotate the image should be outside.
Furthermore I have mixed feelings about reverting the sign of the rotation. What was the use-case for that?
So basically
What do you think? 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @monolidth said, this should rotate the image in the correct possition. Maybe it's not trivial.