Alan Gauld | 1 Jul 01:14 2007

Re: 200 dollar questions!

"elis aeris" <hunter92383 <at> gmail.com> wrote

> I am using pywin32, for python 2.5 on windows xp.
> What I want to do is these:
>
> handle = Gethandle ("window_name")

Try FindWindow(), it returns a handle given a class/window title

> "window_name"   =  "123.txt"  with notepad, for example.

That looks like the title to me...

> it should return a list if there are more than one with the same 
> name.

Nope, it only returns one, but enumWindows will give you a list
(or more accurately will yield a list if called repeatedly)

> GainFocus(handle)

I think you need SetFocus() here

> Keyboard_event ( "hello python!")
> Mouse_event (x,y, left, 2)

> the (x,y) = should be relative to the active window and independent 
> of the
> window's position. 2 as in clicking twice.

(Continue reading)

Alan Gauld | 1 Jul 01:21 2007

Re: Mouse clicking

"elis aeris" <hunter92383 <at> gmail.com> wrote in

> It's very cryptic to me and more or less is not explained in any 
> python
> tutorials.
>
> http://search.msdn.microsoft.com/search/results.aspx?view=msdn&qu=mouse_event
> http://search.msdn.microsoft.com/search/results.aspx?view=msdn&qu=keybd_event
> http://search.msdn.microsoft.com/search/results.aspx?view=msdn&qu=GetFocus

It wouldn't be in a Python tutorial because its all about the Windows
API and few Python programmers get involved with Windowes at
that level. It can be done using PyWin32 but that is just a wrapper
around the Windows C functions. You need to understand how
they work (And Microsoft documentation is notoriously poor)

Personally I do most of my low level Windows work using Borlands
Delphi or C++Builder and use a couple of books on those tools along
with the MS Help files. But the Python wrapper should work just as 
well.

But, normally, a python programmer will step back and try to write a 
program
that does what is needed directly in Python using a high level module
rather than calling OS API calls directly. (Thus is true in Unix as 
well
as Windows)

So rather than try to robotically control Notepad say, its easier to
write a Python program to edit the text file directly.
(Continue reading)

elis aeris | 1 Jul 01:35 2007
Picon

Re: 200 dollar questions!

import win32api
import win32ui
window = win32ui.FindWindow(None, "123.txt")
yourwindow.ShowWindow()

Traceback (most recent call last):
  File "C:/python codes/gain_focus.py", line 3, in <module>
    window = win32ui.FindWindow(None, "123.txt")
win32ui: No window can be found.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Alejandro Decchi | 1 Jul 02:26 2007
Picon

Help search file

Hello Python user.
I am a newbie with python and i am interesting to do a web apliation to search file on the hard disk and give a link to download the file found. Someone can give a help ???
Thz
Ale
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
elis aeris | 1 Jul 04:26 2007
Picon

Re: 200 dollar questions!

I ran this:




import time

import ImageGrab  # Part of PIL
from ctypes import *

# Load up the Win32 APIs we need to use.
class RECT(Structure):
  _fields_ = [
    ('left', c_ulong),
    ('top', c_ulong),
    ('right', c_ulong),
    ('bottom', c_ulong)
    ]

time.sleep(2)

GetForegroundWindow = windll.user32.GetForegroundWindow
GetWindowRect = windll.user32.GetWindowRect

# Sleep for 2 seconds - click the window you want to grab.
#time.sleep(2)

# Grab the foreground window's screen rectangle.
rect = RECT()
foreground_window = GetForegroundWindow()
GetWindowRect(foreground_window, byref(rect))
image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom))

# Save the screenshot as a BMP.
#image.save("c:\ee\screenshot.bmp")

# Get the pixel 10 pixels along the top of the foreground window - this
# will be a piece of the window border.

print time.time()

x = 0
y = 0
while x < 400:
  while y < 20:
    rgb = image.getpixel((10, 0))
    y = y + 1
  y = 0
  x = x + 1


print time.time()

# PIL returns colours as RGB values packed into a triple:
print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2])  # This prints RGB(0, 74, 216) on my XP machine






















What that does is to take a screen shot and then pixelgetcolor() over 8000  (x,y)  points

for me it clocked at 0.08 seconds and I am trying cut it down to maybe 0.04 

any hints on performance increase?


_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Luke Paireepinart | 1 Jul 05:10 2007
Picon

Re: 200 dollar questions!

elis aeris wrote:
> I ran this:
>
[snip a lot of code]
In the future, please send big sections of code as attachments.
It is better in the regard that formatting is preserved as well as
us not having to scroll down past a bunch of code to see what the issue is.
Often, with just the traceback and an explanation of what the code is 
supposed to do,
we already have a general idea of what the problem is, and probably 
where it is as well,
and we can locate and explain the problem quickly.
If we have to look through all the code before we know what the issue 
is, chances are,
you will have fewer people willing to respond.
>
> What that does is to take a screen shot and then pixelgetcolor() over 
> 8000  (x,y)  points
No it doesn't.
It gets the same pixel 8000 times.
What exactly are you trying to accomplish here?
In addition, the PIL manual mentions specifically that the 
image.getpixel method is slow,
and if you need to process portions of the image you should go about it 
in a different manner.
>
> for me it clocked at 0.08 seconds and I am trying cut it down to maybe 
> 0.04 
For something that executes this fast, I would suggest using a more 
accurate method of timing it.
For example, using the timeit module and getting an average of 1,000 
iterations, or something of that nature.
>
> any hints on performance increase?
It depends what you're trying to do.  Give us more info and we can help.
-Luke
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

elis aeris | 1 Jul 05:30 2007
Picon

Re: 200 dollar questions!

Yes, I am trying to write a OCR algorithm with neural network, but the theories of NN is of little importance, what I am trying to get ready for the project is run speed.


the problem comes in a number of ways:


first, as you mentioned,   image.getpixel   is not very fast,  according to PIL
http://www.pythonware.com/products/pil/

so an alternative should be searched for,


also, I loop (x,y)

x = 0 -1023
y = 0- 768  through

and currently i use this:



x = 0
y = 0
for x in xrange(1, 1024, 1):
    for y in xrange(1, 768, 1):
        rgb = image.getpixel ((10, 12))





according to
http://www.python.org/doc/essays/list2str.html


a while loop is slow than for, and then map is faster.








as for that this:

image.getpixel ((10, 12))


currently i only use the shell window itself, so i can't really loop 1024*768, that's why it's only
10 12,

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Luke Paireepinart | 1 Jul 05:32 2007
Picon

Re: 200 dollar questions!

elis aeris wrote:
> Yes, I am trying to write a OCR algorithm with neural network, but the 
> theories of NN is of little importance, what I am trying to get ready 
> for the project is run speed.
Yes, but what you're trying to do _is_ important.
Example code that just loops over a small part of an image doesn't give 
us indication of what you're trying to do.
For example - consider your image is 1024x768  but you only want to 
process 100x100 block of it.
Then a good solution might be to use the image.crop method to reduce the 
image to only the part you care about,
and then doing a getdata() on it,
versus doing getpixel for every pixel in the 100x100 area.
>
>
> the problem comes in a number of ways:
>
>
> first, as you mentioned,   image.getpixel   is not very fast,  
> according to PIL
> http://www.pythonware.com/products/pil/
>
> so an alternative should be searched for,
>
>
> also, I loop (x,y)
>
> x = 0 -1023
> y = 0- 768  through
>
> and currently i use this:
>
>
>
> x = 0
> y = 0
> for x in xrange(1, 1024, 1):
>     for y in xrange(1, 768, 1):
>         rgb = image.getpixel ((10, 12))
These loops don't go 0-1023 and 0-768
they go
1-1023 and 1-767

There is a function called getdata() that you can use to get all of the 
pixel data of the image at once.
-Luke
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

elis aeris | 1 Jul 05:36 2007
Picon

Re: 200 dollar questions!

which modules and origin is this function?

is it the fastest way to get it?

I am told to optimize getpixel (x, y)  alone, so that's the only thing i am trying, it's part of a project that will go live in september, so the prof we are under hasn't one anything but some introductory readings.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
elis aeris | 1 Jul 05:46 2007
Picon

optimization: faster than for

I found this on the net, and it's a different arrangement to using for

the author claims it will result in faster performance, but i can't find documents on it, because I can't figure out which parts are parameters and which parts on special words, for python.

The operations with explicit loop counters have to be translated into Python byte code and must be
interpreted. Implicit loop counter can be incremented by the core code (translated from C sources). Try this:

import timeit

sWhile = """\
x = 0
y = 0
number_scanned = 0
while x < 1024:
    while y < 768:
        y = y + 1
        number_scanned = number_scanned + 1
    y = 0
    x = x + 1
"""
t = timeit.Timer(stmt=sWhile)
print "%.2f usec/pass" % (1000000 * t.timeit(number=10)/10)


sFor = """\
number_scanned = 0
for x in xrange(1024):
    for y in xrange(768):
        number_scanned = number_scanned + 1
"""
t = timeit.Timer(stmt=sFor)
print "%.2f usec/pass" % (1000000 * t.timeit(number=10)/10)


The sWhile and sFor are the multiline strings containing the equivalent code written using while and for
loop respectively.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gmane