IMG519 focus not working in RAW mode

I am trying to get focused images in raw mode, but no matter what I do, this just does not work.

When I first shoot a test image as .jpg that image is in focus, but the images directly after captured in raw mode are directly out of focus.

The problem is not only that the auto focus does not work in raw mode, but that it even changes focus when capturing raw frames.

Any idea why this is happening and how to fix this?

import time
from picamera2 import Picamera2
from picamera2.sensor_format import SensorFormat
from PIL import Image
import numpy as np

num_frames = 2
exposure_time = 55000
res = (4656, 3496)

picam2 = Picamera2()
capture_config = picam2.create_still_configuration(raw={"size": res}, buffer_count=num_frames)
picam2.configure(capture_config)
images = []

print ("setting up cam and focusing..")
picam2.start()
picam2.set_controls({"ExposureTime": exposure_time, "AnalogueGain": 1.0})
picam2.set_controls({"AfMode": 1 ,"AfTrigger": 0})
time.sleep(3)
picam2.switch_mode_and_capture_file(capture_config, "/var/www/html/focus_test.jpg")
time.sleep(3)
print ("Capturing images raw")
for i in range(num_frames):
    images.append(picam2.capture_array("raw"))
print ("shooting end, saving images...")
metadata = picam2.capture_metadata()
metadata["ExposureTime"] = exposure_time
for i in range(len(images)):
    picam2.helpers.save_dng(images[i], metadata, capture_config["raw"], "/var/www/html/img_{}.dng".format(i+1))


Hi,
switch_mode_and_capture_file switches the configuration when obtaining the image file. Each loading configuration is considered as an initialization and re-focus is performed. However, there is no image data at this time, and the calculated focus position is incorrect. We used the capture_file method instead of the switch_mode_and_capture_file method to solve this problem.

import time
from picamera2 import Picamera2
from picamera2.sensor_format import SensorFormat
from PIL import Image
import numpy as np

num_frames = 2
exposure_time = 55000
res = (4656, 3496)

picam2 = Picamera2()
capture_config = picam2.create_still_configuration(raw={"size": res}, buffer_count=num_frames)
picam2.configure(capture_config)
images = []

print ("setting up cam and focusing..")
picam2.start()
picam2.set_controls({"ExposureTime": exposure_time, "AnalogueGain": 1.0})
# picam2.set_controls({"AfMode": 0, "LensPosition": 200})
picam2.set_controls({"AfMode": 1, "AfTrigger": 0})
time.sleep(3)
picam2.capture_file("./focus_test.jpg")
time.sleep(3)
print ("Capturing images raw")
for i in range(num_frames):
    # picam2.set_controls({"AfMode": 1, "AfTrigger": 0})
    # time.sleep(3)
    images.append(picam2.capture_array("raw"))
print ("shooting end, saving images...")
metadata = picam2.capture_metadata()
metadata["ExposureTime"] = exposure_time
for i in range(len(images)):
    picam2.helpers.save_dng(images[i], metadata, capture_config["raw"], "./img_{}.dng".format(i+1))

Besides, we would like to know if you are willing to share this script on GitHub. We want to put this script which is based on yours on Github to serve more customers. Of course, we will Write acknowledgments in the notes.