| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- 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="图片宽度,默认不变")
- def run(dir,width):
- if dir is not None:
- find_path = "find " + dir + " -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -type f -size +100k | sort -nr"
- result = os.popen(find_path).read().split('\n')
- for item in result:
- compress_file(item, width)
- pass
- pass
- else:
- print('请指定要压缩的文件或文件夹')
- print('End')
- def compress_file(item, width):
- if 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
- print(item)
- os.popen(convert_path)
- if __name__ == "__main__":
- run()
|