Skip to content

Instantly share code, notes, and snippets.

@juandesant
Last active April 8, 2025 17:02
Show Gist options
  • Select an option

  • Save juandesant/1984572bd48aeaf8254b9aff36972897 to your computer and use it in GitHub Desktop.

Select an option

Save juandesant/1984572bd48aeaf8254b9aff36972897 to your computer and use it in GitHub Desktop.
Get dimensions from an image based on file metadata, using mdls in the command line
def get_dimensions_from_image_path(path):
import os, subprocess, plistlib #os for path expansion; subprocess for Popen; plistlib for parsing the plist format
mdls_output = subprocess.Popen(
# command line arguments, including "-p -" for stdout output
['/usr/bin/mdls', '-p', '-', os.path.expandvars(path).rstrip('\n')],
stdout=subprocess.PIPE # pipe for stdout
).stdout.read().decode() # read bytes from stdout, and decode them as string
mdls_dict = plistlib.loads(mdls_output) # load string as Plist
return mdls_dict['kMDItemPixelWidth'], mdls_dict['kMDItemPixelHeight'] # keys for image width and height
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment