Python function for capturing full resolution images

Using the normal python capture function I was unable to capture full resolution images due to errors when setting use_video_port to False, so I wrote this work around function for capturing full resolution images.

Would like to hear if it is possible using the normal capture function. I was able to resize it to a larger resolution, but it appeared to be a low resolution image resized to full resolution without adding detail.

def capture_full_image(self, params: dict = None):
    """Capture an image at full resolution and return it as PIL image object"""
    payload = {
        "use_video_port": False,
        "bayer": False,
        "filename": "py_capture",
        "temporary" : True
    }
    if params:
        payload.update(params)
    r = requests.post(self.base_uri + "/actions/camera/capture", json=payload)
    r.raise_for_status()
    captures_response = requests.get(self.base_uri + "/captures")
    captures_response.raise_for_status()
    json_response = json.loads(captures_response.content)
    target_id = ''
    for img_json in json_response[::-1]:
        if img_json['name'] == 'py_capture.jpeg':
            target_id = img_json['id']
            break
    img_response = requests.get(self.base_uri + f"/captures/{target_id}/download/py_capture.jpeg")
    img_response.raise_for_status()
    image = PIL.Image.open(io.BytesIO(img_response.content))
    return image

Hi @Nico thanks for figuring out and sharing that - using the “regular” capture method from the python client is an obvious omission and I’m sorry I’m still yet to find the time to implement it neatly.

A casual read of your code suggests to me that it doesn’t actually check for the capture to complete - so there is a possibility you’ll get an old image if you use parameters that result in a slow capture. If you use poll_task (I think that’s the name - not currently in a position to check) from the python client library, it will wait until the capture’s complete. The return value of poll_task should also include the capture ID that you want to download, and so it would neaten up a couple of things for you.

However, I suspect unless you do very slow captures (e.g. use bayer=True or use the still port) this will not be an issue - and clearly this code works for you as it is now.

I will do my best to include something like this in a future release of the Python client.

1 Like