Wednesday, 16 January 2013

Taking a Photo


Taking a Photo

The most important functionality that one expects from a camera is to
take a photo. Without further ado, here is how to do it.

Example 35: Minimalist camera
import camera
photo = camera.take_photo()
photo.save("E:\\Images\\minicam.jpg")
The function camera.take photo() returns the photo as an Image
object. With photo.save('E:\\Images\\minicam.jpg') the
photo is saved to the image gallery of the phone. If some other application
is using the camera when you start this script, the program fails and an
error is shown.
After trying Example 35, you are probably delighted to see Example 36,
which lets you use the viewfinder before taking a photo.

Example 36: Taking photos with a viewfinder
import e32, camera, appuifw, key_codes
def viewfinder(img):
canvas.blit(img)
def shoot():
camera.stop_finder()
photo = camera.take_photo(size = (640, 480))
w, h = canvas.size
canvas.blit(photo, target = (0, 0, w, 0.75 * w), scale = 1)
image.save('e:\\Images\\photo.jpg')
def quit():
app_lock.signal()
appuifw.app.body = canvas = appuifw.Canvas()
appuifw.app.title = u"Camera"
appuifw.app.exit_key_handler = quit
camera.start_finder(viewfinder)
canvas.bind(key_codes.EKeySelect, shoot)
app_lock = e32.Ao_lock()
app_lock.wait()
First we initialize the application UI and assign a canvas to the
application body in a familiar way. We bind the Select key to the function
shoot() that is used to take the actual photo – this corresponds to
the first approach to handling the keyboard keys, in Section 5.2.1. The
viewfinder functionality is based on Example 34.
Once the user clicks the Select key, the function shoot() is called.
First the viewfinder is closed, which freezes the latest image on the screen,
so that the user can see that the camera is taking the photo. Then we take
the actual photo with the camera.take photo() function. This may
take a few seconds and you should hold the camera still during this time.
Once the photo, photo, is ready, we show it on the canvas. Note that
the photo is typically taken in 3:4 picture ratio, which is not the same as
the canvas size, usually. The photo is too big to fit the canvas and we do
not want to resize it to fill the canvas in full, since this would break the
aspect ratio. Instead, we define the target area to which the photo should
be copied. The target area has the aspect ratio of 3:4, which the photo is

then resized to fit. These actions are handled by two optional parameters
to the blit() function, namely target and scale. you can use the
phone’s Gallery application to see the newly taken photo or you can
copy it to your PC for viewing.
You can change the exposure adjustment and the white balance
settings as well as using digital zoom and flash. All these parameters are
described in detail in the PyS60 API reference documentation.












No comments:

Post a Comment