Textbook-Arrange/fixer.py

30 lines
1.1 KiB
Python
Raw Normal View History

import cv2
import pytesseract
import os
import numpy
directory = 'images'
# iterate over files in
# that directory
pattern = 'review' # Replace with your target substring
matching_files = [f for f in os.listdir(directory) if pattern in f]
for files in os.listdir(directory):
if files.__contains__(pattern):
f = os.path.join(directory, files)
print(f)
image = cv2.imread(f, 0)
thresh = 255 - cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#bottom 2 locations
ROI1 = thresh[1383:1447,77:154]
ROI2 = thresh[1383:1447,950:1027]
ROI = cv2.hconcat([ROI1, ROI2])
cv2.imshow('Review', ROI)
cv2.waitKey(1)
manpage = input("please input the page number")
if os.path.isfile(f'images/{manpage}') == True:
rng = numpy.random.default_rng().random()
os.rename (f'images/{manpage}', f'images/{manpage}.review-{rng}')
os.rename(f'{f}', f'images/{manpage}')
elif os.path.isfile(f'images/{manpage}') == False:
os.rename(f'{f}', f'images/{manpage}')