-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-imagediff
executable file
·99 lines (87 loc) · 2.62 KB
/
simple-imagediff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# Simple Image Diffs
# ==================
#
# How to Install
# --------------
#
# Download the script somewhere on $PATH as 'simple-imagediff' with +x:
#
# $ cd ~/bin
# $ wget -O simple-imagediff https://raw.github.com/gist/1716699/simple-imagediff.py
# $ chmod +x simple-imagediff
#
# Prerequisites
# -------------
#
# The script should work out-of-the box on Ubuntu 11.10. On other OS'es you may
# need to install PIL and Gtk3.
#
# Git Setup
# ---------
#
# In ~/.gitconfig, add:
#
# [diff "image"]
# command = simple-imagediff
#
# In your project, create .gitattributes file and add (this enables the custom
# diff tool above):
#
# *.gif diff=image
# *.jpg diff=image
# *.png diff=image
#
# Try It
# ------
#
# $ git diff path/to/file.png
#
# NOTE: file.png must be versioned and the working copy must be different.
import os
import sys
from PIL import Image
from gi.repository import Gdk, Gtk
class SimpleImageDiffWindow(Gtk.Window):
def __init__(self, left, right):
Gtk.Window.__init__(self, title="Simple Image Diff (%s, %s)" % (left, right))
self.set_default_size(640, 480)
align = Gtk.Alignment()
align.set_padding(10, 10, 10, 10)
box = Gtk.HBox(homogeneous=True, spacing=10)
box.add(self._create_image_box(left))
box.add(self._create_image_box(right))
align.add(box)
self.add(align)
self.resize(1, 1)
self.set_position(Gtk.WindowPosition.CENTER)
accel = Gtk.AccelGroup()
for quit_key in ['Escape', '<Control>Q', '<Control>W']:
key, modifier = Gtk.accelerator_parse(quit_key)
accel.connect(key, modifier, Gtk.AccelFlags.VISIBLE, Gtk.main_quit)
self.add_accel_group(accel)
def _create_image_box(self, image_file):
box = Gtk.VBox(spacing=10)
frame = Gtk.Frame()
image = Gtk.Image()
image.set_from_file(image_file)
title = Gtk.Label(label="W: %dpx | H: %dpx" % Image.open(image_file).size)
frame.add(image)
box.pack_start(frame, True, True, 0)
box.pack_end(title, False, False, 10)
return box
def _halt(message, code):
sys.stderr.write("[ERROR] %s\n" % message)
sys.exit(0 << code)
def _verify_file_exists(target):
if not os.path.exists(target):
_halt("The file '%s' does not exists." % target, 2)
if __name__ == '__main__':
if len(sys.argv) < 3:
_halt('Not enough arguments.', 1)
_verify_file_exists(sys.argv[1])
_verify_file_exists(sys.argv[2])
app = SimpleImageDiffWindow(sys.argv[1], sys.argv[2])
app.connect('delete-event', Gtk.main_quit)
app.show_all()
Gtk.main()