Sign in to follow this  
zxo102

How to make FusionChartsSave.py work with python2.4 and PIL-1.1.6 Module?

Recommended Posts

Hi there,

I downloaded the FusionChartsSave.py provided by the fusioncharts team in this forum. I would like to run it with python2.4 and PIL-1.1.6.  I tested it against fusioncharts3.0.7.  It does return an image for me to save but the image has nothing to do with curves and messed up with different junk colors. I also tested the script with the trial version of fusioncharts3.1. Everything is fine but there is nothing to be returned for me to save except web page displaying error.

I did not get any error when I run this script: FusionChartsSave.py under python2.4 and PIL-1.1.6. Anybody knows what is wrong with it?

Thanks for your help in advance.

ouyang 

#!c:/python24/python.exe
import cgi
import Image,ImageDraw
import cStringIO
# Please find the attached FusionCharts Image saving code for Python CGI. 
# All developers are welcome to review, modify and comment on the code to embed Python best practices.
# The code is developed and tested in Windows Platform- Python 2.5 and is using PIL-1.1.6 Module. 
# Decoded data from charts
form = cgi.FieldStorage()
# Width and height of chart.
width = form["width"].value
height = form["height"].value
# Default background color of the chart
bgcolor = form["bgcolor"].value
# Rows of color values.
data = form["data"].value
# Reference to graphics object
img = Image.new("RGB", (int(width),int(height)), "#"+bgcolor)
# Image to store the chart
draw = ImageDraw.Draw(img)
# Decompress the data
rows = data.split(";")
i=0
for eachRow in rows: 
  # Split individual pixels.   
  pixels = eachRow.split(",")
  
 # Horizontal pixel count 
  imgr = 0
 # Iterate through the pixels
  for eachPixel in  pixels:
 # Split the pixel into color and repeat value
 thispix = eachPixel.split("_")
 # Reference to color
 c = thispix[0]
 # Reference to repeat factor
 r = int(thispix[1])
 # If color is not empty (i.e., not background pixel)  
 if c!="":	 
  c=c.strip()
  if len(c)<6:
# If the hexadecimal code is less than 6 characters, pad with 0
c = ((6-len(c))*"0") +  c
  
  # Now, set the pixels
  k=1
  while (k <= r):
 # Add # to hex coded color
color="#"+c;
# Draw the pixel
draw.point((imgr, i), fill=color)
# Increment horizontal row count
imgr +=1
k +=1
 else:
  # Just increment horizontal index
  imgr += r
  i +=1
# Set Download File Name
print "Content-Disposition: attachment; filename='FusionCharts.jpg'"
## You can try setting PNG
## print "Content-Disposition: attachment; filename='FusionCharts.png'"
## print "Content-type: image/png"
# Set Content Type
print "Content-type: image/jpeg"
f = cStringIO.StringIO()
## IF you want to save as PNG
## img.save(f, "PNG")
img.save(f, "JPEG")
f.seek(0)
# Write Image
print f.read()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this