This commit is contained in:
Hendrik Schutter 2023-07-23 13:29:44 +02:00
parent f14b5c4510
commit 3935600df9
2 changed files with 116 additions and 51 deletions

View File

@ -13,4 +13,9 @@ scanimage --progress --output-file 01.png --format=png --mode Color --resolution
```
pip install opencv-python-headless
zypper install avif-tools
```
rsync -Pav -e "ssh -i $HOME/.ssh/SDS" /home/hendrik/git/ultimate-photo-digitizer/scans/ hendrik@10.0.0.25:/home/hendrik/git/ultimate-photo-digitizer/scans/

View File

@ -1,67 +1,127 @@
# import required libraries
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Author: Hendrik Schutter, mail@hendrikschutter.com
"""
import cv2
import sys
import os
# read the input image
img = cv2.imread('hires_test.png')
def convert_to_avif(input, output):
# print("input: " + str(input))
# print("output: " + str(output))
temp = subprocess.Popen(
["avifenc", "--jobs", "all", input, output], stdout=subprocess.PIPE
)
# get the output as a string
# output = str(temp.communicate())
# print(output)
temp = subprocess.Popen(
[
"sync",
],
stdout=subprocess.PIPE,
)
def auto_crop_scan(src_path, output_dir):
# read the input image
img_src = cv2.imread(src_path)
if not img:
print("unable to read image")
sys.exit(-1)
if not img_src:
print("unable to read image")
sys.exit(-1)
print("read done")
print("read done")
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("convert to gray done")
# convert the image to grayscale
img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
print("convert to gray done")
# apply thresholding on the gray image to create a binary image
ret,thresh = cv2.threshold(gray,127,255,0)
# apply thresholding on the gray image to create a binary image
ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
# find the contours
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# find the contours
contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
areas = list()
for cnt in contours:
#print(cv2.contourArea(cnt))
areas.append(cv2.contourArea(cnt))
areas.sort(reverse=True)
#print(areas)
if (len(areas) >= 5):
outer = areas[0]
inter_min = areas[4]
print("Outer area: " + str(outer))
print("Inner area: " + str(inter_min))
index = 0
areas = list()
for cnt in contours:
area = cv2.contourArea(cnt)
if( (area < outer) and (area >= inter_min)):
# compute the bounding rectangle of the contour
x,y,w,h = cv2.boundingRect(cnt)
# draw the bounding rectangle
#imgView = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# print(cv2.contourArea(cnt))
areas.append(cv2.contourArea(cnt))
x = x + 5
y = y + 5
w = w - 10
h = h - 10
areas.sort(reverse=True)
crop_img = img[y:y+h, x:x+w]
#cv2.imshow("cropped", crop_img)
cv2.imwrite("export_"+str(index)+".png", crop_img)
index = index + 1
#cv2.waitKey(0)
# print(areas)
# display the image with bounding rectangle drawn on it
#imgDownscaled = cv2.resize(imgView, (410, 876))
#cv2.imshow("Bounding Rectangle", imgDownscaled)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
if len(areas) >= 5:
outer = areas[0]
inter_min = areas[4]
print("Outer area: " + str(outer))
print("Inner area: " + str(inter_min))
index = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if (area < outer) and (area >= inter_min):
# compute the bounding rectangle of the contour
x, y, w, h = cv2.boundingRect(cnt)
# draw the bounding rectangle
# imgView = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
x = x + 5 # TODO
y = y + 5 # TODO
w = w - 10 # TODO
h = h - 10 # TODO
img_crop = img_src[y : y + h, x : x + w]
# cv2.imshow("cropped", crop_img)
export_file_name = (
os.path.splitext(os.path.basename(os.path.normpath(src_path)))[0]
+ "-"
+ str(index)
+ ".png"
)
print(export_file_name)
export_file_path = os.path.join(output_dir, export_file_name)
print(export_file_path)
cv2.imwrite(export_file_path, img_crop)
index = index + 1
# cv2.waitKey(0)
def main():
print("starting ...")
destination_dir_path = -1
source_dir_path = -1
tmp_dir_path = -1
for argument in sys.argv:
if argument.startswith("destination_dir"):
destination_dir_path = argument.split("=")[1]
if argument.startswith("source_dir"):
source_dir_path = argument.split("=")[1]
if argument.startswith("tmp_dir"):
tmp_dir_path = argument.split("=")[1]
if (destination_dir_path == -1) or (source_dir_path == -1) or (tmp_dir_path == -1):
print("Unable to parse config!")
print("Example usage:")
print(
" python extract_photos.py source_dir=scans/ destination_dir=photos/ tmp_dir=tmp/"
)
sys.exit(-1)
for path in os.listdir(source_dir_path):
if os.path.isfile(os.path.join(source_dir_path, path)):
print(path)
if __name__ == "__main__":
main()