Skip to content

Commit

Permalink
Added a CLI param param to calcluate the aspect
Browse files Browse the repository at this point in the history
This is useful if you don't know the aspect ratio of your current resolution
and quickly want to know which folder is best used for your screen.
  • Loading branch information
exhuma committed Jan 23, 2012
1 parent 7a68f2f commit b6a8d21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 9 additions & 4 deletions wpclassifier/wpclassifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def get_image_size(filepath):
LOG.error("Unable to open file %s (%s)" % (filepath, str(e)))
return None

def aspect_folder(width, height):
"""
Given an image aspect ratio, return a folder name
"""
return "%d@%d" % (width, height)

def get_image_aspect(filepath):
"""
Returns the image aspect ratio as a tuple of width/height. This simplifies
Expand All @@ -25,9 +31,8 @@ def get_image_aspect(filepath):
size = get_image_size(filepath)
if not size:
return None
x, y = size
fract = Fraction(x, y)
return fract.numerator, fract.denominator
fract = Fraction(*size)
return fract

def move_files(input_dir, output_dir, as_aspect=False, move=False):
from os import listdir, makedirs
Expand All @@ -40,7 +45,7 @@ def move_files(input_dir, output_dir, as_aspect=False, move=False):
if as_aspect:
aspect = get_image_aspect(in_file)
if aspect:
clazz = "%d@%d" % aspect
clazz = aspect_folder(*aspect)
else:
size = get_image_size(in_file)
if size:
Expand Down
13 changes: 12 additions & 1 deletion wpclassifier/wpclassifier/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from optparse import OptionParser

from wpclassifier import move_files
from wpclassifier import move_files, aspect_folder

LOG = logging.getLogger(__name__)

Expand All @@ -20,9 +20,20 @@ def main():
'folder name, use the aspect ration as folder name. For '
'cross-platform compatibility, the generated names will use "@" '
'instead of ":" as separator (f. ex.: 16@9) Default: not-set')
parser.add_option('-s', '--show-aspect', dest='show_aspect',
metavar='SIZE',
default=None, help='Show the target folder for a given resolution '
'and exit. For example: SIZE=1680x1050')

(options, args) = parser.parse_args()

if options.show_aspect:
tw, th = options.show_aspect.split('x')
target = aspect_folder(int(tw), int(th))
print "Pictures of size %s will be moved into %s" % (
options.show_aspect, target)
sys.exit(0)

if len(args) != 2:
parser.print_help()
sys.exit(9)
Expand Down

0 comments on commit b6a8d21

Please sign in to comment.