how to use python to capture screenshot mac m1

I am interested in either using xcode or python to capture screenshot. So far I am finding no instructions, no information on doing this.

Mac mini, macOS 11.2

Posted on Mar 29, 2021 5:31 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 30, 2021 12:42 PM

#!/usr/bin/env python3

# use screen capture to interactively select region of screen to
# capture, and then save that .png to the Desktop (default)

import subprocess
import sys


def main():
    try:
        subprocess.check_output(['screencapture', '-i', '-x', '-p'],
                                shell=False)
    except subprocess.CalledProcessError as e:
        print('Python error: [%d]\n{}\n'.format(e.returncode, e.output))


if __name__ == '__main__':
    sys.exit(main())


Tested on macOS 11.2.3 with Python 3.9.2. Capture goes to the Desktop, though one can remove the '-p' argument and replace it with a file path to represent the captured image file.

10 replies
Question marked as Top-ranking reply

Mar 30, 2021 12:42 PM in response to foxjazz

#!/usr/bin/env python3

# use screen capture to interactively select region of screen to
# capture, and then save that .png to the Desktop (default)

import subprocess
import sys


def main():
    try:
        subprocess.check_output(['screencapture', '-i', '-x', '-p'],
                                shell=False)
    except subprocess.CalledProcessError as e:
        print('Python error: [%d]\n{}\n'.format(e.returncode, e.output))


if __name__ == '__main__':
    sys.exit(main())


Tested on macOS 11.2.3 with Python 3.9.2. Capture goes to the Desktop, though one can remove the '-p' argument and replace it with a file path to represent the captured image file.

Apr 2, 2021 7:35 AM in response to foxjazz

The screencapture utility provides an optional -t argument that allows one to choose an alternative capture image format if the png format is unwanted.


Showing Microsoft C++ code that uses the Windows-only System.Drawing library is a far cry from Python which is what you originally requested. You might have more cross-platform success using the Python MSS package, provided you are using Python 3.5 or later. It provides for specific screen region capture. Otherwise, there is the optional Python Imaging Libraries (e.g. Pillow) that are also available for Windows and macOS.


Sure, Apple has imaging libraries that can be accessed via Objective-C, Swift, C++, and contingent on the installation of Xcode or the command-line utilities for the matching Xcode version. Using Apple's Cocoa scripting bridge, one can presently access the Cocoa frameworks from within either the Python 2.7.16 that Apple ships with Big Sur, or Python 3.9.2 after installing it and adding the optional pyobjc and pyobjc-core modules. That is tremendous pain and learning curve just for image capture, and why I used screencapture.


Apple mentioned in the Catalina release notes that it would be removing Python, Ruby, and Perl from a future release of macOS, and when they eventually do, the burden is on the developer to install their own builds of these scripting languages, with the modules necessary for the scripts to function.





Apr 1, 2021 6:21 AM in response to VikingOSX

And the Python code to capture an explicit area of the screen and write to a named file, or alternatively, a traditional date/time stamped screenshot file. This code captures the specific text string "Apple Support Communities Site Map" from the respective ASC Site map page.


#!/usr/bin/env python3

# use screen capture to interactively select region of screen to
# capture, and then save that .png to the Desktop (default)

# Coordinates (in Safari 14.0.3) set to capture "Apple Support Communities Site Map" from
# https://discussions.apple.com/productsitemap.jspa

import subprocess
import os
import sys


def main():
    outfile = os.path.expanduser('~/Desktop/captured.png')
    outcmd = "{} {} {} {}".format('screencapture', '-x', '-R332,330,770,75', outfile)

    # traditional date/time stamped screen capture file to the default desktop
    # outcmd = "{} {} {} {}".format('screencapture', '-x', '-R332,330,770,75', '-p')

    try:
        # ordinarily we would not use shell=True due to security concerns
        subprocess.check_output([outcmd], shell=True)
    except subprocess.CalledProcessError as e:
        print('Python error: [%d]\n{}\n'.format(e.returncode, e.output))


if __name__ == '__main__':
    sys.exit(main())


Apr 1, 2021 11:13 PM in response to VikingOSX

So this is how it's done on windows pc:

using System.Drawing;

public Image Capture(Point SourcePoint, Point DestinationPoint)

{

var rec = new Rectangle(SourcePoint.X, SourcePoint.Y, DestinationPoint.X - SourcePoint.X,

DestinationPoint.Y - SourcePoint.Y);

LocalWidth = rec.Width;

LocalHeight = rec.Height;

LocalX = Convert.ToInt32(SourcePoint.X);

LocalY = (Int32)SourcePoint.Y;

return CaptureScreen();

}


Image is just a bitmap.

png is useless to me.

pixel = calBitmap.GetPixel(x, y + 2);

// This is the Red, Green Blue of the pixel.

if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255)

{


This code is useful. Is there anything like this for the mac?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to use python to capture screenshot mac m1

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.