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 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,38 +1,61 @@
# import required libraries #!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Author: Hendrik Schutter, mail@hendrikschutter.com
"""
import cv2 import cv2
import sys import sys
import os
# read the input image def convert_to_avif(input, output):
img = cv2.imread('hires_test.png') # 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: if not img_src:
print("unable to read image") print("unable to read image")
sys.exit(-1) sys.exit(-1)
print("read done") print("read done")
# convert the image to grayscale # convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
print("convert to gray done") print("convert to gray done")
# apply thresholding on the gray image to create a binary image # apply thresholding on the gray image to create a binary image
ret,thresh = cv2.threshold(gray,127,255,0) ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
# find the contours # find the contours
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
areas = list() areas = list()
for cnt in contours: for cnt in contours:
#print(cv2.contourArea(cnt)) # print(cv2.contourArea(cnt))
areas.append(cv2.contourArea(cnt)) areas.append(cv2.contourArea(cnt))
areas.sort(reverse=True) areas.sort(reverse=True)
#print(areas) # print(areas)
if (len(areas) >= 5): if len(areas) >= 5:
outer = areas[0] outer = areas[0]
inter_min = areas[4] inter_min = areas[4]
print("Outer area: " + str(outer)) print("Outer area: " + str(outer))
@ -42,26 +65,63 @@ if (len(areas) >= 5):
for cnt in contours: for cnt in contours:
area = cv2.contourArea(cnt) area = cv2.contourArea(cnt)
if( (area < outer) and (area >= inter_min)): if (area < outer) and (area >= inter_min):
# compute the bounding rectangle of the contour # compute the bounding rectangle of the contour
x,y,w,h = cv2.boundingRect(cnt) x, y, w, h = cv2.boundingRect(cnt)
# draw the bounding rectangle # draw the bounding rectangle
#imgView = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) # imgView = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
x = x + 5 x = x + 5 # TODO
y = y + 5 y = y + 5 # TODO
w = w - 10 w = w - 10 # TODO
h = h - 10 h = h - 10 # TODO
crop_img = img[y:y+h, x:x+w] img_crop = img_src[y : y + h, x : x + w]
#cv2.imshow("cropped", crop_img) # cv2.imshow("cropped", crop_img)
cv2.imwrite("export_"+str(index)+".png", 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 index = index + 1
#cv2.waitKey(0) # cv2.waitKey(0)
# 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()
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()