Creating a Better Print Screen, part I
After reading Randall Degges excellent post How I learned to Program, I decided to give creating a screenshot program a try. It’s always a bit of work to take a screenshot, upload it to the web and then grab its permanent link. So being a geek, I thought it high time this process was automated!
I tried writing the program in python only, so that there were fewer dependencies, but using the commandline program scrot as the screenshot snapping program added a lot of flexibility to the options for the screenshot.
Without further ado, here are the parts of the program.
Take Screenshot with scrot
I start with a bash script that calls scrot, and passes in my program as a parameter to scrot.
screen-upload.bash
#!/bin/bash
/usr/local/bin/scrot '%m-%d-%Y-screenshot.png' -d 5 -u -e '/home/bart/projects/screen-upload/screen-upload.py $f'
Next, chmod +x the bash script:
chmod +x screen-upload.bash
Scrot Options explained
- -d 5: Delays the snap for 5 seconds.
- -u: Use the currently focused window.
- -e ‘script’: Execute the following script after the screenshot is snapped.
- $f: passes in filename to script.
On to the Python Picture Uploading Program
Scrot takes a screenshot after a delay of 5 seconds of the currently active window, it names the file with the current date-screenshot.png, it then passes the filename into my program, “screen-upload.py”.
Upload Image to Picasaweb
For uploading the image to Picasaweb, I used the gdata module. Installing it involved the standard: Download the module, extract it and cd into the directory and run “sudo python setup.py install”.
In version 2 of screen-upload, I will rewrite the authentication to use Oauth2, so that the authentication is more secure, for the alpha version, I am just passing in the users email and password into the gd_client object.
import gdata.photos.service
import gdata.media
import gdata.geo
filename = fname
gd_client = gdata.photos.service.PhotosService()
gd_client.email = username
gd_client.password = passwd
gd_client.source = 'screen-upload-1'
gd_client.ProgrammaticLogin()
print 'uploading', filename
album_url = '/data/feed/api/user/%s/albumid/%s' % (username, album_id)
photo = gd_client.InsertPhotoSimple(album_url, 'New Photo',
'Uploaded using the API', filename, content_type='image/png')
Copy URL into the Clipboard
Copying the URL to the clipboard is easy as long as you are not too concerned with cross-platform compatibility. I used the gtk module to gain access to the clipboard on Linux systems.
from gtk import Clipboard
def clip_store(pic_url):
cb = Clipboard()
cb.set_text(pic_url)
cb.store()
This can be made more cross-platform with a series of try statements that load platform specific libraries.
try:
import win32
...
try:
from gtk import Clipboard
Setting up the Keyboard Shortcut in Gnome 3.
I had a bit of a bother creating a keyboard shortcut in Gnome. I created a new shortcut in the keyboard settings configuration, but nothing happened.
Long story short: I created a shortcut under “Custom Shortcuts”. I called it “Take Screenshot” and I named the script “screen-upload”. Then I created a symbolic link to the script in /usr/local/bin.
ln -s ~/projects/screen-upload/screen-upload.bash /usr/local/bin/screen-upload.
This did the trick, now when I press the PrtSc button, a screenshot is taken of the current window, and its uploaded to Picasa and the URL of the image is copied in to my clipboard!
References
- Picasa-web Python Developer’s Guide for API v1
- Google Data API Authentication Guide
- Stackoverflow Answer for Taking Screenshot with python only
- Randall Degges: How I Learned to Program
- Scrot’s Github Repository