From: News123 on


News123 wrote:
> Hi,
>
> I'm trying to scan a document from a python 2.6 script without user
> interaction.
>
> I found a code snippet, that allows me to scan under Vista, but that
> doesn't allow me to select the dpi / color mode / etc.

I'm still stuck.


I'll try to look at any solution (visual C++, C#, Basic) first.
Then it's perhaps easier to pythonize it via python .net.

However I don't know whether python .net exists for 2.6 and whether it
is stable enough.


N

>
> The snippet uses win32com.client
>
> # ##################### script start
> import win32com.client,os
>
> WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
> WIA_COMMAND_TAKE_PICTURE = "{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}"
>
> os.chdir('c:/temp')
> wia = win32com.client.Dispatch("WIA.CommonDialog")
> dev = wia.ShowSelectDevice()
> for command in dev.Commands:
> if command.CommandID==WIA_COMMAND_TAKE_PICTURE:
> foo=dev.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE)
> i=1
> for item in dev.Items:
> if i==dev.Items.Count:
> image=item.Transfer(WIA_IMG_FORMAT_PNG)
> break
> i=i+1
>
> image.SaveFile("test.png")
> ######################### script end
>
>
> My problems are:
>
> - This script works fine for me under Windows 7, however I'm
> unable to specify additional parameters, like dpi and
> color mode.
>
> - The script doesn't work under windows XP, though the scanner driver is
> installed. (Gimp finds the scanner (as WIA scanner)).
> Perhaps 'WIA.CommonDialig' has another name or I need to install some DLL.
> The error message is:
> --------------------------------------------------------------------
> Traceback (most recent call last):
> File "C:\work\python\minidemos\wia_get_simple.py", line 7, in <module>
> wia = win32com.client.Dispatch("WIA.CommonDialog")
> File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line
> 95, in Dispatch
> dispatch, userName =
> dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
> File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line
> 104, in _GetGoodDispatchAndUserName
> return (_GetGoodDispatch(IDispatch, clsctx), userName)
> File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line
> 84, in _GetGoodDispatch
> IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
> pythoncom.IID_IDispatch)
> com_error: (-2147221005, 'Invalid class string', None, None)
> ---------------------------------------------------------------------
>
>
> As I have no knowledge of Win32com and WIA I would appreciate some help
> or good documentation about wincom32 / WIA with python
>
>
>
> thanks for your help and bye
>
>
> N
>
>
>
>
>
>
From: News123 on
r wrote:
> more *maybe useful dump?
>
>>>> for i in dev.Items:
> for p in i.Properties:
> if not p.IsReadOnly:
> print p.Name, '->', p.Value
>
.. . .
> Horizontal Resolution -> 200
> Vertical Resolution -> 200
> Horizontal Start Position -> 0
.. . .
>
> Now how to set the values... hmmm?
>

How to set the values? This is THE magic question.

At least I found out how to set the values in C# :

foreach (Property prop in item.Properties){
if (prop.IsReadOnly) continue;
if (prop.Name == "Horizontal Resolution")
{
IProperty iprop = (IProperty)prop;
Object val = 75;
iprop.set_Value(ref val);
}
}



Below my most recent complete script:
(still not able to set params, though I can with C#)

import win32com.client, os

WIA_COM = "WIA.CommonDialog"
WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
WIA_COMMAND_TAKE_PICTURE="{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}"

def takePictureOrNot(dev): # cameras have to call TakePicture
for command in dev.Commands:
if command.CommandID==WIA_COMMAND_TAKE_PICTURE:
print "take PICK"
foo=dev.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE)

def setImgProps(item): # here scan properties should be set
for prop in item.Properties:
if prop.IsReadOnly: continue
if(prop.Name == "Horizontal Resolution"):
res = 250
print "trying to set",prop.Name,prop,"to ",res
### unfortunately the next line (if uncommented) fails
#prop.set_Value(res)

def transferImg(dev): # set properties and scan image
i=1
image = None
for item in dev.Items:
if i==dev.Items.Count:
setImgProps(item)
image=item.Transfer(WIA_IMG_FORMAT_PNG)
break
i=i+1
return image

def scan_image_wia():
wia = win32com.client.Dispatch(WIA_COM) # CommonDialog object
dev = wia.ShowSelectDevice()
takePictureOrNot(dev)
image = transferImg(dev)
return image

image = scan_image_wia()
fname = 'wia-test.jpg'
if os.path.exists(fname):
os.remove(fname)
image.SaveFile(fname)


bye

N


From: News123 on
Meanwhile I found out, why my script worked only on windows 7/Vista and
not on my XP host.

The WIA 2.0 Scripting Model is by default not installed on Windows XP.

Install instructions can be found at
http://msdn.microsoft.com/en-us/library/ms630827%28VS.85%29.aspx


My second problem (changing parameters) is still not solved in python.

bye

N

News123 wrote:
> Hi,
>
> I'm trying to scan a document from a python 2.6 script without user
> interaction.
>
> I found a code snippet, that allows me to scan under Vista, but that
> doesn't allow me to select the dpi / color mode / etc.
>
> The snippet uses win32com.client
>
> # ##################### script start
> import win32com.client,os
>
> WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
> WIA_COMMAND_TAKE_PICTURE = "{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}"
>
> os.chdir('c:/temp')
> wia = win32com.client.Dispatch("WIA.CommonDialog")
> dev = wia.ShowSelectDevice()
> for command in dev.Commands:
> if command.CommandID==WIA_COMMAND_TAKE_PICTURE:
> foo=dev.ExecuteCommand(WIA_COMMAND_TAKE_PICTURE)
> i=1
> for item in dev.Items:
> if i==dev.Items.Count:
> image=item.Transfer(WIA_IMG_FORMAT_PNG)
> break
> i=i+1
>
> image.SaveFile("test.png")
> ######################### script end
>
>
> My problems are:
>
> - This script works fine for me under Windows 7, however I'm
> unable to specify additional parameters, like dpi and
> color mode.
>
> - The script doesn't work under windows XP, though the scanner driver is
> installed. (Gimp finds the scanner (as WIA scanner)).
> Perhaps 'WIA.CommonDialig' has another name or I need to install some DLL.
> The error message is:
> --------------------------------------------------------------------
> Traceback (most recent call last):
> File "C:\work\python\minidemos\wia_get_simple.py", line 7, in <module>
> wia = win32com.client.Dispatch("WIA.CommonDialog")
> File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line
> 95, in Dispatch
> dispatch, userName =
> dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
> File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line
> 104, in _GetGoodDispatchAndUserName
> return (_GetGoodDispatch(IDispatch, clsctx), userName)
> File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line
> 84, in _GetGoodDispatch
> IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
> pythoncom.IID_IDispatch)
> com_error: (-2147221005, 'Invalid class string', None, None)
> ---------------------------------------------------------------------



From: MRAB on
News123 wrote:
> r wrote:
>> more *maybe useful dump?
>>
>>>>> for i in dev.Items:
>> for p in i.Properties:
>> if not p.IsReadOnly:
>> print p.Name, '->', p.Value
>>
> . . .
>> Horizontal Resolution -> 200
>> Vertical Resolution -> 200
>> Horizontal Start Position -> 0
> . . .
>> Now how to set the values... hmmm?
>>
Well, according to that they /aren't/ read-only because it says:

if not p.IsReadOnly:
^^^

From: News123 on
MRAB wrote:
> News123 wrote:
>> r wrote:
>>> more *maybe useful dump?
>>>
>>>>>> for i in dev.Items:
>>> for p in i.Properties:
>>> if not p.IsReadOnly:
>>> print p.Name, '->', p.Value
>>>
>> . . .
>>> Horizontal Resolution -> 200
>>> Vertical Resolution -> 200
>>> Horizontal Start Position -> 0
>> . . .
>>> Now how to set the values... hmmm?
>>>
> Well, according to that they /aren't/ read-only because it says:
>
> if not p.IsReadOnly:
> ^^^
>
Exactly they are NOT read only, which means they are supposed to be
writable.

the python code is
for p in i.Properties:
if not.p.ISReadOnly:
# if I'm here I'm supposed to change them, but how?


the C# code, which succeds changing the property is:
foreach (Property prop in item.Properties){
if (prop.IsReadOnly) continue; // skip rest of loop
// if property cannot
// be modified
// now change property
if (prop.Name == "Horizontal Resolution")
{
IProperty iprop = (IProperty)prop;
Object val = 75;
iprop.set_Value(ref val);
}
}



The main question is the correct python method to change the property

prop = 300 # doesn't work as it just replaces the property with an integer

prop.set_Value(res) # doesn't work as the method set_Value doesn't seem
to exist

> Traceback (most recent call last):
> File "C:\WIA\scan_wia.py", line 41, in <module>
> image = scan_image_wia()
> File "C:\WIA\scan_wia.py", line 37, in scan_image_wia
> image = transferImg(dev)
> File "C:\WIA\scan_wia.py", line 27, in transferImg
> setImgProps(item)
> File "C:\WIA\scan_wia.py", line 20, in setImgProps
> prop.set_Value(res)
> File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 512, in
> __getattr__
> raise AttributeError("%s.%s" % (self._username_, attr))
> AttributeError: <unknown>.set_Value


bye


N


First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: python bijection
Next: python and netezza