Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from neutrinus/master

inital setup.py for easy pip install
commit 5f09e7ff1f3e0574c9b4911e81dda8831b601d00 2 parents 9369a4d + d8ebfcd
@luxerama luxerama authored
Showing with 34 additions and 2 deletions.
  1. +13 −0 setup.py
  2. +21 −2 smush/smush.py
View
13 setup.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+from distutils.core import setup
+
+setup(name='Smush',
+ version='1.0',
+ description='Lossless image optimiser script',
+ url='https://github.com/thebeansgroup/smush.py',
+ platforms='OS Independent',
+ keywords="image optimize lossless",
+)
+
+
View
23 smush/smush.py
@@ -31,6 +31,7 @@ def __init__(self, **kwargs):
continue
self.exclude[dir] = True
self.quiet = kwargs.get('quiet')
+ self.identify_mime = kwargs.get('identify_mime')
# setup tempfile for stdout and stderr
self.stdout = Scratch()
@@ -65,6 +66,13 @@ def process(self, dir, recursive):
for file in os.listdir(dir):
if self.__checkExclude(file):
continue
+
+ if self.identify_mime:
+ import mimetypes
+ (type,encoding) = mimetypes.guess_type(file)
+ if type and (type[:5] != "image"):
+ continue
+
self.__smush(os.path.join(dir, file))
elif os.path.isfile(dir):
self.__smush(dir)
@@ -77,6 +85,13 @@ def __walk(self, dir, callback):
for file in os.listdir(dir):
if self.__checkExclude(file):
continue
+
+ if self.identify_mime:
+ import mimetypes
+ (type,encoding) = mimetypes.guess_type(file)
+ if type and (type[:5] != "image"):
+ continue
+
nfile = os.path.join(dir, file)
callback(nfile)
if os.path.isdir(nfile):
@@ -141,7 +156,7 @@ def __checkExclude(self, file):
def main():
try:
- opts, args = getopt.getopt(sys.argv[1:], 'hrqs', ['help', 'recursive', 'quiet', 'strip-meta', 'exclude=', 'list-only'])
+ opts, args = getopt.getopt(sys.argv[1:], 'hrqs', ['help', 'recursive', 'quiet', 'strip-meta', 'exclude=', 'list-only' ,'identify-mime'])
except getopt.GetoptError:
usage()
sys.exit(2)
@@ -155,6 +170,7 @@ def main():
strip_jpg_meta = False
exclude = ['.bzr', '.git', '.hg', '.svn']
list_only = False
+ identify_mime = False
for opt, arg in opts:
if opt in ('-h', '--help'):
@@ -166,6 +182,8 @@ def main():
quiet = True
elif opt in ('-s', '--strip-meta'):
strip_jpg_meta = True
+ elif opt in ('--identify-mime'):
+ identify_mime = True
elif opt in ('--exclude'):
exclude.extend(arg.strip().split(','))
elif opt in ('--list-only'):
@@ -187,7 +205,7 @@ def main():
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
- smush = Smush(strip_jpg_meta=strip_jpg_meta, exclude=exclude, list_only=list_only, quiet=quiet)
+ smush = Smush(strip_jpg_meta=strip_jpg_meta, exclude=exclude, list_only=list_only, quiet=quiet, identify_mime=identify_mime)
for arg in args:
try:
@@ -220,6 +238,7 @@ def usage():
-q, --quiet Don't display optimisation statistics at the end
-s, --strip-meta Strip all meta-data from JPEGs
--exclude=EXCLUDES comma separated value for excluding files
+ --identify-mime Fast identify image files via mimetype
--list-only Perform a trial run with no changes made
"""
Please sign in to comment.
Something went wrong with that request. Please try again.