Commits

Michael Schurter committed 06d8399

Comments (0)

Files changed (1)

snippets/custom-export.py

+from StringIO import StringIO
+import Image
+import ImageColor
+
+import cherrypy
+
+def str2color(val):
+    return ImageColor.getrgb('#'+val.ljust(6, '0'))
+
+class SaveImagePage(LoggedInPage):  # Custom framework specific -- remove
+    def control(self, page, meta_width='', meta_height='', meta_bgColor='ffffff', stream=''):
+        # Convert 3.1 parameters to 3.0 style as they made more sense
+        width = meta_width
+        height = meta_height
+        bgcolor = meta_bgColor
+        data = stream
+
+        # Split the data into rows using ; as the spearator
+        rows = data.split(';')
+
+        # Create image
+        bgcolor = str2color(bgcolor)
+        im = Image.new('RGB', (int(width), int(height)), bgcolor)
+        imcore = im.load()
+
+        for y, row in enumerate(rows):
+            x = 0
+
+            # Split row into pixels
+            pixels = row.split(',')
+            for pixel in pixels:
+                # Split pixel into color and repeat value
+                color, repeat = pixel.split('_')
+                repeat = int(repeat)
+
+                if color == '':
+                    # Empty color == background color
+                    color = bgcolor
+                else:
+                    # Pad color to 6 characters
+                    color = str2color(color)
+
+                while repeat:
+                    # Add pixels 1-at-a-time since putdata() doesn't work
+                    imcore[x, y] = color
+                    x += 1
+                    repeat -= 1
+
+        # Save image into file like object
+        imstr = StringIO()
+        im.save(imstr, 'PNG', quality=100)
+
+        # Set HTTP headers -- CherryPy specific code
+        cherrypy.response.headers['content-type'] = 'image/png'
+        cherrypy.response.headers['content-disposition'] = \
+                'attachment; filename="Chart.png'
+
+        # Return image as string, your framework may be different
+        return imstr.getvalue()