summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2007-06-15 21:49:21 +0200
committerGuillaume Seguin <guillaume@segu.in>2007-06-15 21:49:21 +0200
commit12355201a2631d61f8d3defd2e1e3d5ead12474c (patch)
tree094170d1a87af3b2836c793a0ea86b99d9551cdf
parent055ddaba036703dc05c327a8ee9b78a6e119b75a (diff)
downloadscreenshot-beautifier-12355201a2631d61f8d3defd2e1e3d5ead12474c.tar.gz
screenshot-beautifier-12355201a2631d61f8d3defd2e1e3d5ead12474c.tar.bz2
* Add some comments, refactor shadows code
-rwxr-xr-xbeautifier.py65
1 files changed, 37 insertions, 28 deletions
diff --git a/beautifier.py b/beautifier.py
index e0eb656..36a3cee 100755
--- a/beautifier.py
+++ b/beautifier.py
@@ -3,7 +3,7 @@
import cairo
from math import pi
from sys import argv
-
+from os.path import exists
class Color:
def __init__ (self, r = 1, g = 1, b = 1, a = 1):
self.r = r
@@ -25,9 +25,7 @@ def cairo_surface_gradient (width, height, start, end):
return surface
-def save_surface (surface, file):
- surface.write_to_png (file)
-
+# Draw a rectangle path
def draw_rectangle (cr, x0, y0, x1, y1):
cr.new_path ()
cr.move_to (x0, y0)
@@ -36,6 +34,7 @@ def draw_rectangle (cr, x0, y0, x1, y1):
cr.line_to (x0, y1)
cr.close_path ()
+# Draw a rounded rectangle path
def draw_rounded_rectangle (cr, radius, x0, y0, x1, y1):
cr.new_path ()
cr.arc (x0 + radius, y1 - radius, radius, pi / 2, pi)
@@ -62,11 +61,13 @@ def cairo_trim (surface, radius = 0.10):
draw_rectangle (cr, 0.01, 0.01, 0.99, 0.99)
cr.fill ()
+# Load and trim an image
def cairo_prepare_surface (file, radius):
surface = cairo.ImageSurface.create_from_png (file)
cairo_trim (surface, radius)
return surface
+# Run the whole drawing process
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)
@@ -92,43 +93,48 @@ def cairo_beautify (file, radius = 0.1, ratio = 1, xy = 0.0, yx = 0.1):
cr.set_source (linear)
cr.paint ()
# Prepare background
- bgwidth = int (1.4 * width)
- bgheight = int (1.8 * height)
+ bgwidth = int (1.2 * width)
+ bgheight = int (1.5 * height)
topcolor = Color (1, 1, 1, 1)
- bottomcolor = Color (0.8, 0.8, 0.8, 1)
+ bottomcolor = Color (0.9, 0.9, 0.9, 1)
result = cairo_surface_gradient (bgwidth, bgheight, topcolor, bottomcolor)
- # Paint image
cr = cairo.Context (result)
cr.set_operator (cairo.OPERATOR_OVER)
+ # Shadows
+ x0 = 0.1 * width
+ y0 = 1.13 * height
+ x1 = 1.09 * width
+ y1 = 1.13 * height + yx * width
+ x2 = x1 + 0.2 * width
+ y2 = y1 - 0.13 * height
+ x3 = x0 + 0.2 * width
+ y3 = y0 - 0.13 * height
+ linear = cairo.LinearGradient ((x0 + x1) / 2, (y0 + y1) / 2,
+ (x2 + x3) / 2, (y2 + y3) /2)
+ 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 (x1, y1)
+ cr.line_to (x2, y2)
+ cr.line_to (x3, y3)
+ cr.close_path ()
+ cr.fill ()
+ # Paint image
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.set_source_surface (surface, 0.1 * width, 0.11 * height)
cr.paint ()
cr.set_matrix (old_matrix)
# Paint reflection
- y0 = 1.09 * height + ratio * height
+ y0 = 0.89 * 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.set_source_surface (reflect_surface, 0.1 * 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 ():
@@ -139,9 +145,12 @@ if __name__ == "__main__":
if len (argv) < 2:
usage ()
file = argv[1]
+ if not exists (file):
+ print "Source file not found!"
+ raise SystemExit
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)
+ s.write_to_png (out)