TileStache.Pixels module¶
Support for 8-bit image palettes in PNG output.
PNG images can be significantly cut down in size by using a color look-up table. TileStache layers support Adobe Photoshop’s .act file format for PNG output, and can be referenced in a layer configuration file like this:
“osm”: {
“provider”: {“name”: “proxy”, “provider”: “OPENSTREETMAP”}, “png options”: {“palette”: “http://tilestache.org/example-palette-openstreetmap-mapnik.act“}}
The example OSM palette above is a real file with a 32 color (5 bit) selection of colors appropriate for use with OpenStreetMap’s default Mapnik cartography.
To generate an .act file, convert an existing image in Photoshop to indexed color, and access the color table under Image -> Mode -> Color Table. Saving the color table results in a usable .act file, internally structured as a fixed-size 772-byte table with 256 3-byte RGB triplets, followed by a two-byte unsigned int with the number of defined colors (may be less than 256) and a finaly two-byte unsigned int with the optional index of a transparent color in the lookup table. If the final byte is 0xFFFF, there is no transparency.
-
TileStache.Pixels.apply_palette(image, palette, t_index)¶ Apply a palette array to an image, return a new image.
-
TileStache.Pixels.apply_palette256(image)¶ Get PIL to generate and apply an optimum 256 color palette to the given image and return it
-
TileStache.Pixels.load_palette(file_href)¶ Load colors from a Photoshop .act file, return palette info.
Return tuple is an array of [ (r, g, b), (r, g, b), ... ], bit depth of the palette, and a numeric transparency index or None if not defined.
-
TileStache.Pixels.palette_color(r, g, b, palette, t_index)¶ Return best palette match index.
Find the closest color in the palette based on dumb euclidian distance, assign its index in the palette to a mapping from 24-bit color tuples.