Skip to content

Commit

Permalink
Fix scanning of rear pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropombeiro committed Oct 6, 2024
1 parent 7ae89df commit 308845a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
2 changes: 2 additions & 0 deletions files/runScanner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ echo "capabilities:"
scanimage -A

echo "startup successful"
rm -f /var/log/scanner.log
touch /var/log/scanner.log
while true; do
tail -f /var/log/scanner.log
done
Expand Down
66 changes: 16 additions & 50 deletions script/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def remove_blank_pages(
return
page_count = int(pages_line.group(1))

print(
f" Analyzing {page_count} pages in {input_file} with threshold {remove_blank_threshold}%"
)
print(f" Analyzing {page_count} pages in {input_file} with threshold {remove_blank_threshold}%")
os.chdir(dirname)

def non_blank_pages() -> List[str]:
Expand All @@ -186,22 +184,14 @@ def non_blank_pages() -> List[str]:
output.decode(),
re.MULTILINE,
)
if ink_coverage_line is None:
ink_coverage = None
else:
if ink_coverage_line is not None:
ink_coverage = sum(map(float, ink_coverage_line.groups()))

if ink_coverage is not None and ink_coverage < remove_blank_threshold:
print(
f" Page {
page}: delete (ink coverage: {ink_coverage:.2f}%)"
)
else:
picked_pages += str(page)
print(
f" Page {
page}: keep (ink coverage: {ink_coverage:.2f}%)"
)
if ink_coverage < remove_blank_threshold:
print(f" Page {page}: delete (ink coverage: {ink_coverage:.2f}%)")
else:
picked_pages += str(page)
print(f" Page {page}: keep (ink coverage: {ink_coverage:.2f}%)")

return picked_pages

Expand All @@ -224,20 +214,11 @@ def non_blank_pages() -> List[str]:
print(f" No blank pages detected in {input_file}")
else:
os.replace(output_file, input_file)
print(
f" Removed {
removed_pages} blank pages and saved as {input_file}"
)
print(f" Removed {removed_pages} blank pages and saved as {input_file}")
except FileNotFoundError:
print(
f" WARNING: '{
command[0]}' executable not found. Skipping PDF manipulation."
)
print(f" WARNING: '{command[0]}' executable not found. Skipping PDF manipulation.")
except subprocess.CalledProcessError:
print(
f" ERROR: manipulating {
input_file}. Skipping PDF manipulation."
)
print(f" ERROR: manipulating {input_file}. Skipping PDF manipulation.")


#
Expand Down Expand Up @@ -334,10 +315,7 @@ def convert_and_post_process(

def wait_for_rear_pages_or_convert(job_name: str) -> None:
# Wait for 2 minutes in case there is a rear side scan
print(
f" front side: Waiting for 2 minutes before starting file conversion for {
job_name}"
)
print(f" front side: Waiting for 2 minutes before starting file conversion for {job_name}")
time.sleep(120)

convert_and_post_process(job_name, "front", None)
Expand Down Expand Up @@ -391,10 +369,7 @@ def kill_front_processing_from_pid(job_dir: str) -> Optional[int]:
try:
with open(path, "r") as scan_pid_file:
pid = int(scan_pid_file.read().strip())
print(
f" rear side: Read pid from {
path}, killing front processing job {pid}"
)
print(f" rear side: Read pid from {path}, killing front processing job {pid}")
os.kill(pid, signal.SIGKILL)
except FileNotFoundError:
print(" rear side: ERROR: scan_pid file {path} not found.")
Expand Down Expand Up @@ -439,10 +414,7 @@ def scan_front(log: TextIO, device: Optional[str], scanimage_args=[]) -> None:
os._exit(0) # Exit child process cleanly
elif pid > 0:
save_front_processing_pid(job_dir, pid)
print(
f" front side: INFO: Waiting to start conversion process for {
job_name} in process with PID {pid}"
)
print(f" front side: INFO: Waiting to start conversion process for {job_name} in process with PID {pid}")
else:
print(f" front side: ERROR: Fork failed ({pid}).")

Expand Down Expand Up @@ -488,26 +460,20 @@ def scan_rear(log: TextIO, device: Optional[str], scanimage_args=None) -> None:
cnt += 1
cnt_formatted = f"{cnt:03d}"
os.rename(filename, f"index{cnt_formatted}-1-{filename}")
print(
f" rear side: DEBUG: renamed {filename} to index{
cnt_formatted}-1-{filename}"
)
print(f" rear side: DEBUG: renamed {filename} to index{cnt_formatted}-1-{filename}")

cnt = 0
for filename in glob.glob("*back*.pnm"):
cnt += 1
rear_index = number_of_pages - cnt + 1
rear_index_formatted = f"{rear_index:03d}"
os.rename(filename, f"index{rear_index_formatted}-2-{filename}")
print(
f" rear side: DEBUG: renamed {filename} to index{
rear_index_formatted}-2-{filename}"
)
print(f" rear side: DEBUG: renamed {filename} to index{rear_index_formatted}-2-{filename}")

# Convert to PDF
remove_blank_threshold_str = os.getenv("REMOVE_BLANK_THRESHOLD")
remove_blank_threshold = None
if remove_blank_threshold_str is not None:
if remove_blank_threshold_str is not None and remove_blank_threshold_str != "":
remove_blank_threshold = float(remove_blank_threshold_str)

pid = os.fork()
Expand Down

0 comments on commit 308845a

Please sign in to comment.