summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2007-06-15 21:17:06 +0200
committerGuillaume Seguin <guillaume@segu.in>2007-06-15 21:17:06 +0200
commit055ddaba036703dc05c327a8ee9b78a6e119b75a (patch)
tree65eb18decfa53245e69963958e7b264517a80b50
downloadscreenshot-beautifier-055ddaba036703dc05c327a8ee9b78a6e119b75a.tar.gz
screenshot-beautifier-055ddaba036703dc05c327a8ee9b78a6e119b75a.tar.bz2
* Initial import
-rwxr-xr-xbeautifier.py147
1 files changed, 147 insertions, 0 deletions
diff --git a/beautifier.py b/beautifier.py
new file mode 100755
index 0000000..e0eb656
--- /dev/null
+++ b/beautifier.py
@@ -0,0 +1,147 @@
+#!/usr/bin/python
+
+import cairo
+from math import pi
+from sys import argv
+
+class Color:
+ def __init__ (self, r = 1, g = 1, b = 1, a = 1):
+ self.r = r
+ self.g = g
+ self.b = b
+ self.a = a
+
+def cairo_surface_gradient (width, height, start, end):
+ surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
+ cr = cairo.Context (surface)
+ cr.scale (width, height)
+
+ linear = cairo.LinearGradient (0, 0, 0, 1)
+ linear.add_color_stop_rgba (0, start.r, start.g, start.b, start.a)
+ linear.add_color_stop_rgba (1, end.r, end.g, end.b, end.a)
+
+ cr.set_source (linear)
+ cr.paint ()
+
+ return surface
+
+def save_surface (surface, file):
+ surface.write_to_png (file)
+
+def draw_rectangle (cr, x0, y0, x1, y1):
+ cr.new_path ()
+ cr.move_to (x0, y0)
+ cr.line_to (x1, y0)
+ cr.line_to (x1, y1)
+ cr.line_to (x0, y1)
+ cr.close_path ()
+
+def draw_rounded_rectangle (cr, radius, x0, y0, x1, y1):
+ cr.new_path ()
+ cr.arc (x0 + radius, y1 - radius, radius, pi / 2, pi)
+ cr.line_to (x0, y0 + radius)
+ cr.arc (x0 + radius, y0 + radius, radius, pi, 3 * pi / 2)
+ cr.line_to (x1 - radius, y0)
+ cr.arc (x1 - radius, y0 + radius, radius, 3 * pi / 2, 2 * pi)
+ cr.line_to (x1, y1 - radius)
+ cr.arc (x1 - radius, y1 - radius, radius, 0, pi / 2)
+ cr.close_path ()
+
+# cairo_trim_image was originally written by Mirco "MacSlow" Muller in C
+def cairo_trim (surface, radius = 0.10):
+ width = surface.get_width ()
+ height = surface.get_height ()
+ cr = cairo.Context (surface)
+ cr.scale (width, height)
+ pattern = cairo.SolidPattern (0, 0, 0, 1)
+ cr.set_operator (cairo.OPERATOR_DEST_IN)
+ cr.set_source (pattern)
+ if radius > 0:
+ draw_rounded_rectangle (cr, radius, 0.01, 0.01, 0.99, 0.99)
+ else:
+ draw_rectangle (cr, 0.01, 0.01, 0.99, 0.99)
+ cr.fill ()
+
+def cairo_prepare_surface (file, radius):
+ surface = cairo.ImageSurface.create_from_png (file)
+ cairo_trim (surface, radius)
+ return surface
+
+def cairo_beautify (file, radius = 0.1, ratio = 1, xy = 0.0, yx = 0.1):
+ # Load and trim image
+ surface = cairo_prepare_surface (file, radius)
+ width = surface.get_width ()
+ height = surface.get_height ()
+ # Create reflected surface
+ reflect_surface = cairo.ImageSurface (cairo.FORMAT_ARGB32,
+ width, height)
+ cr = cairo.Context (reflect_surface)
+ cr.set_source_surface (surface)
+ cr.paint_with_alpha (1)
+ # Apply reflection gradient
+ cr.set_operator (cairo.OPERATOR_OVER)
+ cr.scale (width, height)
+ if radius > 0:
+ draw_rounded_rectangle (cr, radius, 0.01, 0.01, 0.99, 0.99)
+ else:
+ draw_rectangle (cr, 0.01, 0.01, 0.99, 0.99)
+ cr.clip ()
+ linear = cairo.LinearGradient (0, 0, 0, 1)
+ linear.add_color_stop_rgba (0, 1, 1, 1, 0.5)
+ linear.add_color_stop_rgba (1, 1, 1, 1, 0.7)
+ cr.set_source (linear)
+ cr.paint ()
+ # Prepare background
+ bgwidth = int (1.4 * width)
+ bgheight = int (1.8 * height)
+ topcolor = Color (1, 1, 1, 1)
+ bottomcolor = Color (0.8, 0.8, 0.8, 1)
+ result = cairo_surface_gradient (bgwidth, bgheight, topcolor, bottomcolor)
+ # Paint image
+ cr = cairo.Context (result)
+ cr.set_operator (cairo.OPERATOR_OVER)
+ old_matrix = cr.get_matrix ()
+ matrix = cairo.Matrix (xy = xy, yx = yx)
+ cr.transform (matrix)
+ cr.set_source_surface (surface, 0.2 * width, 0.11 * height)
+ cr.paint ()
+ cr.set_matrix (old_matrix)
+ # Paint reflection
+ y0 = 1.09 * height + ratio * height
+ matrix = cairo.Matrix (xy = - xy, yx = yx, yy = - ratio, y0 = y0)
+ cr.transform (matrix)
+ cr.set_source_surface (reflect_surface, 0.2 * width)
+ cr.paint_with_alpha (0.4)
+ cr.set_matrix (old_matrix)
+ # Shadows!
+ x0 = 1.19 * width
+ y0 = 1.13 * height + yx * width
+ x1 = x0 + 0.2 * width
+ y1 = y0 - 0.13 * height
+ y2 = 2 * y0 - y1
+ linear = cairo.LinearGradient (x0 + 0.1 * width, y0, x0, y1)
+ linear.add_color_stop_rgba (0, 0.5, 0.5, 0.5, 0.4)
+ linear.add_color_stop_rgba (1, 0.8, 0.8, 0.8, 0.3)
+ cr.set_source (linear)
+ cr.new_path ()
+ cr.move_to (x0, y0)
+ cr.line_to (x0, y1)
+ cr.line_to (x1, y0 - 0.1 * height)
+ cr.close_path ()
+ cr.fill ()
+ return result
+
+def usage ():
+ print "Usage : %s <source png> <destination png>" % argv[0]
+ raise SystemExit
+
+if __name__ == "__main__":
+ if len (argv) < 2:
+ usage ()
+ file = argv[1]
+ if len (argv) > 2: out = argv[2]
+ else: out = ".".join (file.split (".")[:-1]) + ".thumb.png"
+ if len (argv) > 3: radius = float (argv[3])
+ else: radius = 0
+ s = cairo_beautify (file, radius = radius)
+ save_surface (s, out)