python-pillow / Sane

Python interface to the SANE scanner and frame grabber
Other
55 stars 19 forks source link

Unable to do Duplex Scanning #70

Open kamidehlvi opened 3 years ago

kamidehlvi commented 3 years ago

Hi, I am trying to do duplex scanning using Python-sane but unable to do duplex scanning please help me ... regards

import scanPython
from PIL import  Image

mode = 'grey'

var = scanPython.init()
print("sane version ", var)
devices = scanPython.get_devices()
print('Available devices:', devices)

dev = scanPython.open(devices[0][0])
dev.__setattr__(2,'ADF Duplex') 
print("Current Source",dev.__getattr__(2))
dev.set_mode =mode
dev.resolution = 200
dev.start()
iterator = dev.multi_scan()
progress = True

while progress:
    try:
        image = iterator.next()
 except:
        progress = False
        break
#dev.cancel()

dev.close()
kamidehlvi commented 3 years ago

I am using Ubuntu 18.4 LTS , Python 2.7

kamidehlvi commented 3 years ago

iter = sane._SaneIterator(dev) iter = dev.multi_scan()

Its working now as iterator class was not used ... Another question is how can we save the images during Next() within while loop are add them in an array but how can we identify Front and Back side of the scanned images please?

aravindor commented 3 years ago

iter = dev.multi_scan()

Hi , did u find any solution ?

furqanazhar commented 8 months ago

i am unable to perform duplex scanning with below code, kindly help me here. Below is my code.

import os
from PIL import Image
import sane

# Initialize sane
ver = sane.init()
print('SANE version:', ver)

# Get devices
devices = sane.get_devices()
print('Available devices:', devices)

# Open first device
dev = sane.open(devices[0][0])
try:
    dev.br_x = 320.
    dev.br_y = 240.
except:
    print('Cannot set scan area, using default')

params = dev.get_parameters()
print('Device parameters:', params)

# Create a directory to store scanned images
output_dir = "scanned_images"
os.makedirs(output_dir, exist_ok=True)

# Start scanning
images_list = []
try:
    # Perform scanning operation
    images = dev.multi_scan()
    print('Scanning documents')

    # Save each scanned image to a separate file
    for i, image in enumerate(images):
        output_filename = os.path.join(output_dir, f"page_{i + 1}.png")
        # Convert the image to PIL Image object
        pil_image = Image.frombytes('RGB', image.size, image.tobytes())
        # Rotate the image
        pil_image = pil_image.rotate(180)
        # Save the image
        pil_image.save(output_filename)
        images_list.append(output_filename)
        print(f"Saved {output_filename}")

except KeyboardInterrupt:
    pass

print('Converting images to pdf')

images = []
for image in images_list:
    i = Image.open(image)
    images.append(i)

images[0].save(f'{output_dir}/scan.pdf', "PDF", resolution=300.0, save_all=True, append_images=images[1:])
print('Conversion complete')