Last active
April 8, 2025 17:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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