[Edu-sig] Images in VPython: Faces

Joel Kahn jj2kk4 at yahoo.com
Thu Sep 9 22:42:21 CEST 2004


The attached script contains another approach to the
process of displaying images in VPython. Instead of
there being an object for each "pixel," two triangles
on a faces object are placed together to produce a
square, as noted in the appropriate comment lines. As
with my "objects" example, I kept this prototype as
simple as possible; there are many things that could
be done with varying normals, z-axis-positions, &c
that I haven't touched yet.

The new "faces" algorithm delivers the image to the
screen significantly more quickly than the "objects"
system, and it seems to be able to handle much higher
resolution pictures; still, I suggest beginning your
experiments with small bitmaps to play it safe.

As always, feedback is encouraged.

Joel


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
import Image
from visual import *

# "Faces" object is used to display bitmap image.

autocenter = 1
scene = display()
scene.width = 1024
scene.height = 738
scene.x = 0
scene.y = 0

# Adjust ambient light and normal values to your preference.
scene.ambient = 0.7
nrm = vector (0.5, 0.5, 0.5)

Picture = Image.open ("bitmap.bmp")
Picture = Picture.convert ("RGB")
Pic_si = Picture.size
Pic_width = Pic_si[0]
Pic_height = Pic_si[1]
Pic_fr = frame ()
Pic = faces (frame = Pic_fr)

# To form each "pixel," two isosceles right triangles are placed
# hypotenuse to hypotenuse to make a square.
for xx in arange (Pic_width):
    for yy in arange (Pic_height):
        pxl = Picture.getpixel ((xx, yy))
        x2 = int (xx - Pic_width / 2)
        y2 = int (yy - Pic_height / 2)
        rr = pxl[0] / 256.0
        gg = pxl[1] / 256.0
        bb = pxl[2] / 256.0
        Pic.append (pos = (x2, y2, 0.0), color = (rr, gg, bb), normal = nrm)
        Pic.append (pos = (x2, y2 - 1, 0.0), color = (rr, gg, bb), normal = nrm)
        Pic.append (pos = (x2 + 1, y2, 0.0), color = (rr, gg, bb), normal = nrm)
        Pic.append (pos = (x2 + 1, y2, 0.0), color = (rr, gg, bb), normal = nrm)
        Pic.append (pos = (x2, y2 - 1, 0.0), color = (rr, gg, bb), normal = nrm)
        Pic.append (pos = (x2 + 1, y2 - 1, 0.0), color = (rr, gg, bb), normal = nrm)

# Upside down image is rotated in its frame to be right side up.
Pic_fr.rotate (angle = pi, axis = (0.0, 0.0, 1.0), origin = (0.0, 0.0, 1.0))


More information about the Edu-sig mailing list