Моя настройка: WSL ubuntu (я подключил USB-порт, lsub показывает сканер Canon) p>
Код, который я сейчас использую:
Код: Выделить всё
import snoop
import stackprinter
stackprinter.set_excepthook(style='darkbg2')
from loguru import logger
logger.add("logfile.log", rotation="500 MB",mode = "w") # Automatically rotate too big file
import time
import sane
import numpy as np
from PIL import Image
def init_scanner():
"""Initialize SANE and find available devices."""
sane.init()
devices = sane.get_devices()
print("Available devices:")
for device in devices:
print(f"- {device[0]} ({device[1]} {device[2]})")
return devices
def find_canon_scanner(prefer_escl=True):
"""
Find the Canon LIDE 400 scanner device name.
Args:
prefer_escl (bool): If True, prefer the ESCL device over AirScan
"""
devices = init_scanner()
escl_device = None
airscan_device = None
for device in devices:
if 'lide 400' in device[2].lower():
if 'escl:' in device[0].lower():
escl_device = device[0]
elif 'airscan:' in device[0].lower():
airscan_device = device[0]
# Return the preferred device or fallback to the available one
if prefer_escl:
return escl_device or airscan_device
return airscan_device or escl_device
def scan_image(dpi=300, mode='color', format='png', prefer_escl=True):
"""
Scan an image using the Canon LIDE 400.
Args:
dpi (int): Scanning resolution in DPI
mode (str): Scanning mode ('color', 'gray', or 'lineart')
format (str): Output format ('png', 'jpeg', etc.)
prefer_escl (bool): If True, prefer the ESCL device over AirScan
Returns:
str: Path to saved image file
"""
try:
# Find the scanner
device_name = find_canon_scanner(prefer_escl)
if not device_name:
raise Exception("Canon LIDE 400 scanner not found")
print(f"Using scanner: {device_name}")
# Open the device
scanner = sane.open(device_name)
# Configure scanner settings
scanner.mode = mode
scanner.resolution = dpi
# Some scanners need a moment to initialize
time.sleep(1)
# Start the scan
print("Starting scan...")
scanner.start()
# Get the image
img = scanner.snap()
# Convert to PIL Image
if mode == 'color':
pil_img = Image.fromarray(img)
else:
pil_img = Image.fromarray(np.uint8(img))
# Save the image
timestamp = time.strftime("%Y%m%d_%H%M%S")
output_file = f'scan_{timestamp}_{mode}_{dpi}dpi.{format}'
pil_img.save(output_file, format=format)
print(f"Scan saved as {output_file}")
# Clean up
scanner.close()
return output_file
except Exception as e:
print(f"Error during scanning: {str(e)}")
if 'scanner' in locals():
scanner.close()
return None
if __name__ == "__main__":
scan_image(
dpi=300,
mode='color',
format='png',
prefer_escl=True
)
- escl:http://10.255.255.254:60000 (Canon LiDE 400 (USB) )
- airscan:e0:Canon LiDE 400 (USB) (eSCL Canon LiDE 400 (USB))
Ошибка получения параметров сканера: неверный аргумент
Подробнее здесь: https://stackoverflow.com/questions/791 ... d-argument