2 |
2 |
3 """ |
3 """ |
4 Python clone of the pngtile binary. |
4 Python clone of the pngtile binary. |
5 """ |
5 """ |
6 |
6 |
7 import optparse |
7 import optparse, sys |
8 |
8 |
9 import pypngtile as pt |
9 import pypngtile as pt |
10 |
10 |
11 # CLI options |
11 # CLI options |
12 options = None |
12 options = None |
13 |
13 |
|
14 def log (fmt, args) : |
|
15 print >>sys.stderr, fmt % args |
|
16 |
14 def log_debug (fmt, *args) : |
17 def log_debug (fmt, *args) : |
15 if options.debug or options.verbose : |
18 if options.debug or options.verbose : |
16 print fmt % args |
19 log(fmt, args) |
17 |
20 |
18 def log_info (fmt, *args) : |
21 def log_info (fmt, *args) : |
19 if not options.quiet : |
22 if not options.quiet : |
20 print fmt % args |
23 log(fmt, args) |
21 |
24 |
22 def log_warn (fmt, *args) : |
25 def log_warn (fmt, *args) : |
23 print fmt % args |
26 log(fmt, args) |
24 |
27 |
25 |
28 |
26 def parse_hex (hex) : |
29 def parse_hex (hex) : |
27 """ |
30 """ |
28 Parse a 0xHH.. style string as a raw bytestring |
31 Parse a 0xHH.. style string as a raw bytestring |
49 parser.add_option('-v', "--verbose", help="Display more output", action='store_true') |
52 parser.add_option('-v', "--verbose", help="Display more output", action='store_true') |
50 parser.add_option('-D', "--debug", help="Equivalent to -v", action='store_true') |
53 parser.add_option('-D', "--debug", help="Equivalent to -v", action='store_true') |
51 parser.add_option('-U', "--force-update", help="Unconditionally update the image caches", action='store_true') |
54 parser.add_option('-U', "--force-update", help="Unconditionally update the image caches", action='store_true') |
52 parser.add_option('-N', "--no-update", help="Do not update the image caches, even if unusable", action='store_true') |
55 parser.add_option('-N', "--no-update", help="Do not update the image caches, even if unusable", action='store_true') |
53 parser.add_option('-B', "--background", help="Background pattern for sparse cache file", metavar="0xHH..") |
56 parser.add_option('-B', "--background", help="Background pattern for sparse cache file", metavar="0xHH..") |
54 |
57 parser.add_option('-W', "--width", help="Output tile width", metavar="PX", type='int', default=800) |
|
58 parser.add_option('-H', "--height", help="Output tile height", metavar="PX", type='int', default=600) |
|
59 parser.add_option('-x', "--x", help="Tile x offset", metavar="PX", type='int', default=0) |
|
60 parser.add_option('-y', "--y", help="Tile y offset", metavar="PX", type='int', default=0) |
|
61 parser.add_option('-z', "--zoom", help="Tile zoom level, -n", metavar="ZL", type='int', default=0) |
|
62 parser.add_option('-o', "--out", help="Render tile to output file, or -", metavar="FILE") |
|
63 |
55 # parse |
64 # parse |
56 options, args = parser.parse_args(args=args) |
65 options, args = parser.parse_args(args=args) |
57 |
66 |
58 # decode |
67 # decode |
59 if options.background : |
68 if options.background : |
62 |
71 |
63 except ValueError : |
72 except ValueError : |
64 raise ValueError("Invalid value for --background: %s" % options.background) |
73 raise ValueError("Invalid value for --background: %s" % options.background) |
65 |
74 |
66 return args |
75 return args |
|
76 |
|
77 |
|
78 def process_tile (image) : |
|
79 """ |
|
80 Process output tile for given image |
|
81 """ |
|
82 |
|
83 # parse out |
|
84 if options.out == '-' : |
|
85 log_debug("\tUsing stdout as output...") |
|
86 |
|
87 # use stdout |
|
88 out = sys.stdout |
|
89 |
|
90 else : |
|
91 log_debug("\tOpening file for output: %s", options.out) |
|
92 |
|
93 # open file |
|
94 out = open(options.out, "wb") |
|
95 |
|
96 log_info("\tRender tile %dx%d@(%d:%d)@%d -> %s...", options.width, options.height, options.x, options.y, options.zoom, options.out) |
|
97 |
|
98 # render |
|
99 image.tile_file(options.width, options.height, options.x, options.y, options.zoom, out) |
|
100 |
|
101 log_debug("Rendered tile: %s", options.out) |
67 |
102 |
68 |
103 |
69 def process_image (image) : |
104 def process_image (image) : |
70 """ |
105 """ |
71 Process given image |
106 Process given image |
107 log_warn("\tSupressing cache update even though it is needed") |
142 log_warn("\tSupressing cache update even though it is needed") |
108 |
143 |
109 else: |
144 else: |
110 log_debug("\tImage cache is fresh") |
145 log_debug("\tImage cache is fresh") |
111 |
146 |
|
147 # open it |
|
148 image.open() |
|
149 |
112 # show info |
150 # show info |
113 info = image.info() |
151 info = image.info() |
114 |
152 |
115 log_info("\tImage dimensions: %d x %d (%d bpp)", info['img_width'], info['img_height'], info['img_bpp']) |
153 log_info("\tImage dimensions: %d x %d (%d bpp)", info['img_width'], info['img_height'], info['img_bpp']) |
116 log_info("\tImage mtime=%d, bytes=%d", info['image_mtime'], info['image_bytes']) |
154 log_info("\tImage mtime=%d, bytes=%d", info['image_mtime'], info['image_bytes']) |
117 log_info("\tCache mtime=%d, bytes=%d, blocks=%d (%d bytes), version=%d", |
155 log_info("\tCache mtime=%d, bytes=%d, blocks=%d (%d bytes), version=%d", |
118 info['cache_mtime'], info['cache_bytes'], info['cache_blocks'], info['cache_blocks'] * 512, info['cache_version'] |
156 info['cache_mtime'], info['cache_bytes'], info['cache_blocks'], info['cache_blocks'] * 512, info['cache_version'] |
119 ) |
157 ) |
120 |
158 |
|
159 |
|
160 # render tile? |
|
161 if options.out : |
|
162 log_debug("\tRendering output tile") |
|
163 |
|
164 process_tile(image) |
|
165 |
|
166 else : |
|
167 log_debug("\tNot rendering output tile") |
121 |
168 |
122 def process_images (image_paths) : |
169 def process_images (image_paths) : |
123 """ |
170 """ |
124 Open up each image_path and process using process_image |
171 Open up each image_path and process using process_image |
125 """ |
172 """ |