2
# $URL: http://pypng.googlecode.com/svn/trunk/code/pipwindow $
6
# Tool to crop/expand an image to a rectangular window. Come the
7
# revolution this tool will allow the image and the window to be placed
8
# arbitrarily (in particular the window can be bigger than the picture
9
# and/or overlap it only partially) and the image can be OpenGL style
10
# border/repeat effects (repeat, mirrored repeat, clamp, fixed
11
# background colour, background colour from source file). For now it
12
# only acts as crop. The window must be no greater than the image in
15
def window(tl, br, inp, out):
16
"""Place a window onto the image and cut-out the resulting
17
rectangle. The window is an axis aligned rectangle opposite corners
18
at *tl* and *br* (each being an (x,y) pair). *inp* specifies the
19
input file which should be a PNG image.
24
r = png.Reader(file=inp)
25
x,y,pixels,meta = r.asDirect()
26
if not (0 <= tl[0] < br[0] <= x):
27
raise NotImplementedError()
28
if not (0 <= tl[1] < br[1] <= y):
29
raise NotImplementedError()
30
# Compute left and right bounds for each row
31
l = tl[0] * meta['planes']
32
r = br[0] * meta['planes']
34
"""An iterator to perform the crop."""
36
for i,row in enumerate(pixels):
40
# Same as "raise StopIteration"
43
meta['size'] = (br[0]-tl[0], br[1]-tl[1])
44
w = png.Writer(**meta)
45
w.write(out, itercrop())
55
br = tuple(map(int, argv[:2]))
58
br = tuple(map(int, argv[2:4]))
59
if len(argv) in (2, 4):
62
f = open(argv[-1], 'rb')
64
return window(tl, br, f, sys.stdout)
66
if __name__ == '__main__':