You Are Here:

Community: Wiki

This page was last modified on 20 September 2009, at 13:06.

How to generate graphics in Python

From Forum Nokia Wiki

Reviewer Approved   

Contents

Introduction

This articles demonstrates generation of graphics for S60 devices. The simplest mobile application development tool, PYS60,is used to develop this GraphicsGenerator.

Basically, the code snippet given in this article generates beautiful patterns (slide strokes for now) of the desired colour and latency (a parameter to interpret the pattern).


Features

  • Generates slide stroke patterns.
  • Supports all colours.
  • Support for random colours.
  • Supports 30 different patterns - all slide strokes.
  • Graphics generated can be saved.


Usage

The following modules are used when developing this application:

  • appuifw module
  • os module
  • graphics module
  • e32 module
  • key_codes module
  • random module
  • math module
  • time module

The following functions are used to create GraphicsGenerator:

  • save(): save the screenshots of Graphicsgenerator
  • draw_screen(): draw the screen on canvas
  • Start(): create a different pattern on canvas

These all functions are defined in source code in detail.


Source code

The code for the GraphicsGenerator is below.

#
# Graphics Generator
#
 
'''
1.00 2009-07-05 Initial release
'
''
 
VERSION = "1.00"
 
# importing the necessary modules
import e32,random
import time
import appuifw, os
import graphics, time
import math
 
# define color code
BLACK=(0,0,0)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
WHITE=(255,255,255)
running=1
 
#initializing pattern
pattern=30
 
 
# Define the exit function
app_lock = e32.Ao_lock()
def quit():
global running
running=0
app_lock.signal()
appuifw.app.exit_key_handler = quit
 
# Defining the save function for Screeenshots
def save():
dirpath="C:\\GraphicsGenerator"
try:
os.mkdir(dirpath)
except: pass
 
# Sleep for 1 second
e32.ao_sleep(1)
 
# This function converts the current view to the new images
image=graphics.screenshot()
 
#current time is return by this function
s = time.ctime()[11:19]
 
# This is the path and name for storing the screenshots
image.save(dirpath+"\\screenshot.jpg")
appuifw.note(u"Saved to "+ unicode(dirpath))
 
#Define a function that will be called when the canvas needs to be redrawn
def handle_redraw(rect):
canvas.blit(img)
 
#define the draw function
def draw_screen():
 
#Set the screen to full
appuifw.app.screen = 'full'
 
#Global variable for application UI
global canvas
 
#Create an instance of Canvas and set it as the application's body
 
canvas = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit
 
#Global variable for application UI
global img
img = graphics.Image.new(canvas.size)
 
#Clear the image
img.clear(BLACK)
handle_redraw(None)
 
def menu_about():
''' Callback for menu item About '''
 
appuifw.note(u"Graphics Generator version 1.0"+"\n Developed by Nirpsis")
 
#defining start function
 
def start():
global running, pattern
running=0
UserWantRandom=0
 
# Define x & y coordinate
x=120
y=160
acelX=-1
acelY=-1
appuifw.note(u"Choose the colour of the pattern")
L=[u"Red", u"Blue", u"Green", u"White", u"Random",u"Manual Colour"]
 
#Selectionlist allows the user to select the list items
i = appuifw.selection_list(L, search_field=1)
if (i==0): COLOR=RED
if (i==1): COLOR=BLUE
if (i==2): COLOR=GREEN
if (i==3): COLOR=WHITE
if (i==4): UserWantRandom=1
if (i==5):
a=appuifw.query(u"Enter Red concentration 0-255", "number")
if a>255 or a<0:
a=255
b=appuifw.query(u"Enter Green concentration 0-255", "number")
if b>255 or b<0:
b=255
c=appuifw.query(u"Enter Blue concentration 0-255", "number")
if c>255 or c<0:
c=255
COLOR=(a,b,c)
else:
quit()
 
pattern=appuifw.query(u"Enter patter 1-30", "number")
if pattern>30 or pattern<1:
pattern=30
 
# Calling the draw_screen function
draw_screen()
running=1
 
# Main loop
while running:
if x<0:
if acelX==1:
acelX=-1
else:
acelX=1
if y<0:
if acelY==1:
acelY=-1
else:
acelY=1
if x>240:
if acelX==1:
acelX=-1
else:
acelX=1
 
if y>320:
if acelY==1:
acelY=-1
else:
acelY=1
 
if(y <= 0):
y = 0;
if(y >= 320):
y = 320;
if(x <= 0):
x = 0;
if(x >= 240):
x = 240;
 
#defining the position of the points
 
x = x * acelX
y=y * acelY
x=x+pattern
y=y+pattern
if UserWantRandom==1:
 
#below script draws random points
#defining the colours of points
a=random.randint( 0, 255)
b=random.randint( 0, 255)
c=random.randint( 0, 255)
COLOR=(a,b,c)
 
# Drawing points
img.point((x,y),outline=COLOR,width=15)
 
# Sleep for 0.01
e32.ao_sleep(0.01)
e32.ao_yield()
 
# Main menu
appuifw.app.menu = [(u"Save Graphics", save), (u"Start again", start),(u"About", menu_about), (u"Exit", quit)]
handle_redraw(None)
start()
app_lock = e32.Ao_lock()
 
#Wait for the user to request the exit
app_lock.wait()


Screenshots

Below are few of the graphics generated by GraphicsGenerator application, demonstrating the different pattern generation on canvas.

Image:Screenshot1.jpgImage:Screenshot2.jpg Image:Screenshot3.jpgImage:Screenshot4.jpg

Related Links

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fPyPhotoFrameX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ