Created
March 3, 2012 16:18
-
-
Save lichray/1966850 to your computer and use it in GitHub Desktop.
A simple screenshoter (python + ImageMagick)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Zhihao Yuan <lichray at gmail.com> | |
SSCMD = 'import -quality 04 -border -frame png:-' | |
SSCMD_ALT = 'import -quality 04 png:-' | |
from cStringIO import StringIO | |
import sys, os | |
from getopt import getopt | |
import pygtk | |
pygtk.require20() | |
import gtk, cairo | |
if __name__ == '__main__': | |
cmd = SSCMD | |
for k, v in getopt(sys.argv[1:], 'hn')[0]: | |
if k == '-h': | |
print '''%s: [options]... | |
options: | |
-n no window frame | |
-h display this help''' % os.path.basename(sys.argv[0]) | |
sys.exit(1) | |
elif k == '-n': | |
cmd = SSCMD_ALT | |
shot = os.popen(cmd).read() | |
sfce = cairo.ImageSurface.create_from_png(StringIO(shot)) | |
def expose(area, event): | |
ctx = area.window.cairo_create() | |
ctx.set_source_surface(sfce, 0, 0) | |
ctx.paint() | |
dragfrom = [0.0, 0.0] | |
def pressat(x, y): | |
dragfrom[0] = x | |
dragfrom[1] = y | |
def scrollto(area, event, scroll): | |
h, v = scroll.get_hscrollbar(), scroll.get_vscrollbar() | |
h.set_value(h.get_value() + dragfrom[0] - event.x) | |
v.set_value(v.get_value() + dragfrom[1] - event.y) | |
chooser = gtk.FileChooserDialog('Save the PNG file...', | |
action=gtk.FILE_CHOOSER_ACTION_SAVE, | |
buttons=(gtk.STOCK_SAVE, gtk.RESPONSE_OK)) | |
try: | |
chooser.set_icon(gtk.icon_theme_get_default() | |
.load_icon("camera", 48, gtk.ICON_LOOKUP_FORCE_SVG)) | |
except: pass | |
chooser.set_do_overwrite_confirmation(1) | |
chooser.set_current_folder(os.getcwd()) | |
preview = gtk.DrawingArea() | |
preview.set_size_request(sfce.get_width(), sfce.get_height()) | |
preview.connect('expose-event', expose) | |
wrapper = gtk.ScrolledWindow() | |
wrapper.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) | |
wrapper.add_with_viewport(preview) | |
wrapper.show_all() | |
chooser.vbox.pack_start(wrapper) | |
chooser.vbox.reorder_child(wrapper, 0) | |
filter = gtk.FileFilter() | |
filter.add_mime_type('image/png') | |
chooser.set_filter(filter) | |
preview.set_events(gtk.gdk.POINTER_MOTION_MASK | |
| gtk.gdk.BUTTON_PRESS_MASK | |
| gtk.gdk.BUTTON_RELEASE_MASK) | |
scrollto_id = preview.connect('motion-notify-event', scrollto, wrapper) | |
preview.handler_block(scrollto_id) | |
preview.connect('button-press-event', lambda w, e: | |
e.button == 1 and e.type == gtk.gdk.BUTTON_PRESS | |
and preview.handler_unblock(scrollto_id) or pressat(e.x, e.y)) | |
preview.connect('button-release-event', lambda w, e: | |
e.button == 1 and e.type == gtk.gdk.BUTTON_RELEASE | |
and preview.handler_block(scrollto_id)) | |
preview.connect('realize', lambda w: | |
w.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))) | |
if chooser.run() == gtk.RESPONSE_OK: | |
with open(chooser.get_filename(), 'wb') as f: | |
f.write(shot) | |
chooser.destroy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment