| 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="图片宽度,默认不变")
|
| xqd
@@ -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
|
| xqd
@@ -33,4 +46,4 @@ def compress_file(item, width):
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- run()
|
|
|
+ run()
|