瀏覽代碼

更新 'compress.py'

qiang 6 年之前
父節點
當前提交
964fc3f416
共有 1 個文件被更改,包括 16 次插入3 次删除
  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()