SRL Archive - Python/Perl Help and Tutorials

[Python] Screenshot taker and uploader


bobng

2008-11-03 16:28:06

I got bored of taking screen shots using the keyboard, then pasting it in paint and uploading it myself. So I made this script to do it for me.
It runs in the background, and will take a screenshot when you press shift+F11, save it, play a notification sound (One of vistas default ones) then upload it to imageshack, and then it even opens a browser window with the direct link to the image.

Save it as a .pyw file - so when you run it it will not open a console window.

It requires python 2.6, PIL and the Win32 extensions.

Downloads:
Win32 extensions: http://sourceforge.net/projects/pywin32/
PIL: http://www.pythonware.com/library/
Python: http://www.python.org

Code:
import ImageGrab
import urllib, httplib, mimetypes,webbrowser,re,os
from winsound import PlaySound, SND_FILENAME, SND_ASYNC
def screenshot():
ImageGrab.grab().save('screenshot.jpg')
def post_multipart(host, port, selector, fields, files):
content_type, body = encode_multipart_formdata(fields, files)
h = httplib.HTTP(host, port)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()

def encode_multipart_formdata(fields, files):
BOUNDARY = '---------------------------13049614110900'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s";filename="%s"' % (key, filename))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body

def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or'application/octet-stream'

params = [('MAX_FILE_SIZE', '3145728'), ('refer','http://reg.imageshack.us/v_images.php')]
files = [('fileupload', 'screenshot.jpg', open('screenshot.jpg', 'rb').read())]
z = post_multipart('imageshack.us', 80, '/index.php', params, files)
for line in z.split('\n'):
if line.rstrip().lstrip().startswith("""<input type="text" onClick="track('direct');highlight(this)" style="width: 500px"""):
webbrowser.open(re.search('http://(.*?).imageshack.us/(.*?)/(.*?)/(.*?).jpg',line).group(),1,0)
os.remove('screenshot.jpg')

import threading,win32con,ctypes
from ctypes import wintypes
user32 = ctypes.windll.user32
byref = ctypes.byref
HOTKEYS = {
1 : (122,win32con.MOD_SHIFT),
}
for id, (vk, modifiers) in HOTKEYS.items ():
if not user32.RegisterHotKey (None, id, modifiers, vk):
PlaySound('C:\\Windows\\\Media\\Windows Hardware Fail.wav',SND_FILENAME|SND_ASYNC)
exit()

try:
msg = wintypes.MSG()
while user32.GetMessageA(byref(msg),None,0,0) != 0:
if msg.message == win32con.WM_HOTKEY:
try:
PlaySound('C:\\Windows\\\Media\\Windows Battery Low.wav',SND_FILENAME|SND_ASYNC)
screenshot()
except:
PlaySound('C:\\Windows\\\Media\\Windows Ding.wav',SND_FILENAME|SND_ASYNC)
user32.TranslateMessage(byref(msg))
user32.DispatchMessageA(byref(msg))
finally:
PlaySound('C:\\Windows\\\Media\\Windows Ding.wav',SND_FILENAME|SND_ASYNC)
user32.UnregisterHotKey (None, 1)


Kasi

2008-11-03 16:37:07

There are easyer ways of doing this for example for taking screenshots of the mm for map walking would be like this but mine takes screen shots as u play it
ctrl + alt + R
to run


Procedure SaveMM;
var
MMBMP, A : integer;
SFile : String;
Begin
Repeat
Inc(A);
If Not FileExists(ScriptPath + 'MM'+IntToStr(A)+'.bmp') Then Break;
Until(Not FileExists(ScriptPath + 'MM'+IntToStr(A)+'.bmp'));
SFile := ScriptPath + 'MM'+IntToStr(A)+'.bmp';
CopyClientToBitmap(MMBMP,519,0,764,167); // coords of MM could be changed
// e.g. inv for items in rs
SaveBitmap(MMBMP, SFile);
Writeln('Saved to '+ScriptPath+'MM'+IntToStr(A)+'.bmp');
End;
any Way still looks good

Edit : This Doesnt upload could be made to…

bobng

2008-11-03 16:44:33

This does not run in the background (Like a windows service), or respond to hotkeys. If you are trying to screenshot a your desktop, you don't want scar in the way! The advantage is the ability to select an area to save, and that's pretty cool. Also, it doesn't open a browser window and load the uploaded image URL.

noidea

2008-11-03 16:57:51

From: bobng
This does not run in the background (Like a windows service), or respond to hotkeys. If you are trying to screenshot a your desktop, you don't want scar in the way! The advantage is the ability to select an area to save, and that's pretty cool. Also, it doesn't open a browser window and load the uploaded image URL.



In SCAR there is function that Minimizes SCAR. It's like GetSelf.Hide; or some thing like that? Then you can tel SCAR to hold down the hotkeys to bring it back up.

bobng

2008-11-03 17:02:45

From: noidea
In SCAR there is function that Minimizes SCAR. It's like GetSelf.Hide; or some thing like that? Then you can tel SCAR to hold down the hotkeys to bring it back up.


It still does not run in the background, like a service.

I could also make the upload threaded, so you can take multiple snapshots then make a thread for each one which uploads it.
Can scar do that?

noidea

2008-11-03 17:05:47

From: bobng
It still does not run in the background, like a service.

It could. Just use that GetSelf.Hide; and have like a
repeat
wait(150)
until isfkey(12);//or w/e this proc is
TakeScreenyCrap;

Edit: Here, like this…
Procedure SaveMM;
var
MMBMP, A : integer;
SFile : String;
Begin
getself.hide;//or w/e
repeat
repeat
wait(150)
until isfkey(12);//or w/e
Inc(A);
If Not FileExists(ScriptPath + 'MM'+IntToStr(A)+'.bmp') Then Break;
Until(Not FileExists(ScriptPath + 'MM'+IntToStr(A)+'.bmp'));
SFile := ScriptPath + 'MM'+IntToStr(A)+'.bmp';
CopyClientToBitmap(MMBMP,519,0,764,167); // coords of MM could be changed
// e.g. inv for items in rs
SaveBitmap(MMBMP, SFile);
Writeln('Saved to '+ScriptPath+'MM'+IntToStr(A)+'.bmp');
until false
End;

I didn't compile this or anything so I doubt it would work…

bobng

2008-11-03 17:09:18

From: noidea
It could. Just use that GetSelf.Hide; and have like a
ScarScript:By Drunkenoldma [LEFT]repeat wait(150) until isfkey(12);//or w/e this proc is TakeScreenyCrap; [/LEFT]



It still doesn't run in the background, like service (Hint: Without a window)

noidea

2008-11-03 17:12:54

From: bobng
It still doesn't run in the background, like service (Hint: Without a window)


Yes it will. GetSelf.hide; puts SCAR into the task bar thing next to the clock.

bobng

2008-11-03 17:50:04

Fair enough. Make it upload and respond to a hotkey then I will be impressed.

noidea

2008-11-03 18:09:08

Thats what the script I/that other guy posted does. I havent tried to compile it though so there are errors.

Eddit: nvm, it doesn't upload it. It just saves it and and responds to the hotkey.