Allow scanning multiple images

This commit is contained in:
RunasSudo 2025-01-11 17:54:54 +11:00
parent a2681d2a22
commit 8a7940bfbb
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# GIMP 3.0 plug-in for scanning via XSane
# Copyright (C) 2024 Lee Yingtong Li (RunasSudo)
# Copyright (C) 2024-2025 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -28,6 +28,7 @@ import sys
import tempfile
DEVICE_NAME = None # e.g. DEVICE_NAME = 'escl:http://10.0.0.1:80'
SCAN_MULTIPLE = True
def xsanecli_run(procedure, config, run_data, *args):
# Get temporary directory
@ -38,15 +39,14 @@ def xsanecli_run(procedure, config, run_data, *args):
args = ['xsane', '--save', '--no-mode-selection', '--force-filename', png_out, '--print-filenames'] + ([DEVICE_NAME] if DEVICE_NAME else [])
proc = subprocess.Popen(args, stdout=subprocess.PIPE, encoding='utf-8')
while True:
# Wait until XSane prints the name of the scanned file, indicating scanning is finished
# This blocks Python but that is ok because GIMP UI is not affected
result = proc.stdout.readline().strip()
proc.terminate()
if result == '':
# XSane was closed
return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.CANCEL)])
break
if result != 'XSANE_IMAGE_FILENAME: ' + png_out:
Gimp.message('Unexpected XSane result')
@ -58,6 +58,11 @@ def xsanecli_run(procedure, config, run_data, *args):
# Remove temporary files
os.unlink(png_out)
if not SCAN_MULTIPLE:
proc.terminate()
break
os.rmdir(tempdir)
return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.SUCCESS), GObject.Value(Gimp.Image.__gtype__, image)])