yxy620
Posts: 5
Joined: Sat Aug 06, 2022 12:13 am

Transitioning from legacy Picamera to new Picamera 2.

Sat Aug 06, 2022 2:40 am

Hi,

I'm new to raspberry pi 4. I downloaded some code with legacy Picamera that i wanted to convert it to Picamera 2 & libcamera.



Here is the camera.py file (original code with legacy):

import time
from time import sleep
from picamera import Picamera

class Camera(object):

def __init__(self):

camera = Picamera()
camera.rotation = 180
camera.zoom = (0.05, 0.0, 0.75, 0.95)

camera.resolution = (1024, 768)

self.camera=camera
sleep(2)

def capture(self, file_path):

self.camera.capture(file_path, size = (240,240))

return file_path
---------------------------------------------------------------------------------------------------------------------------------------------------------

Here is me trying to convert it to Picamera2.:

import time
from time import sleep
from picamera2 import Picamera2


class Camera(object):

def __init__(self):

camera = Picamera2()
camera.rotation = 180
camera.zoom = (0.05, 0.0, 0.75, 0.95)

camera.resolution = (1024, 768)

self.camera=camera
sleep(2)

def capture(self, file_path):

self.camera.capture_file(file_path, size=(240,240))

return file_path

------------------------------------------------------------------------------------------------------------------------------------
When I run the above, it's giving me this error (on second to last line): 'capture_file' got an unexpected keyword argument 'size'.

I went through the Picamera2 manual-draft.pdf and couldn't find any relevant information.


By the way, just to give you some context. The goal of the my program is to use this camera.py to take pictures and feed into google cloud service API. So the above error is when I tried to run it in my API.py. Also, I'm hosting a live camera feed to a local web browser. So it's live streaming while taking photos every couple of seconds and uploading them directly to the cloud. The camera display on my web page is also not working and keeps showing it's initializing.


Thanks for the help!

User avatar
bensimmo
Posts: 5658
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Transitioning from legacy Picamera to new Picamera 2.

Sat Aug 06, 2022 9:35 am

Remove the size= parameter.

Seems they only have a long pdf for documentation, do they have a nice website like picamera for documents? pdf are a pain on a phone and in general.
See if you set that elsewhere before you start it.
Maybe just set the resolution to that at the start.

yxy620
Posts: 5
Joined: Sat Aug 06, 2022 12:13 am

Re: Transitioning from legacy Picamera to new Picamera 2.

Sat Aug 06, 2022 6:24 pm

Sorry, I made a typo. (For that second to last line, the second parameter is resize=(240,240) , not size=.)

I tried to delete it, take it out on a separate line and rename it. They all didn't work.

My question is what's the Picamera2 equivalent of capture() in the old Picamera? It looks like in the Picamera2 document it's called capture_file(). But no further information is given regarding to what and how many parameters you can put into it.


yxy620
Posts: 5
Joined: Sat Aug 06, 2022 12:13 am

Re: Transitioning from legacy Picamera to new Picamera 2.

Sun Aug 07, 2022 12:04 am

I have sort of fixed it.

camera_config = camera.create_still_configuration(main={"size": (1024, 768)})
camera.configure(camera_config)

Now errors are gone.
But I still couldn't do the resize, which is fine. Still couldn't put a resize parameter into the capture_file().

Return to “Troubleshooting”