WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
  #1  
Old 03-23-2009, 12:13 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Vizard tech tip: Using the Python Imaging Library (PIL)

Use the Python Imaging Library (PIL) to add image processing capabilities to your Vizard script. You can process an image and then load it as a texture within Vizard. Cutting, pasting, and merging images, rotating and resizing images, and image enhancement are some examples of what you can do. Setting up and using this 3rd party library is easy. Follow the instructions in this article and then check out the example scripts provided.

Installation

Go to the Pythonware site to download the installer.

Download the PIL version of the installer for the version of Python used by Vizard. Use the table below to determine which version of Python your Vizard installation uses:

Vizard 2.x uses Python 2.3
Vizard 3.x uses Python 2.4

Run the installer. It should automatically detect Vizard's Python installation. If you have multiple Python installations on your computer, make sure you select the Vizard Python installation.

You should now be able to use PIL within your Vizard script. Browse through the documentation and examples to learn how to use the module.

This first script shows the basics for taking a PIL image and coping it to a Vizard texture. Use the function shown here to apply your own processed images as textures within Vizard.

Code:
import viz
import Image
viz.go()

def PIL_TO_VIZARD(image,texture):
	"""Copy the PIL image to the Vizard texture"""
	im = image.transpose(Image.FLIP_TOP_BOTTOM)
	texture.setImageData(im.convert('RGB').tostring(),im.size)

#Create a blank Vizard texture
tex = viz.addBlankTexture([1,1])

#Apply texture to quad
quad = viz.addTexQuad(pos=(0,1.8,2),texture=tex)

#Create a PIL image.  If the image is in the current script directory
#you don't need to specify the path
image = Image.open( viz.res.getFullPath('lake3.jpg') )

#Copy PIL image to Vizard texture
PIL_TO_VIZARD(image,tex)
In the following example we open up a PIL image, process it with the image.crop method and then copy the resulting images to Vizard textures with the PIL_TO_VIZARD function. Then we animate the quads those textures are applied to.

Code:
import viz
import Image
viz.go()

import vizinfo
info = vizinfo.add('This script demonstrates the use of the Python Imaging Library(PIL) within Vizard')

def PIL_TO_VIZARD(image,texture):
	"""Copy the PIL image to the Vizard texture"""
	im = image.transpose(Image.FLIP_TOP_BOTTOM)
	texture.setImageData(im.convert('RGB').tostring(),im.size)

#Create 4 blank Vizard texture
tex1 = viz.addBlankTexture([1,1])
tex2 = viz.addBlankTexture([1,1])
tex3 = viz.addBlankTexture([1,1])
tex4 = viz.addBlankTexture([1,1])

#Apply textures to 4 quads
quad1 = viz.addTexQuad(pos=(-.5,2.5,8),texture=tex1)
quad2 = viz.addTexQuad(pos=(.5,2.5,8),texture=tex2)
quad3 = viz.addTexQuad(pos=(-.5,1.5,8),texture=tex3)
quad4 = viz.addTexQuad(pos=(.5,1.5,8),texture=tex4)

#Create a PIL image.  If the image is in the current script directory
#you don't need to specify the path
image = Image.open( viz.res.getFullPath('lake3.jpg') )

#Break up the image into 4 regions using the image.crop method
WIDTH = image.size[0] 
HEIGHT = image.size[1] 
HALF_WIDTH = int(round(image.size[0] / 2.0)) 
HALF_HEIGHT = int(round(image.size[1] / 2.0))

box1 = (0,0,HALF_WIDTH,HALF_HEIGHT)
region1 = image.crop(box1)
box2 = (HALF_WIDTH,0,WIDTH,HALF_HEIGHT)
region2 = image.crop(box2)
box3 = (0,HALF_HEIGHT,HALF_WIDTH,HEIGHT)
region3 = image.crop(box3)
box4 = (HALF_WIDTH,HALF_HEIGHT,WIDTH,HEIGHT)
region4 = image.crop(box4)

#Copy PIL images to Vizard textures
PIL_TO_VIZARD(region1,tex1)
PIL_TO_VIZARD(region2,tex2)
PIL_TO_VIZARD(region3,tex3)
PIL_TO_VIZARD(region4,tex4)

#Animate our texture quads
wait = vizact.waittime(1)

move1_out = vizact.move([-.5,.5,0], 1)
move1_in = vizact.move([.5,-.5,-0], 1)
quad1.addAction(vizact.sequence([move1_out,move1_in, wait], viz.FOREVER))

move2_out = vizact.move([.5,.5,0], 1)
move2_in = vizact.move([-.5,-.5,-0], 1)
quad2.addAction(vizact.sequence([move2_out,move2_in, wait], viz.FOREVER))

move3_out = vizact.move([-.5,-.5,0], 1)
move3_in = vizact.move([.5,.5,-0], 1)
quad3.addAction(vizact.sequence([move3_out,move3_in, wait], viz.FOREVER))

move4_out = vizact.move([.5,-.5,0], 1)
move4_in = vizact.move([-.5,.5,-0], 1)
quad4.addAction(vizact.sequence([move4_out,move4_in, wait], viz.FOREVER))
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
starting Vizard from a Python script hotspur1 Vizard 12 06-12-2019 01:03 PM
Vizard tech tip: Text to Speech Jeff Vizard 1 01-15-2009 10:39 PM
Matlab and Vizard brystewa Vizard 1 09-18-2006 04:11 PM
Use Vizard 2.5 with Python 2.4.1 / IDLE 1.1.1 Gilliard Vizard 4 08-05-2005 12:49 AM
running multiple vizard scripts from python exhale Vizard 1 05-09-2005 06:35 AM


All times are GMT -7. The time now is 10:51 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC