AppleScript Capture Low Res Photo?
  • Is it possible to capture a lower resolution image when issuing the following command within AppleScript please?

    tell application "SecuritySpy"
    capture image camera number 3 as "/tmp/ss-temp-image.jpg" with overwrite
    end tell

    Following on from that, I intend to create a script that captures a series of 30 photos in quick succession with a 0.5 second delay between each one, then create a video from those images to send via Telegram. This is so that I have a quick summary of the motion within the notification rather than just a static photo, and hence the ask for a lower resolution photo.

    Is there already a way to do something similar within SecuritySpy or is the method above the best way to achieve what I'm after?

  • You can scale images with the "sips" tool built into macOS, for example:

    tell application "SecuritySpy"
        capture image camera number 3 as "/tmp/ss-temp.jpg" with overwrite
    end tell

    do shell script "sips -Z 320 /tmp/ss-temp.jpg"

    This captures an image and resizes it to 320 pixels wide. The "Z" parameter tells sips to keep the original aspect ratio.
  • ahh thank you, I never thought to call another utility to resize the image.

    I'll post back on here when I have a working example in case others find it useful too.
  • I'm making progress with the script but I'm struggling to ask SecuritySpy to generate the images quick enough.

    My aim is to create a photo every 0.2 seconds, compress these into a video file with ffmpeg, and then send it to Telegram.

    At this interval SecuritySpy fails with an error saying:

    Script Error
    SecuritySpy got an error: Not ready yet

    The lowest I can go is 0.5 when generating 20 images, but I suspect this may give me an error occasionally too if SS is busy with other operations.

    Do you know if there's a quicker way to capture photos from SS please? (if indeed it's possible to capture images that quickly?)

    This is the code I'm using to generate the images:

    set cameraNum to "1"
    set cameraName to "BackCam"
    set reason to "Manual"
    set folderName to "/Users/paul/SecuritySpy/MotionVid/" & cameraName & "/"
    do shell script "mkdir -p " & folderName

    set i to 1
    repeat while i < 21
    tell application "SecuritySpy"
    capture image camera number cameraNum as folderName & "/image_" & i & ".jpg" with overwrite
    end tell
    set i to i + 1
    delay 0.5
    end repeat
  • The issue is that when your "capture image" command comes in, SecuritySpy has to then wait for the next frame to arrive from the camera, which it then captures. So there is a short period of time between the command being issued, and the frame being captured, during which SecuritySpy cannot accept another "capture image" command. A solution is to put the "capture image" command within a "try" block, like this:

        capture image
    end try

    This way, if one of the "capture image" command fails due to this condition, it won't stop the script with an error message.

    Also, make sure that the frame rate of this camera is sufficient to minimise this condition (e.g. if you want to capture at 10fps, then set the camera to 20fps).
  • Brilliant, I didn't know you could do that (try) so thank you for the tip.

    Thanks also for explaining what happens behind the scenes.

    b.t.w, is there a way to post code so that it's formatted with indents correctly?
  • Unfortunately there is no good way to post code on this forum, sorry! The only way to preserve the formatting is to put your code inside an HTML <code> element when writing your comment.

    I hope that helped and you are now getting good results. Please let me know if you have any other questions.
  • Yes thanks, it works really well now. I'll post the code in a new thread in case others want to use it too.
  • Many thanks for posting your code in the other thread, looks great! Anyone reading this can find that other thread here: Sending a short fast video of motion to Telegram example.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!