Hi guys

I have been making a program over the last few days that grabs an image and re-paints it in paint in 256 colour. The main thing this program is for is so you can watch it getting painted in paint.

It works perfectly on my computer but i find that as soon as i take it to faster computers the whole thing goes haywire. It just stuffs up the whole image becuase the program seems to just get ahead of itself. I am finding it difficult to debug as i do not have access to a computer that is fast enough to make is stuff up and so i was wondering if anybody out there with a fast computer is willing to lend a hand and help find a way to fix it so nothing goes wrong.

Note that you will need the Win32 extensions to python installed as well as PyHook so the program can exit when you press the Q key (to do this try it while it is changing colour otherswise it sometimes wont work). You will also need Sendkeys.

Any help would be appreciated!

Attachments
import win32api
import win32con
from ctypes import windll
import time
import os
from SendKeys import SendKeys as s
import Image as i
import winsound
from threading import Thread
import sys
import pyHook as ph
import pythoncom, pyHook
import wx


def m(x,y):
    windll.user32.SetCursorPos(x,y)

def l(x="current", y="current",keepdown = 1):
    if x == "current" and y == "current":
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)

        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    elif keepdown == 1:
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)
    
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y)
    

def r(x="current", y="current"):
    if x == "current" and y == "current":
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
        
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
    else:
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, x, y)
      
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, x, y)

        

class Get_Pic(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,title = "Choose Picture")
        self.panel = wx.Panel(self)
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.text = wx.StaticText(self,label = 'Please Pick a picture')
        self.butload = wx.Button(self,-1,label = 'Load Picture')
        self.butload.Bind(wx.EVT_LEFT_DOWN,self.get)
        self.Bind(wx.EVT_CLOSE,self.exiting)

        self.vbox.Add(self.text,proportion=0,border = wx.SUNKEN_BORDER)
        self.vbox.Add(self.butload,proportion=1,flag = wx.EXPAND)
        self.SetSizer(self.vbox)
    
    
    
        self.Show()
    def exiting(self,event):
        sys.exit()
    def get(self,event):
        loadbox = wx.FileDialog(self,message='open',
                                defaultDir=os.getcwd(),
                                defaultFile='',
                                wildcard = 'Image Files|*.bmp;*.jpg;*.png',
                                style = wx.OPEN)
        if loadbox.ShowModal() == wx.ID_OK:
            self.file = loadbox.GetPath()
            global path
            path = self.file
            global f
            f = i.open(self.file)
     
        
            os.startfile(r'mspaint.exe')
            time.sleep(2)
            s('%F')
            s('O')
            s('%N')
            fil = self.file
            fil = fil.replace(' ','{SPACE}')
        
            s(str(fil))
            s('{ENTER}')
            s('%F')
            s('A')
            s('%N')
            cur = os.getcwd()
            cur = cur.replace(' ','{SPACE}')
            t = open('debug.txt','w')
            t.write(str(cur))
            t.close()
            s(str(cur))
           
            s('\\template.bmp')
            s('%t')
            s('{DOWN}')
            s('2')
              
            s('{ENTER}')
            s('{ENTER}')
            s('Y')
            s('Y')
            s('%F')
            s('a')
            s('%t')
            s('{DOWN}')
            s('{DOWN}')
            s('{ENTER}')
            s('{ENTER}')
            s('Y')
            
        loadbox.Destroy()
      
        self.Destroy()
        
app = wx.App(False)
g = Get_Pic()
app.MainLoop()

def OnKeyboardEvent(event):
  
  
  if event.Key == 'Q':
      sys.exit()
  
  


  return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever


class Stop(Thread):
    def __init__(self):
      
        Thread.__init__(self)
    def run(self):
        pythoncom.PumpMessages()
s("%{F4}")
os.startfile(r'mspaint.exe')
time.sleep(2)
m(150,150)
st = Stop()
st.start()



m(20,140)
l()
m(61,51)
x = 61
y = 51
oldr = 999
oldg = 999
olrb = 999
f = i.open('template.bmp')
size = f.size

colours = f.getcolors()

s('^e')
s('%p')
s('%w')

send = size[0]

s(str(send))
sennd = str(size[1])
s('%h')
s(sennd)
s('{ENTER}')
imagedata = {}


for col in f.getdata():
 
    if str(col) not in imagedata.keys():
        imagedata[str(col)] = [[(x,y)]]
    
    else:
        imagedata[str(col)][0]+=[(x,y)]
    x+=1

    

    if x==61+size[0]:
        y+=1
        x=61

print "Time approx:",((size[0]*size[1])*0.003)
for f in range(int(((size[0]*size[1])*0.003)/60)):
    winsound.Beep(1000,10)
    time.sleep(0.001)
max_one = max([len(imagedata[g][0]) for g in imagedata.keys()])
max_c = 0

for z in imagedata.keys():
    if len(imagedata[z][0]) == max_one:
     
        
        max_c = z

temp2 = max_c.split(',')
temp = []
for x in temp2:
    x = x.replace('[','')
    x = x.replace(']','')
    x= x.replace('(','')
    x = x.replace(')','')
    temp.append(x)
r = temp[0]
g = temp[1]
b= temp[2]


s('%c')
s('e')
s('%d')
s('%r')
s(str(r))
s('%g')
s(str(g))
s('%u')
s(str(b))
s('{ENTER}')
s('{ENTER}')
l(62,52)
m(35,80)
l()
m(62,52)
l()
m(20,140)
l()
m(61,51)
nono = (str(r),str(g),str(b))
print nono
temp = []
temp2=''
r=0
g=0
b=0
for key in imagedata.keys():
    
    temp2 = key.split(',')
    temp = []
    for x in temp2:
        x = x.replace('[','')
        x = x.replace(']','')
        x= x.replace('(','')
        x = x.replace(')','')
        temp.append(x)
    
    
    
   
    temp = tuple(temp)
  
    r = temp[0]
    g = temp[1]
    b = temp[2]

    for pos in list(imagedata[str(key)]):
        for e in pos:
            
            if temp != nono:
      
                m(e[0],e[1])
                l()
                time.sleep(.000001)
                
             
                    
                  
                
            if oldr == r and oldg == g and oldb == b:
                pass
            else:
                s("%c")
                s("E")
                s("%D")
                s("%R")
                s(r)
                s('%G')
                s(g)
                s('%u')
                s(b)
                s('{ENTER}')
            
            
           
            oldr = r
            oldg = g
            oldb = b
winsound.PlaySound('Windows XP Print complete.wav',1)
time.sleep(2)
s("%f")
s("A")
s('%n')
if os.path.exists(str(os.getcwd()+r"\Finished Pictures"))==False:
    os.system('mkdir "%s"'%(os.getcwd()+r"\Finished Pictures"))
filen = os.getcwd()+r"\Finished Pictures"+'\\'+path.split('\\')[-1]
filen = str(filen)
filen = filen.replace(' ','{SPACE}')
s(str(filen))
s("{ENTER}")
s("y")


sys.exit()

I am not trying to do that.
What i want to do is have it so you supply an image in the program and the program then draws it in paint. I really dont want to just shortcut and re-save it.

The point of the program is to see the actual image being drawn in paint. If you want just grab the source code i have attached to the previous post and have a look.

Sorry to double post and all its just i thought it might be useful to people if i posted the links to all of the modules this code needs so they could run it.

Win32 modules - python.net/crew/mhammond/win32/Downloads.html

Sendkeys - http://www.rutherfurd.net/python/sendkeys/

pyHook - http://sourceforge.net/project/showfiles.php?group_id=65529

wxPython - http://www.wxpython.org/download.php

Python Imaging Libary - http://www.pythonware.com/products/pil/

Sorry there is so many modules that people might not have.
Oh i will also attach the .py file this time in case people want to help. It is in the zip file i attach.

I ran your code and it took over my computer. It took me forever to get it back to normal again. There is something wrong with your application of sendkeys or pyhook!

To tell you the truth, I won't even try to figure it out.

Oh i'm so sorry Ene. The program uses the mouse to draw the picture. I should have said that above. Also it is best to try a small picture otherwise it will take some time to do. The amount of beeps will tell you how many minutes. Anyhow dont worry about it. I think i should do some more coding before i post it again. Thanks for trying though and sorry!

This question has already been answered. Start a new discussion instead.