Archived:How to take a photo using PySymbian
Article Metadata
Tested with
Compatibility
Article
Contents |
Overview
This snippet shows how to take a photograph in Python.
Preconditions
Note: The camera module is not available for S60 1st Edition.
Note: The Image object method save is not available for S60 1st Edition.
Images can be saved in either JPEG or PNG format.
Source code
import appuifw, e32, camera
app_lock = e32.Ao_lock()
#Define the exit function
def quit():
#Close the viewfinder
camera.stop_finder()
#Release the camera so that other programs can use it
camera.release()
app_lock.signal()
appuifw.app.exit_key_handler = quit
#Function for displaying the viewfinder
def vf(im):
appuifw.app.body.blit(im)
#Function for taking the picture
def take_picture():
#Take the photo
photo = camera.take_photo('RGB', (1024, 768))
#Save it at maximum quality
photo.save(photo_savepath, quality = 100)
#Restart the viewfinder
camera.stop_finder()
camera.start_finder(vf)
photo_savepath = u"C:\\photo.jpg"
#This is the path and name for storing the photo
#Set the application's body to Canvas
appuifw.app.body = appuifw.Canvas()
#Start the viewfinder
camera.start_finder(vf)
#Set the application's menu with the option to take the photo
appuifw.app.menu=[(u"Take photo", take_picture)]
#Wait for the user to request the exit
app_lock.wait()
Postconditions
A photograph will be taken with the given settings and stored at the given path.
Additional information
The camera module has the following functions that are related to taking photos:
- cameras_available()
Returns the number of cameras available in the device.
- image_modes()
Returns the image modes supported in the device as a list of strings, for example: ['RGB12', 'RGB', 'JPEG_Exif', 'RGB16'].
- image_sizes()
Returns the image sizes (resolution) supported in the device as a list of (x, y) tuples, for example: [(640, 480), (160, 120)].
- flash_modes()
Returns the flash modes available in the device as a list of strings.
- max_zoom()
Returns the maximum digital zoom value supported in the device as an integer.
- exposure_modes()
Returns the exposure settings supported in the device as a list of strings.
- white_balance_modes()
Returns the white balance modes available in the device as a list of strings.
The function used for taking pictures takes its optional parameters from the functions described above and has the following general form: take_photo([mode, size, zoom, flash, exposure, white_balance, position]), where position is the camera to be used, if the device has more than one (0 for normal camera, 1 for front camera).
On some devices, in order to use the maximum resolution available, special steps must be taken:
- Switch to the landscape mode.
- Import the camera module.
- Take the picture in the 'JPEG_Exif' format.
- Save it by writing its information in a file, not as an Image object.
Here is a code snippet for a device with a 5 megapixel camera, such as the Nokia N95:
import appuifw, e32
#Switch to landscape mode
appuifw.app.orientation = 'landscape'
import camera
app_lock = e32.Ao_lock()
#Define the exit function
def quit():
#Close the viewfinder
camera.stop_finder()
#Release the camera so that other programs can use it
camera.release()
app_lock.signal()
appuifw.app.exit_key_handler = quit
#Function for displaying the viewfinder
def vf(im):
appuifw.app.body.blit(im)
#Function for taking the picture
def take_picture():
#Take the photo
photo = camera.take_photo('JPEG_Exif', (2592, 1944))
#Save it
f = open(photo_savepath, "wb")
f.write(photo)
f.close()
#Restart the viewfinder
camera.stop_finder()
camera.start_finder(vf)
photo_savepath = u"C:\\photo.jpg"
#This is the path and name for storing the photo
appuifw.app.body = appuifw.Canvas()
#Start the viewfinder
camera.start_finder(vf)
appuifw.app.menu=[(u"Take photo", take_picture)]
#Wait for the user to request the exit
app_lock.wait()


21 Sep
2009
28 Sep
2009