Friday, August 21, 2009

PyPhotoFrame

Introduction

How many times do we capture photos with our phones? Wouldn't it be great to customize these photos on the phones itself? This article illustrates an application which is useful for adding photo frame pattern on your selected photos. The code snippet is written with the simplest mobile application development tool, [[PyS60]]. The application has been tested on Nokia N73.

The application uses the following modules:
*appuifw module
*graphics module
*e32 module
*time module

The following functions are used to create this application PyPhotoFrame :

*draw_screen():draw the black screen on canvas

*save_image: save the image with its selected frame

*gold_frame():draw the gold frame on selected image

*sliver_frame(): draw the sliver frame on selected image

*wooden_frame(): draw the wooden frame on selected image

*red_frame(): draw the red frame on selected image

*blue_frame(): draw the blue frame on selected image

*RingsnChain(): draw the Rings and Chain frame on selected image

*DottedLine(): draw the Dotted line frame on selected image

*DottedLine(): draw the Big Dotted line frame on selected image

*BigDottedFrame(): draw the fancy4 frame on selected image

*ArrowFrame(): draw the arrow frame on selected image


All the functions and parameters are defined in source code in detail.

Code snippet
The code for the is below.

# importing the necessary modules
import e32
import appuifw
import graphics
import math

# defining color codes

BLACK=(0,0,0)
RED=(176,23,31)
BLUE=(0,178,238)
WHITE=(255,255,255)
DEEP_BLUE=(0,0,255)
VIOLET=(255,62,150)
GOLD=(205,133,0)
SLIVER=(139,137,137)
ORANGE=(139,90,0)
GRAY=(205,201,201)
WOOD= (139,87,66)
MEGENTA=(139,0,139)
CRIMSON=(220,20,60)
BLUE2=(0,0,238)

running=1

#global variables
global x,y
x=240
y=320
x1=25
Separator=10
outer_line=14
inner_line=5
x_Separator=10
y_Separator=20

def quit():
'''the exit function'''
global running
running=0
app_lock.signal()


def menu_about():
'''the about function'''
appuifw.note(u"PhotoFrame version 1.00" + "\n Developed by Nirpsis")


def handle_redraw(rect):
'''function that will be called when the canvas needs to be redrawn'''
canvas.blit(img)


def draw_screen():
'''define the draw
function - loads new image'''
#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,event_callback = None)
appuifw.app.body=canvas
#Set the screen to full
appuifw.app.screen = 'full'
appuifw.app.exit_key_handler=quit
#Global variable for application UI
global img
#We open the image
img=graphics.Image.open("C:\\image.jpg")
img=img.resize((240,320), keepaspect=0)
handle_redraw(img)

def save_image():
'''save image'''
img.save("C:\\saved.jpg", quality=100)
appuifw.note(u"Image saved at C:\\saved.jpg")


def gold_frame():

'''function to draw golden frame'''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=GOLD,width=20)
j=1
while (j<5):

img.rectangle(((Separator+(j*2)),(Separator+(j*2)),((x-y_Separator)+(j*2)),((y-y_Separator)+(j*2))),fill=None,outline=ORANGE)
j=j+1
handle_redraw(None)


def sliver_frame():

'''function to draw silver frame'''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=GRAY,width=20)
j=1

while (j<5):
img.rectangle(((Separator+(j*2)),(Separator+(j*2)),((x-20)+(j*2)),((y-20)+(j*2))),fill=None,outline=SLIVER)
j=j+1
handle_redraw(None)


def wooden_frame():

'''function to draw wooden frame'''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=WOOD,width=20)
j=1
while (j<5):
img.rectangle(((Separator+(j*2)),(Separator+(j*2)),((x-y_Separator)+(j*2)),((y-y_Separator)+(j*2))),fill=None,outline=WOOD)
j=j+1
handle_redraw(None)


def red_frame():

'''function to draw red frame'''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=RED,width=20)
j=1
while (j<5):
img.rectangle(((Separator+(j*2)),(Separator+(j*2)),((x-y_Separator)+(j*2)),((y-y_Separator)+(j*2))),fill=None,outline=RED)
j=j+1
handle_redraw(None)


def blue_frame():

'''function to draw blue frame'''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=BLUE,width=20)
j=1

while (j<5):
img.rectangle(((Separator+(j*2)),(Separator+(j*2)),((x-y_Separator)+(j*2)),((y-y_Separator)+(j*2))),fill=None,outline=WHITE)
j=j+1
handle_redraw(None)


def RingsnChain():

'''function to draw rings and chain frame '''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=BLUE,width=5)
img.rectangle((20,20,220,300),fill=None,outline=BLUE,width=4)
a=1

while (a<23):>img.point(((Separator*a),Separator),fill=None,outline=DEEP_BLUE,width=8)
a=a+1
b=1
while (b<23):

global x,y,Separator
img.point(((Separator*b),(y-Separator)),fill=None,outline=DEEP_BLUE,width=8)
b=b+1

c=1
while (c<31):> img.point((Separator,(Separator*c)),fill=None,outline=DEEP_BLUE,width=8)
c=c+1

d=1
while (d<31):> global x,y,Separator
img.point(((x-Separator),(Separator*d)),fill=None,outline=DEEP_BLUE,width=8)
d=d+1

handle_redraw(None)

def DottedLine():

'''function to draw dotted line frame '''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=MEGENTA,width=5)
img.rectangle((20,20,220,300),fill=None,outline=MEGENTA,width=4)
a=2
while(a<22):>
global x,y,x1,x_Separator,y_Separator
img.ellipse(((x_Separator*a),(x_Separator/2),((x_Separator*a)+5),x1),fill=None,outline=MEGENTA)
a=a+1

b=2
while (b<22):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator*b),(y-x1),((x_Separator*b)+(x_Separator/2)),(y-(x_Separator/2))),fill=None,outline=MEGENTA)
b=b+1

c=1
while (c<19):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator/2),(x_Separator+(c*15)),y_Separator,(x1+(c*15))),fill=None,outline=MEGENTA)
c=c+1

d=1
while (d<19):>
global x,y,x_Separator,x1
img.ellipse(((x-y_Separator),(x_Separator+(d*15)),(x-(x_Separator/2)),(x1+(d*15))),fill=None,outline=MEGENTA)
d=d+1
handle_redraw(None)


def DottedLine():

'''function to draw dotted line frame '''
draw_screen()
img.rectangle((0,0,x,y),fill=None,outline=CRIMSON,width=5)
img.rectangle((20,20,220,300),fill=None,outline=CRIMSON,width=4)
a=0

while (a<11):>
global x,y,x1,x_Separator,y_Separator
img.ellipse(((x_Separator+(a*y_Separator)),x_Separator,(x1+(a*y_Separator)),(y_Separator-(x_Separator/2))),fill=CRIMSON,outline=GRAY)
a=a+1
b=0
while (b<11):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator+(b*y_Separator)),(y-(x1-x_Separator)),(x1+(b*y_Separator)),(y-x_Separator)),fill=CRIMSON,outline=GRAY)
b=b+1

c=0
while (c<15):>
global x,y,x_Separator,y_Separator,x1
img.ellipse((x_Separator,(x_Separator+(c*y_Separator)),(x1-x_Separator),(x1+(c*y_Separator))),fill=CRIMSON,outline=GRAY)
c=c+1

d=0
while (d<(x_Separator+5)):
global x,y,x_Separator,x1
img.ellipse(((x-15),(x_Separator+(d*y_Separator)),(x-x_Separator),(x1+(d*y_Separator))),fill=CRIMSON,outline=GRAY)
d=d+1
handle_redraw(None)


def BigDottedFrame():

'''function to draw big dotted frame '''
draw_screen()
x1=25
img.rectangle((0,0,x,y),fill=None,outline=VIOLET,width=4)
img.rectangle((20,20,220,300),fill=None,outline=VIOLET,width=2)
a=0

while (a<22):>
global x,y,x1,x_Separator,y_Separator
img.ellipse(((x_Separator+(a*x_Separator)),(x_Separator/2),(x1+(a*x_Separator)),(x1-x_Separator)),fill=VIOLET,outline=GRAY)
a=a+1

b=0
while (b<22):>
img.ellipse(((x_Separator+(b*x_Separator)),(y-(x1-x_Separator)),(x1+(b*x_Separator)),(y-5)),fill=VIOLET,outline=GRAY)
b=b+1

c=0
while (c<30):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator/2),(x_Separator+(c*x_Separator)),(x1-x_Separator),(x1+(c*x_Separator))),fill=VIOLET,outline=GRAY)
c=c+1

d=0
while (d<30):>
global x,y,x_Separator,x1
img.ellipse(((x-(x1-x_Separator)),(x_Separator+(d*x_Separator)),(x-5),(x1+(d*x_Separator))),fill=VIOLET,outline=GRAY)
d=d+1
handle_redraw(None)


def ArrowFrame():

'''function to draw arrow frame '''
draw_screen()
x1=25
img.rectangle((0,0,x,y),fill=None,outline=BLUE2,width=4)
img.rectangle((20,20,220,300),fill=None,outline=BLUE2,width=2)
a=0

while (a<13):>
global x,y,x1,x_Separator,y_Separator
img.ellipse(((x_Separator+(a*(y_Separator-3))),(x_Separator/2),(x1+(a*(y_Separator-3))),y_Separator),fill=BLUE2,outline=WHITE)
a=a+1

b=0
while (b<13):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator+(b*(y_Separator-3))),(y-(x1-x_Separator)),(x1+(b*(y_Separator-3))),y),fill=BLUE2,outline=WHITE)
b=b+1

c=0
while (c<18):>
global x,y,x_Separator,y_Separator,x1
img.ellipse(((x_Separator/2),(x_Separator+(c*(y_Separator-3))),y_Separator,(x1+(c*(y_Separator-3)))),fill=BLUE2,outline=WHITE)
c=c+1

d=0
while (d<18):>
global x,y,x_Separator,x1
img.ellipse(((x-y_Separator),(x_Separator+(d*(y_Separator-3))),(x-5),(x1+(d*(y_Separator-3)))),fill=BLUE2,outline=WHITE)
d=d+1
handle_redraw(None)


# Create options menu
appuifw.app.menu = [(u"Load Image", draw_screen),(u"Simple",((u"Golden",gold_frame),(u"Sliver",sliver_frame),(u"Wooden",wooden_frame),(u"Red",red_frame),(u"Blue",blue_frame))),(u"Fancy",((u"Rings n Chain",RingsnChain),(u"Dotted line",DottedLine),(u"Dotted Line",DottedLine),(u"Big Dotted",BigDottedFrame),(u"Arrow",ArrowFrame))),(u"Save Image",save_image),(u"About",menu_about),(u"Exit",quit)]

draw_screen()

app_lock = e32.Ao_lock()
#Wait for the user to request the exit


app_lock.wait()




Screenshots














Thursday, August 20, 2009

PyHealth

Introduction

Health is important to you and me, especially me :)

This PyHealth application calculates BMI (Body Mass Index) and BMR (Basal Metabolic Rate) in Python for S60. The BMI and BMR figures could tell indicate if you over-weight, under-weight or normal-weight and how many calories you burn everyday, respectively.


BMI

Body Mass Index (BMI) - The measure of body fat based on height and weight that applies to both adult male and female. Enter your weight and height using Metric measures and this application would calculate your BMI.

BMR

Basal Metabolic Rate (BMR) - The measurement of energy, in calories, required to keep the body functioning at rest. Metabolic rates increase with exertion, stress, fear, and illness. The BMR uses the variables of height, weight, age and gender to calculate itself. We all need a certain number of calories to function, so it's important not to reduce your calories below your BMR when you're trying to lose weight.

The following standard PyS60 modules are used when developing this application,
* appuifw module
* graphics module
* e32 module
* math module

The following functions are used in PyHealth,
* draw_screen: prepare the canvas

* BMI: for calculating BMI

* BMR : for calculating BMR
All the functions and parameters are defined in source code in detail.



Code Snippet

The source code for the PyHealth is given below

# importing the necessary modules
import e32
import appuifw
import graphics
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

#global variables
global x,y
x=10
y=50

# Set application title
appuifw.app.title=u"PyHealth"

# Define the exit function
def quit():
global running
running=0
app_lock.signal()


#define functions
def menu_about():
appuifw.note(u"PyHealth version 1.00" + "\n Developed by Nirpsis")


#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():
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 img
img = graphics.Image.new(canvas.size)

#Clear the image
img.clear(BLACK)

handle_redraw(None)


#define start function
def BMI():
global x,y
img.clear(BLACK)
data1,data2 = appuifw.multi_query(u" Your Weight(Kg)",u" Your Height(Cm)")
result = (float(data1)/((float(data2)*float(data2))/10000)) #Divide
a=result

if(a < fill =" BLUE,font="(u'Nokia" fill =" BLUE,font="(u'Nokia">18) and( a<24): fill =" BLUE,font="(u'Nokia" fill =" BLUE,font="(u'Nokia">24) and (a<30): fill =" BLUE,font="(u'Nokia" fill =" BLUE,font="(u'Nokia">30):
img.text((x,y), u"BMI: "+unicode(a), fill = BLUE,font=(u'Nokia Hindi S60',22,appuifw.STYLE_BOLD))
img.text((x,y+40),u"Comment: Obesity! NO junk food!", fill = BLUE,font=(u'Nokia Hindi S60',14,appuifw.STYLE_BOLD))
handle_redraw(None)


def BMR():
global x,y
img.clear(BLACK)
data1,data2 = appuifw.multi_query(u" Your Weight(Kg)",u" Your Height(Cm)")
list=[u'Female',u'Male']
#Selectionlist allows the user to select the list items
list1=appuifw.selection_list(choices=list,search_field=1)
data3= appuifw.query(u"Your Age","number")


if(list1==0):
result =(655 + ( 9.6 * float(data1) ) + ( 1.8 *float(data2) )-( 4.7 * float(data3) ))
a=result
if(a>1300) and(a < fill =" BLUE,font="(u'Nokia" fill =" BLUE,font="(u'Nokia">1500):
img.text((x,y), u"BMR: "+unicode(result), fill = BLUE,font=(u'Nokia Hindi S60',22,appuifw.STYLE_BOLD))
img.text((x,y+40), u"Comment: High BMR! Take proper Vitamins ", fill = BLUE,font=(u'Nokia Hindi S60',14,appuifw.STYLE_BOLD))

if(list1==1):

result= (66 + ( 13.7 * float(data1) ) + ( 5 *float(data2) ) - ( 6.8 *float(data3) ))
img.text((x,y), u"BMR: "+unicode(result), fill = BLUE,font=(u'Nokia Hindi S60',22,appuifw.STYLE_BOLD))
a=result
if(a>1600) and(a < fill =" BLUE,font="(u'Nokia" fill =" BLUE,font="(u'Nokia">1800):
img.text((x,y), u"BMR: "+unicode(result), fill = BLUE,font=(u'Nokia Hindi S60',22,appuifw.STYLE_BOLD))
img.text((x,y+40), u" Comment: High BMR!Take proper Vitamins ", fill = BLUE,font=(u'Nokia Hindi S60',14,appuifw.STYLE_BOLD))
#display result

draw_screen()
appuifw.app.menu = [(u"BMI",BMI),(u"BMR",BMR),(u"About", menu_about), (u"Exit", quit)]

app_lock = e32.Ao_lock()
#Wait for the user to request the exit

app_lock.wait()


Screenshots

Below are few of the Screenshots by PyHealth applicaion.