How to use the "twain-acquire" function in GIMP Python-Fu?

I’ve got a task I’m working on that involves repeated scanning via GIMP, and I’m stuck on a Windows box to do it.

I get the best results in GIMP when I scan via the scanner’s TWAIN driver. All works well when I go through the menus to acquire via TWAIN.

I would like to automate this task (at least partially) via Python-Fu. However, I cannot figure out how to call the “twain-acquire” function via Python-Fu.

The “Browse…” dialog describes the function as taking a single argument, with that argument being “The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }”. It also indicates that the function returns two items, the first being the image count and the second being the IDs of the images.

It seems like it should be straight-forward to call this function, but I apparently do not know how to do so without generating an error. Calling it with a 0 or 1 argument generates an error:

>>> image_count, image_ids = pdb.twain_acquire(0)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: calling error
>>> image_count, image_ids = pdb.twain_acquire(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: calling error

And calling it with no argument generates the same error:

>>> image_count, image_ids = pdb.twain_acquire()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: calling error

Can anyone kindly provide an example of how to use the twain-acquire function from Python-Fu? Or, more generally, how to invoke a TWAIN scan from Python-Fu?

Thanks!

The “run mode” is not positional in the Python calls. It is an optional named parameter:

pdb.some_call(parm1,parm2,..,run_mode=RUN_INTERACTIVE)

So in your case, that could be a simple:

pdb.twain_acquire()

This said I would scan outside of Gimp (because you can then pass parameters to the scanner application driver) and then call Gimp on the file.

Thanks for that information! I should’ve realized it was non-positional. It works with a call of

image_count, image_ids = pdb.twain_acquire(run_mode=RUN_INTERACTIVE)

Strangely, it does not appear to work with a run_mode of RUN_NONINTERACTIVE or if the run_mode argument is not provided, though. In that case, it fails with the same error message shown in my original posting.

I agree with your assessment – I believe it will make more sense to acquire the file elsewhere and simply script the GIMP portion of my workflow to work with a file input. I’ll work down that path.

Thanks again!

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.