<s> import Image <EOL> import sys <EOL> import optparse <EOL> import glob <EOL> import os <EOL> import string <EOL> import tempfile <EOL> import shutil <EOL> import zipfile <EOL> def TrimImage ( imagename ) : <EOL> im = Image . open ( imagename ) <EOL> idx = <NUM_LIT:0> <EOL> width = im . size [ <NUM_LIT:0> ] <EOL> height = im . size [ <NUM_LIT:1> ] <EOL> top = height <EOL> bottom = <NUM_LIT:0> <EOL> left = width <EOL> right = <NUM_LIT:0> <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> for pix in im . getdata ( ) : <EOL> if pix [ <NUM_LIT:3> ] != <NUM_LIT:0> : <EOL> if top > y : <EOL> top = y <EOL> if bottom < y : <EOL> bottom = y <EOL> if left > x : <EOL> left = x <EOL> if right < x : <EOL> right = x <EOL> x = x + <NUM_LIT:1> <EOL> if x == width : <EOL> x = <NUM_LIT:0> <EOL> y = y + <NUM_LIT:1> <EOL> box = ( left , top , right , bottom ) <EOL> cropped = im . crop ( box ) <EOL> cropped . load ( ) <EOL> return ( width , height ) , box , cropped <EOL> def GetBaseDirectory ( file , knowndir ) : <EOL> fullpath = os . path . abspath ( file ) <EOL> basedir = fullpath <EOL> idx = string . find ( basedir , knowndir ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" + basedir + "<STR_LIT>" + knowndir ) <EOL> return basedir [ : idx ] <EOL> def VerifyDirectory ( basedir , dir ) : <EOL> outdir = os . path . join ( basedir , dir ) <EOL> if not os . path . exists ( outdir ) : <EOL> os . mkdir ( outdir ) <EOL> return outdir </s>
