Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not display the source user interface #19

Open
lisha992610 opened this issue Feb 12, 2019 · 2 comments
Open

Could not display the source user interface #19

lisha992610 opened this issue Feb 12, 2019 · 2 comments

Comments

@lisha992610
Copy link

lisha992610 commented Feb 12, 2019

Hello

I am implementing an application with Epson scan.
I tried to use this command "ss.RequestAcquire(1,0)" to show the UI. but the UI was not completely opened and was closed again automatically.
How to keep the UI windows alive and trigger the scan directly on the UI?

I added a "while True" loop after "ss.RequestAcquire(1,0)", in order to keep the python program running while the UI is displayed. Only a partial of the UI is able to be displayed and the program crashed.

Could you please help me out here? What could be the problem? Thank you so much.

p.s. how can I avoid the following exception?
Exception ignored in: <bound method SourceManager.del of <twain.SourceManager object at 0x002D5A50>>
Traceback (most recent call last):
File "C:\Users\AppData\Local\conda\conda\envs\py35_32\lib\site-packages\twain.py", line 2066, in del
File "C:\Users\AppData\Local\conda\conda\envs\py35_32\lib\site-packages\twain.py", line 2073, in close
File "C:\Users\AppData\Local\conda\conda\envs\py35_32\lib_weakrefset.py", line 101, in pop
KeyError: 'pop from empty WeakSet'

Best regards,
lisha

@lisha992610 lisha992610 changed the title Could not the source user interface Could not display the source user interface Feb 14, 2019
@martintan
Copy link

martintan commented Nov 10, 2020

The source user interface closes immediately because of Python's garbage collection. The solution I found for this is to use QThread. Here is a part of my code that works:

class ScanPDFThread(QtCore.QThread):
  def __init__(self, parent, folder_path: str, settings:dict={}):
    QtCore.QThread.__init__(self)
    self.parent = parent
    self.scan_pdf(folder_path, settings)

  def scan_pdf(self, folder_path: str, settings):
    try:
      sm, ds = self.initialize_scanner(settings)
      # Source: Twain 2.4 Spec ch.10 p.420
      ds.SetCapability(twain.CAP_XFERCOUNT, twain.TWTY_INT16, -1)
      ds.RequestAcquire(1, 1)

      with tempfile.TemporaryDirectory() as tempdir:
        def temp_file(i): return os.path.join(tempdir, f'temp-{i}.bmp')
        more_to_come = True
        i = 0
        while more_to_come:
          if not ds.GetImageInfo(): continue
          more_to_come = self.transfer_native(ds, temp_file(i))
          i += 1
          time.sleep(0.25)
        
        images = [Image.open(temp_file(i)) for i in range(i)]
        # Source: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#pdf
        file_name = datetime.datetime.now().strftime('%d-%m-%Y_%H-%M-%S') + '.pdf'
        save_path = os.path.join(folder_path, file_name)
        images[0].save(save_path, save_all=True, append_images=images[1:])
        for i in images: i.close()

        sm.destroy()
        ds.destroy()

    except Exception as e:
      ei = sys.exc_info()
      logging.error('Scanner Scan PDF error', exc_info=str(e))
      traceback.print_exception(ei[0], ei[1], ei[2])

  def initialize_scanner(self, settings:dict={}):
    sm = twain.SourceManager(self.parent.winId(), ProductName='Demo')
    ds = sm.OpenSource()

    ds.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, 200)
    ds.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, 200)
    _, has_duplex = ds.GetCapability(twain.CAP_DUPLEX)
    if has_duplex == twain.TWDX_1PASSDUPLEX or has_duplex == twain.TWDX_2PASSDUPLEX:
    ds.SetCapability(twain.CAP_DUPLEXENABLED, twain.TWTY_BOOL, True)
    ds.SetCapability(twain.ICAP_AUTOMATICDESKEW, twain.TWTY_BOOL, True)
    ds.SetCapability(twain.ICAP_PIXELTYPE, twain.TWTY_UINT16, twain.TWPT_RGB)
    # ds.SetCapability(twain.ICAP_MAXFRAMES, twain.TWTY_UINT16, 1)

    return (sm, ds)

Edit (11/15/2020):
After some more coding, I found out that you also have to call data_source.modal_loop() after data_source.request_acquire(1, 1) for the source dialog to work properly.

@Dhruv00710
Copy link

Dhruv00710 commented Aug 27, 2023

sir can you help me about multiple file scan and saving
i design a form in form one checkbox and two button
checkbox indicate show 'show_ui' means request_acquire(1, 1) or request_acquire(0, 0)
1st button select scanner from list
2nd button to scan document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants