I seek help on merging an ObjC bridge program, which Ole offered some time ago, with the standard photos.capture_image
These three lines of my coding work how I want but the iOS insists on me pressing the camera button, then touching 'Use Photo'. I can then work with the output it gives.
import photos
theImage=photos.capture_image()
theImage.show()
So I found Ole's excellent Objective-C bridge program which takes a photo when told to by the program, quoted below –
from objc_util import *
import time
import threading
C = ObjCClass
def take_photo_now(filename='photo.jpg'):
session = C('AVCaptureSession').new().autorelease()
session.sessionPreset = 'AVCaptureSessionPresetPhoto'
device = C('AVCaptureDevice').defaultDeviceWithMediaType_('vide')
device_input = C('AVCaptureDeviceInput').deviceInputWithDevice_error_(device, None)
session.addInput_(device_input)
image_output = C('AVCaptureStillImageOutput').new().autorelease()
session.addOutput_(image_output)
session.startRunning()
# NOTE: You may need to adjust this to wait for the camera to be ready (use a higher number if you see black photos):
time.sleep(0.1)
def handler_func(_block, _buffer, _err):
buffer = ObjCInstance(_buffer)
img_data = C('AVCaptureStillImageOutput').jpegStillImageNSDataRepresentation_(buffer)
img_data.writeToFile_atomically_(filename, True)
e.set()
video_connection = None
for connection in image_output.connections():
for port in connection.inputPorts():
if str(port.mediaType()) == 'vide':
video_connection = connection
break
if video_connection:
break
e = threading.Event()
handler = ObjCBlock(handler_func, restype=None, argtypes=[c_void_p, c_void_p, c_void_p])
retain_global(handler)
image_output.captureStillImageAsynchronouslyFromConnection_completionHandler_(video_connection, handler)
e.wait()
take_photo_now('photo.jpg')
import console
console.quicklook('photo.jpg')
But, hopelessly I cannot work out how to change the ObjC code to return a reference to the photo which my code can handle. That is to say that I don't know how to get 'photo.jpg' into my theImage (variable?) so I can show() it, and use it in the Image and ImageDraw modules. I wish for it happen quickly so I guess it needs to stay in a buffer and not be delayed by saving to the camera roll.
Many thanks.