|
From: Ives, T. W. <tho...@hp...> - 2005-06-09 17:53:08
|
VPython Community,
=20
I finally got tired of not being able to use my favorite graphics
routines to create image files for graphs for reporting and for creating
animations that I could transport to people without VPython, so I hacked
some useful stuff together. My apologies if someone already submitted
something similar.
=20
First, a clean and simple way to capture images for graphs, etc. The key
here is the use of PIL (Python Imaging Library) and importing ImageGrab
from that.
=20
from visual import *
from visual.graph import *
import ImageGrab
=20
ImageWidth =3D 640; ImageHeight =3D 480;=20
oscillation =3D gdisplay(xtitle=3D't', ytitle=3D'Response', width =3D
ImageWidth, height =3D ImageHeight,=20
background =3D color.white, foreground =3D color.black)
funct1 =3D gcurve(color=3Dcolor.magenta)
funct2 =3D gvbars(delta=3D0.5, color=3Dcolor.red)
funct3 =3D gdots(color=3Dcolor.orange)
=20
for t in arange(-30, 70, 1):
funct1.plot( pos=3D(t, 5.0+5.0*cos(-0.2*t)*exp(0.015*t)) )
funct2.plot( pos=3D(t, 2.0+5.0*cos(-0.1*t)*exp(0.015*t)) )
funct3.plot( pos=3D(t, 5.0*cos(-0.03*t)*exp(0.015*t)) )
=20
im =3D ImageGrab.grab((4,30,ImageWidth-4,ImageHeight-4))
im.save("screen.gif")
oscillation.display.visible =3D 0
=20
This places the window in the upper left corner of the screen. I had to
experiment with how much to take away from the grab, but it works
cleanly now for any width and height I specify.
=20
To create an animated gif, there is an extension of the above, below,
using a modification of the bounce2.py demo file. The new addition here
is WhirlGif from http://www.danbbs.dk/~dino/whirlgif/ which was very
easy compile and link with MinGW http://www.mingw.org/. This is a free
utility for creating animated gif files. The key factor in the file
below is to save the filenames into a string list to feed to the
os.system("command prompt script in a string") command. Also, the image
capture slows the animation down enough to turn off the rate setting, at
least on my machine :-). This method is also good for creating animated
surface plots.=20
from visual import *
import os
import ImageGrab
=20
ImageWidth =3D 300; ImageHeight =3D 300
scene =3D display(width=3DImageWidth, height=3DImageHeight)
scene.x =3D 0; scene.y =3D 0
#scene.fullscreen =3D 1
side =3D 4.0
thk =3D 0.3
s2 =3D 2*side - thk
s3 =3D 2*side + thk
wallR =3D box (pos=3D( side, 0, 0), length=3Dthk, height=3Ds2,
width=3Ds3, color =3D color.red)
wallL =3D box (pos=3D(-side, 0, 0), length=3Dthk, height=3Ds2,
width=3Ds3, color =3D color.red)
wallB =3D box (pos=3D(0, -side, 0), length=3Ds3, height=3Dthk,
width=3Ds3, color =3D color.blue)
wallT =3D box (pos=3D(0, side, 0), length=3Ds3, height=3Dthk,
width=3Ds3, color =3D color.blue)
wallBK =3D box(pos=3D(0, 0, -side), length=3Ds2, height=3Ds2,
width=3Dthk, color =3D (0.7,0.7,0.7))
=20
ball =3D sphere (color =3D color.green, radius =3D 0.4)
ball.mass =3D 1.0
ball.p =3D vector (-0.15, -0.23, +0.27)
=20
side =3D side - thk*0.5 - ball.radius
=20
dt =3D 0.5
t=3D0.0
commandstring =3D "whirlgif -o bounce.gif "
filelist =3D ""
basename =3D "bounce_"; extname =3D ".gif"
for i in range(200):
#rate(100)
t =3D t + dt
ball.pos =3D ball.pos + (ball.p/ball.mass)*dt
if not (side > ball.x > -side):
ball.p.x =3D -ball.p.x
if not (side > ball.y > -side):
ball.p.y =3D -ball.p.y
if not (side > ball.z > -side):
ball.p.z =3D -ball.p.z
im =3D ImageGrab.grab((4,30,ImageWidth-4,ImageHeight-4))
filename =3D basename+str(i)+extname
filelist +=3D filename + " "
im.save(filename)
=20
scene.visible =3D 0
os.system(commandstring + filelist)
=09
Hope these are helpful,
thom
=20
=20
=20
________________________________
HP logo
<http://www.hp.com/Redirect/gw/useng_welcome/logo/=3Dhttp://welcome.hp.co=
m
/country/us/eng/welcome.htm> Thom Ives, Ph.D.
R&D Engineering Scientist=09
11413 Chinden Blvd MS 303
Boise, ID. 83714
208.396.6880 Phone
208.396.7560 Fax
tho...@hp...=20
Home:
5556 N Columbine Pl
Boise, ID 83713
208.938.3365 Phone
208.272.0285 Cell
407.210.5514 Fax
tw...@ms... <mailto:tw...@ms...>=20
________________________________
=20
=20
|