📜 ⬆️ ⬇️

Working with mobile phone camera in Python

In the last article we looked at installing Python for S60 and working in an interactive console, today we will look at how Python works with a mobile phone camera.

So let's get started ...



There is a special camera module for working with the camera in Python. To get started, we’ll get photo resolutions supported by the camera:
import camera
sizes = camera.image_sizes()

And if we just write in the console camera.image_sizes then we get very strange results - that the camera resolution is no more than 2 megapixels. In order to be able to take photos in all the "power" of the camera, you must first transfer the application to landscape mode (horizontal).
To do this, before connecting the camera, we need to add the line appuifw.app.orientation = 'landscape' .
In order to enable camera preview, you need to get the canvas of our form, for this: canvas = appuifw.Canvas()
Also, you must first prepare a small function to initialize the preview:
def cam_finder(im):
canvas.blit(im)


Well, now we can safely run the previewer:
camera.start_finder(cam_finder, size=(320,240))
The size of the preview window is shown here - they can be of any size.

Now we need to take a picture from the camera, for this we will add the ability to take a photo to the central button:
canvas.bind(key_codes.EKeySelect, take_picture)
here:
EKeySelect is the central button of the joystick,
take_picture is a function that will be executed when a button is pressed, we will write it:
First you need to turn off the preview:
camera.stop_finder()
then the most important thing is to get a photo from the camera, for this is the function camera.take_photo, which can take many parameters, such as zoom, flash use, photo size and others. We set only the size of the photo.
pic = camera.take_photo(size = (cur_w,cur_h))

That's actually what we added - get a photo from the camera. As a result, we have an object of class Image which supports basic operations, such as resizing, saving, adding text, drawing simple shapes and others.
In order to save the resulting image, the save function is used, which takes the file name and the quality level of jpeg from 1 to 99
pic.save(filename, quality=75)

Below is a small program with a choice of photo resolution and detailed comments.
image
The source code could not write here, as Habr eats all spaces at the beginning of lines, but for python it is critical, so we look at the source code with comments here

')

Source: https://habr.com/ru/post/53549/


All Articles