Image Events Module Overview

PyXA supports the full scripting dictionary for Image Events, and some additional image manipulation features have been implemented using Objective-C APIs.

Image Events Tutorials

Tutorial 2 - Working With Lists of Images

The Image Events module has been designed around convenience, and a key aspect of that is the ability to conduct bulk operations with XAList objects, in particular by using the XAImageEventsImageList class. In addition to general syntactic convenience, XAImageEventsImageList objects provide significant performance improvements over conventional looping operations. Since XALists do not spend time dereferencing PyObjC/ScriptingBridge pointers, they send far fewer Apple Events, leading to much faster execution. This is evidenced by the code below:

from timeit import timeit
import PyXA
app = PyXA.Application("Image Events")

def without_xalist():
    img_data = []
    images = [app.open("/Users/exampleUser/Desktop/Example1.jpeg"), app.open("/Users/exampleUser/Desktop/Example2.jpeg"), app.open("/Users/exampleUser/Desktop/Example3.jpeg")]
    for image in images:
        image.scale(3)
        image.rotate(45)
        img_data.append(image.get_clipboard_representation())
    PyXA.XAClipboard().content = img_data

def with_xalist():
    images = app.open("/Users/exampleUser/Desktop/Example1.jpeg", "/Users/exampleUser/Desktop/Example2.jpeg", "/Users/exampleUser/Desktop/Example3.jpeg")
    PyXA.XAClipboard().content = images.scale(3).rotate(45)

r1 = timeit(without_xalist, number=100)
r2 = timeit(with_xalist, number=100)

print("Non-XAList avg over 100 trials:", r1 / 100)
# ~3.835 seconds per iteration (on M1 Pro MacBook Pro)

print("XAList avg over 100 trials:", r2 / 100)
# ~0.076 seconds per iteration (on M1 Pro MacBook Pro)

In the XAList-equipped function, app.open yeilds an XAImageEventsImageList object. We then scale and rotate all images in the list The code above also highlights the concise coding style supported by XAImageEventsImageList objects. Method chaining as done here is entirely optional, but some may prefer this approach due to its similarity to JXA and JavaScript at large.

All attributes and methods of the XAImageEventsImage class can be called on XAImageEventsImageList objects as well.

images = app.open(“/Users/exampleUser/Desktop/Example1.jpeg”, “/Users/exampleUser/Desktop/Example2.jpeg”, “/Users/exampleUser/Desktop/Example3.jpeg”) # # print(images[0].rotate(30).image_with_modifications.show_in_preview()) # print(images.original_images()[0].show_in_preview())

# Access Attributes in Bulk
print(images.properties())
print(images.bit_depth(), images.color_space())
print(images.dimensions(), images.resolution())
print(images.file_type(), images.image_file(), images.name())

# Retrieve Images By Attribute Value
print(images.by_name("Example.png"))
print(images.by_dimensions((2022, 1542)))
print(images.by_file_type(app.FileType.JPEG))
print(images.by_color_space(app.ColorSpace.RGB))
print(images.by_bit_depth(app.BitDepth.MILLIONS_OF_COLORS))

# Perform Bulk Manipulation Operations
images.rotate(45).scale(2)
images.flip_horizontally()
images.embed_profile(app.profiles().by_name("Generic CMYK Profile"))
images.save(file_paths=["/Users/exampleUser/Desktop/NewExample1.jpeg", "/Users/exampleUser/Desktop/NewExample2.jpeg", "/Users/exampleUser/Desktop/NewExample3.jpeg"])
PyXA.XAClipboard().content = images

Image Events Examples

The examples below provide an overview of the capabilities of the Image Events module. For more in-depth examples that show output and provide more detailed explanations, refer to the previous section (Image Events Tutorials).

Example 1 - Using Image Events Alongside Other PyXA Features

Combining Image Events, XAImages, Photos.app, and more

The functionality of the Image Events module can be easily extended by intertwining it with other PyXA features. The XAImage class is a key example of this. In fact, you can easily convert images managed by Image Events into XAImages, giving you full access to all features thereof. In the code below, we first use Image Events to rotate an image, then we use the extract_text() method from XAImage to retrieve text contained within. We also open the original and modified images in preview, copy the raw data of the modified image’s TIFF representation to the clipboard, and save the modified image to a file on the disk.

import PyXA
app = PyXA.Application("Image Events")
image = app.open("/Users/steven/Desktop/code.png").rotate(45)
modified_image = image.modified_image_object
print(modified_image.extract_text())

image.original_image_object.show_in_preview()
modified_image.show_in_preview()

PyXA.XAClipboard().contents = str(modified_image.data)
modified_image.save("/Users/exampleUser/Desktop/NewExample4.png")

Image Events Resources

For all classes, methods, and inherited members of the Image Events module, see the Image Events Module Reference.