Re: [Tutor] threading tutorial
On 01/06/17 16:30, Michael C wrote: > Oh i get it alright, however in my code I have to push the W button like > this: > > import pyautogui > import time > > pyautogui.keyDown('w') > time.sleep(2) > pyautogui.keyUp('w') So this emulates a user pressing the w key for 2 seconds. What's not clear is where this appears in your design, is it part of the code running in the thread or is it part of the code that stops the thread? If you can explain a little bit more of the high level requirement hee rather than the implementation perhaps we can come up with a better solution - ideally one that doesn't involve any keypress emulation at all... > while the example you gave: > > def fn(): >global run_me >while run_me: > ... do some work ... > > and then elsewhere you go: > > global run_me > run_me = True > ... create and start the Thread ... > ... later ... > run_me = False > T.join() > > theoretically deals with my problem, in practice though, my function spend > almost all its time holding down the 'w' button, The fact you say the function suggests you mean the thread. So the question is why does it need to hold the key down for so long? Indeed why does a background process need to emulate a button press? What is the button press doing? Is it in turn detected by some other process/thread? We need to understand how the various bits interact to give a better solution. > Is there a way to pause/kill the thread? Not really other than setting a flag, but if its the thread that's doing the sleeping then killing it won't help - in fact if you could kill it in the middle of the sleep() you'd have the problem of a key being "held down" forever - probably a bad thing... Thats why its better to have the thread kill itself on detection of the semaphore. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Pasting an image with transparency
Slackware 14.2 64-bit Python 2.7.13 I am trying to automate some photo processing by pasting a sig or watermark. The sig image is a .png with transparency but when it pastes it does so with a black background. Is there a way to paste with transparency? from PIL import Image from PIL import ImageEnhance fname = "sample.jpg" im = Image.open(fname) print(im.format, im.size, im.mode) out = im.resize((1068, 712)) enh = ImageEnhance.Sharpness(out) enh = enh.enhance(2.5) sig = Image.open("/home/tvbare/pics/recent_pics/sigs/opi_sig_landscape.png") print(sig.format, sig.size, sig.mode) box = (768, 616, 1018, 662) enh.paste(sig, box) enh.show() -- Terry "Hoots" To stay young, never lose your sense of wonder. * My main photo gallery can be seen at: http://www.flickr.com/photos/tvbare/> I also keep a secondary gallery at: http://www.facebook.com/pages/Osage-Plains-Images/300412117042> * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading tutorial
let me try that! thanks! On Thu, Jun 1, 2017 at 10:52 AM, Jerry Hill wrote: > On Thu, Jun 1, 2017 at 11:30 AM, Michael C > wrote: > > Oh i get it alright, however in my code I have to push the W button like > > this: > > > > import pyautogui > > import time > > > > pyautogui.keyDown('w') > > time.sleep(2) > > pyautogui.keyUp('w') > > ... > > > theoretically deals with my problem, in practice though, my function > spend > > almost > > all its time holding down the 'w' button, given how many miliseconds i > need > > it. and so it's not responsive enough > > for this reason. > > From that example, it looks like you spend almost all the time > sleeping, right? Maybe sleep for a shorter amount of time, in a loop > where you can check the flag? Something like: > > global run_me > time_to_sleep = 2 > time_asleep = 0 > > pyautogui.keyDown('w') > while run_me and (time_asleep < time_to_sleep): > delta = time_to_sleep/100 > time.sleep(delta) > time_asleep += delta > pyautogui.keyUp('w') > > That would let you check the flag more often, so you can clean up properly. > > -- > Jerry > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading tutorial
On Thu, Jun 1, 2017 at 11:30 AM, Michael C wrote: > Oh i get it alright, however in my code I have to push the W button like > this: > > import pyautogui > import time > > pyautogui.keyDown('w') > time.sleep(2) > pyautogui.keyUp('w') ... > theoretically deals with my problem, in practice though, my function spend > almost > all its time holding down the 'w' button, given how many miliseconds i need > it. and so it's not responsive enough > for this reason. >From that example, it looks like you spend almost all the time sleeping, right? Maybe sleep for a shorter amount of time, in a loop where you can check the flag? Something like: global run_me time_to_sleep = 2 time_asleep = 0 pyautogui.keyDown('w') while run_me and (time_asleep < time_to_sleep): delta = time_to_sleep/100 time.sleep(delta) time_asleep += delta pyautogui.keyUp('w') That would let you check the flag more often, so you can clean up properly. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading tutorial
Oh i get it alright, however in my code I have to push the W button like this: import pyautogui import time pyautogui.keyDown('w') time.sleep(2) pyautogui.keyUp('w') while the example you gave: def fn(): global run_me while run_me: ... do some work ... and then elsewhere you go: global run_me run_me = True ... create and start the Thread ... ... later ... run_me = False T.join() theoretically deals with my problem, in practice though, my function spend almost all its time holding down the 'w' button, given how many miliseconds i need it. and so it's not responsive enough for this reason. Is there a way to pause/kill the thread? thanks! On Thu, May 25, 2017 at 7:47 PM, Michael C wrote: > message received, i ll take a look tomorrow asap. > > thanks for replying!!! > > On Thu, May 25, 2017 at 3:03 PM, Cameron Simpson wrote: > >> On 25May2017 11:52, Michael C wrote: >> >>> Right now all i need is to grab 3 values from 3 variables before killing >>> a >>> thread, like this: >>> >>> def stuff(): >>> do stuff, >>>get values, (x,y,d) >>> >>> # main code >>> startthread(stuff(), blah) >>> # if else need to sleep or kill the thread, and because I'll restart the >>> thread later, I'd like to get the values from the thread, say x,y,d in >>> order to restart the thread. >>> loop. >>> >>> Therefore, how do I get a few values from a few variables from the thread >>> and then close it? >>> >>> Threading is very new to me, so I have to be very diligent. >>> >> >> You always need to be diligent with threads :-) >> >> Can you explain why you need to use a thread for this? The first cut of >> your program looks like you could do it with a ordinary function: >> >> def stuff(): >>... compute x, y, z ... >>return x, y, z >> >> def main(): >>x, y, z = stuff() >> >> OTOH, your later description suggests that you want to kick off a thread >> to work on something, and have your main program let it run, or pause it. >> The implication is that x, y, z represent the thread state allowing you to >> restart it from scratch at the same point where you paused/stopped things. >> >> There are a few different ways to manage that scenario. But first, write >> yourself a small Thread based program to familiarise yourself with threads. >> For example (untested): >> >> from __future__ import print_function >> from time import sleep >> from threading import Thread >> >> def thread_main_body(): >>print("thread started") >>for n in range(20): >> print("thread", n) >> sleep(0.3) >>print("thread done") >> >> def main(): >>print("main") >>T = Thread(target=thread_main_body) >>print("main: Thread created but _not_ started") >>sleep(1) >>T.start() >>print("thread started") >>for n in range(10): >> print("main", n) >> sleep(0.4) >>print("main program waiting for thread") >>T.join() >>print("main program done") >> >> You should see the main thread and your subthread outputs interleaved. >> The sleeps are just to ensure some interleaving and to give good >> interactive feel. It should run for about 8 seconds overall. Make sure >> you're happy you understand what is happening, why, and when. >> >> There are a few things you need to keep in mind with threads (in Python, >> and to a degree in other languages): >> >> 1: You can't kill/stop a Thread. Instead, the usual approach to to share >> some state with some kind of "running" flag, a boolean saying that the >> Thread's function should continue. Then the thread polls that regularly. >> Eg, if the thread function runs a main loop it might look like this: >> >> def fn(): >>global run_me >>while run_me: >> ... do some work ... >> >> and then elsewhere you go: >> >> global run_me >> run_me = True >> ... create and start the Thread ... >> ... later ... >> run_me = False >> T.join() >> >> so effectively you ask the Thread to stop, and it obeys when it notices >> the change to "run_me". Using a global for this is pretty crube, and not >> the general approach, BTW. >> >> 2: Like any other function, the local varaibles to the thread function >> are not available outside. Thus the "global" hack above. So to share state >> you would usually make some kind of object with the state, and pass it in >> to the Thread when you create and start it: >> >> def fn(state): >>while state.run_me: >> ... do stuff, keep important things like results in "state" ... >> state.x = 1 >> state.y = whatever >> >> class State(object): >>pass >> >> def main(): >>state = State() >>state.run_me = True >>T = Thread(target=fn, args=(state,)) >>T.start() >>for n in range(10): >> print("main", n, "x =", state.x, "y =", state.y) >> sleep(0.3) >>state.run_me = False >>T.join() >> >> As I remarked, there are a few ways to approach your scenario. The above >> should get you started on one approach. Pausing can be done in a few ways, >>