Explorar o código

更新 'compress.py'

qiang %!s(int64=6) %!d(string=hai) anos
pai
achega
964fc3f416
Modificáronse 1 ficheiros con 16 adicións e 3 borrados
  1. 16 3
      compress.py

+ 16 - 3
compress.py

xqd xqd xqd
@@ -4,7 +4,6 @@ import os
 import click
 from PIL import Image
 
-
 @click.command()
 @click.option('-d', "--dir", type=str, default='/', help="压缩文件夹下所有文件")
 @click.option('-w', "--width", type=int, default=1024, help="图片宽度,默认不变")
@@ -24,7 +23,21 @@ def run(dir,width):
 
 def compress_file(item, width):
     if item:
-        img = Image.open(item)
+        # print(item)
+        if not os.path.isfile(item):
+            print "这不是一个文件 file = %s" % item
+            return
+
+        basename = os.path.basename(item)
+        fileName, fileSuffix = os.path.splitext(basename)
+        if fileSuffix != '.png' and fileSuffix != '.jpg' and fileSuffix == '.jpeg':
+            print "不支持该文件类型! file = %s" % item
+            return
+
+        try:
+           img = Image.open(item)
+        except IOError:
+           print(file)
         x, y = img.size
         if x >= width:
             convert_path = "convert -resize 50%x50% " + item + ' ' +item
@@ -33,4 +46,4 @@ def compress_file(item, width):
 
 
 if __name__ == "__main__":
-    run()
+    run()