128 lines
3.5 KiB
Python
128 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
""" Author: Hendrik Schutter, mail@hendrikschutter.com
|
|
"""
|
|
|
|
import cv2
|
|
import sys
|
|
import os
|
|
|
|
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_src:
|
|
print("unable to read image")
|
|
sys.exit(-1)
|
|
|
|
print("read 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(img_gray, 127, 255, 0)
|
|
|
|
# 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
|
|
|
|
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()
|