Skip to content

Instantly share code, notes, and snippets.

@danielmcgrath
Created December 22, 2012 18:35
Show Gist options
  • Save danielmcgrath/4360377 to your computer and use it in GitHub Desktop.
Save danielmcgrath/4360377 to your computer and use it in GitHub Desktop.
Script to count the number of pixels in PSD files in a directory. For some reason. Requires psd-tools: https://github.com/kmike/psd-tools
import operator
import os
import sys
from psd_tools import PSDImage
def _parse_psd_size(dirname, filename):
file_ = '{0}/{1}'.format(dirname, filename)
size = (0, 0)
try:
psd = PSDImage.load(file_)
size = psd.as_PIL().size
except:
print 'Could not parse: {0}'.format(file_)
return size
def get_psd_pixels(dir):
dimensions = (0, 0)
for root, dirs, files in os.walk(dir):
for f in files:
name, ext = os.path.splitext(f)
if ext.lower() == '.psd':
size = _parse_psd_size(root, f)
dimensions = map(operator.add, dimensions, size)
return dimensions
def main(dir):
if not os.path.isdir(dir):
raise Exception('Invalid directory specified')
dimensions = get_psd_pixels(dir)
print 'Total PSD pixels in {0}:'.format(dir)
print 'WidthxHeight: {0}x{1}'.format(dimensions[0], dimensions[1])
print 'Total Area: {0}px'.format(reduce(operator.mul, dimensions))
if __name__ == '__main__':
sys.exit(main(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment