1 |
#!/usr/bin/python |
#!/usr/bin/python |
2 |
|
|
3 |
import cgi, Image, ImageDraw, sys |
import sys |
4 |
|
|
5 |
|
sys.stderr = sys.stdout |
6 |
|
#print "Content-Type: text/plain" |
7 |
|
#print |
8 |
|
|
9 |
|
import cgi, Image, ImageDraw |
10 |
from string import atoi |
from string import atoi |
11 |
|
from Drawrect import drawrect |
12 |
|
|
13 |
|
|
14 |
form= cgi.FieldStorage() |
form= cgi.FieldStorage() |
15 |
|
|
16 |
width, height= 570, 570 # Image size |
# Image size |
17 |
|
if form.has_key('width'): |
18 |
|
width=atoi(form['width'].value) |
19 |
|
else: |
20 |
|
width= 570 |
21 |
|
if form.has_key('height'): |
22 |
|
height=atoi(form['height'].value) |
23 |
|
else: |
24 |
|
height= 570 |
25 |
|
|
26 |
xmax, ymax = width-1, height-1 # Coordinate maximums |
xmax, ymax = width-1, height-1 # Coordinate maximums |
27 |
|
|
28 |
inx=max(0, min(atoi(form['image.x'].value), xmax)) |
inx=max(0, min(atoi(form['image.x'].value), xmax)) |
50 |
julia= Image.new("L", (width, height)) |
julia= Image.new("L", (width, height)) |
51 |
draw= ImageDraw.Draw(julia) |
draw= ImageDraw.Draw(julia) |
52 |
|
|
|
def drawrect((x1, y1), (x2, y2)): |
|
|
color= plot(x1, y1) |
|
|
cut= 0 |
|
|
x, y= x1+1, y1 |
|
|
while x<=x2: |
|
|
if color<>plot(x, y): |
|
|
cut= 1 |
|
|
x= x+1 |
|
|
x, y= x2, y1+1 |
|
|
while y<=y2: |
|
|
if color<>plot(x, y): |
|
|
cut= 1 |
|
|
y= y+1 |
|
|
x, y= x2-1, y2 |
|
|
while x>=x1: |
|
|
if color<>plot(x, y): |
|
|
cut= 1 |
|
|
x= x-1 |
|
|
x, y= x1, y2-1 |
|
|
while y>y1: |
|
|
if color<>plot(x, y): |
|
|
cut= 1 |
|
|
y= y-1 |
|
|
if x2-x1<=1 or y2-y1<=1: |
|
|
return |
|
|
if cut: |
|
|
if x2-x1 > y2-y1: # If wider than high |
|
|
xc= ((x2-x1)/2)+x1 |
|
|
drawrect((x1, y1), (xc, y2)) |
|
|
drawrect((xc, y1), (x2, y2)) |
|
|
else: # if higher than wide |
|
|
yc= ((y2-y1)/2)+y1 |
|
|
drawrect((x1, y1), (x2, yc)) |
|
|
drawrect((x1, yc), (x2, y2)) |
|
|
else: |
|
|
if not debug: |
|
|
draw.rectangle([x1, y1, x2, y2], fill= color) |
|
|
if yorig >= 0: # The x axis is visible on image |
|
|
# mirror over yorig, but keep within image |
|
|
y1m= max(0, min(2*yorig-y1, ymax)) |
|
|
y2m= max(0, min(2*yorig-y2, ymax)) |
|
|
x1m= max(0, min(2*xorig-x1, xmax)) |
|
|
x2m= max(0, min(2*xorig-x2, xmax)) |
|
|
draw.rectangle([x1m, y1m, x2m, y2m], fill= color) |
|
|
|
|
53 |
def plot(x, y): |
def plot(x, y): |
54 |
z= constant(x, y) |
z= constant(x, y) |
55 |
i= 0 |
i= 0 |
78 |
raise IndexError, the_error |
raise IndexError, the_error |
79 |
return color |
return color |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
# Initial default values |
# Initial default values |
84 |
yfrom= 0 |
yfrom= 0 |
85 |
yto= ymax |
yto= ymax |
86 |
yorig= -1 # The pixel coordinate of the x axis; negative means not on image |
yorig= -1 # The pixel coordinate of the x axis; negative means not on image |
87 |
xorig= -1 |
xorig= -1 |
88 |
|
|
89 |
|
|
90 |
|
def fillrect(x1, y1, x2, y2, color): |
91 |
|
draw.rectangle([x1, y1, x2, y2], fill= color) |
92 |
|
if yorig >= 0: # The x axis is visible on image |
93 |
|
# mirror over yorig, but keep within image |
94 |
|
y1m= max(0, min(2*yorig-y1, ymax)) |
95 |
|
y2m= max(0, min(2*yorig-y2, ymax)) |
96 |
|
x1m= max(0, min(2*xorig-x1, xmax)) |
97 |
|
x2m= max(0, min(2*xorig-x2, xmax)) |
98 |
|
draw.rectangle([x1m, y1m, x2m, y2m], fill= color) |
99 |
|
|
100 |
|
|
101 |
# Check if we should do mirroring |
# Check if we should do mirroring |
102 |
if c1.imag > 0 and c2.imag < 0: # the x axis is visible |
if c1.imag > 0 and c2.imag < 0: # the x axis is visible |
103 |
yorig= int((-c1.imag/(c2.imag-c1.imag))*height) # pixel pos. of x axis |
yorig= int((-c1.imag/(c2.imag-c1.imag))*height) # pixel pos. of x axis |
109 |
if c1.real < 0 and c2.real > 0: |
if c1.real < 0 and c2.real > 0: |
110 |
xorig= int((-c1.real/(c2.real-c1.real))*height) |
xorig= int((-c1.real/(c2.real-c1.real))*height) |
111 |
|
|
112 |
drawrect((0, yfrom), (xmax, yto)) |
if not debug: |
113 |
|
funcpoint= fillrect |
114 |
|
else: |
115 |
|
funcpoint= lambda x1, y1, x2, y2, color: x1+y1+x2+y2+color |
116 |
|
|
117 |
|
drawrect((0, yfrom), (xmax, yto), plot, funcpoint) |
118 |
|
|
119 |
print "Content-type: image/png" |
print "Content-type: image/png" |
120 |
print |
print |
121 |
julia.save(sys.stdout, "PNG") |
julia.save(sys.stdout, "PNG") |
122 |
|
|