batch scanning script

main
Hendrik Schutter 2023-07-23 11:57:38 +02:00
parent b4d6347578
commit f14b5c4510
3 changed files with 135 additions and 0 deletions

View File

@ -1,2 +1,16 @@
# ultimate-photo-digitizer
## Scanner comands
```
scanimage --progress --output-file 01.png --format=png --mode Color --resolution 2400 --verbose --depth 16
```
## Setup
```
pip install opencv-python-headless
```

67
extract_photos.py Normal file
View File

@ -0,0 +1,67 @@
# import required libraries
import cv2
import sys
# read the input image
img = cv2.imread('hires_test.png')
if not img:
print("unable to read image")
sys.exit(-1)
print("read done")
# convert the image to grayscale
gray = cv2.cvtColor(img, 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)
# 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
y = y + 5
w = w - 10
h = h - 10
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)
# 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()

54
scan_photos.py Normal file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Author: Hendrik Schutter, mail@hendrikschutter.com
"""
import os
import errno
import time
import shutil
import sys
import subprocess
scan_cmd = "scanimage --progress --format=png --mode Color --resolution 2400 --verbose --depth 16 --output-file "
def main():
print("starting ...")
destination_dir_path = -1
for argument in sys.argv:
if argument.startswith('destination_dir'):
destination_dir_path = argument.split("=")[1]
if (destination_dir_path == -1):
print("Unable to parse config!")
print("Example usage:")
print(" python scan_photos.py destination_dir=scans/ ")
sys.exit(-1)
filename = time.strftime("%Y-%m-%d_%H-%M-%S.png")
#print(filename)
filepath = os.path.join(destination_dir_path, filename)
#print(filepath)
cmd = scan_cmd + filepath
print(cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
#print("Command output : ",output.decode())
print("Command exit status/return code : ", p_status)
if(int(p_status) == 0):
print("Scan successful")
else:
print("Scan failed!")
if os.path.exists(filepath):
os.remove(filepath)
if __name__ == "__main__":
main()