tinyimg.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import os
  4. import os.path
  5. import click
  6. import tinify
  7. online_key_list = [
  8. "eCaBl5SWbQaAJbT2MX3xnFzvHfLZA93O",
  9. "pPgiVMQxqFEExdAGwQI6nFlLKqtYlruz",
  10. "1DkU3Rp61yTi683OOvsd3daw9kVEqZf0",
  11. "PUxtVdhYtodwO4jC7d6I4VdsEyDx37mw",
  12. "fp1VyXlzPWQgJyLN7JRWYvgLjtkqSYvH",
  13. "nXNH4VFVjvPJtkDytSynSCnY0G1FmYwh",
  14. "ZhjfRpjGvh7txM90WJyj9tkRfkwKGNcC",
  15. "F79DPN8vNhx2wc40cBg60qJ4h3SvXyV8",
  16. "y48xStsV3tjQ6mw6yYk5gXVBlSGKgMWs",
  17. "G0t57lLwWHkS5nK2RqPPsQCP7WZP6lGP",
  18. "dpls8PYG8SpZYZxfzybB4FGYK1FV1qZv",
  19. "sq5RzZVjHxVKRN0CHSBn659XPb67PyMl",
  20. "rcj2WmWcPZGMDbmwDXJ69XQKGhyr6mCw",# 可以继续添加 防止一个key不够
  21. ]
  22. online_key_list_iter = iter(online_key_list)
  23. online_key = next(online_key_list_iter)
  24. version = "1.0.1" # 版本
  25. # 压缩的核心
  26. def compress_core(inputFile, outputFile, img_width):
  27. global online_key
  28. compresskey = online_key
  29. tinify.key = compresskey
  30. print "file = %s" % inputFile
  31. try:
  32. source = tinify.from_file(inputFile)
  33. if img_width is not -1:
  34. resized = source.resize(method="scale", width=img_width)
  35. resized.to_file(outputFile)
  36. else:
  37. source.to_file(outputFile)
  38. except tinify.AccountError, e:
  39. online_key = next(online_key_list_iter)
  40. compress_core(inputFile, outputFile, img_width) # 递归方法 继续读取
  41. except tinify.ClientError, e:
  42. print(e)
  43. pass
  44. # 仅压缩指定文件
  45. def compress_file(inputFile, width):
  46. print "compress_file-------------------------------------"
  47. if not os.path.isfile(inputFile):
  48. print "这不是一个文件,请输入文件的正确路径!"
  49. return
  50. basename = os.path.basename(inputFile)
  51. fileName, fileSuffix = os.path.splitext(basename)
  52. if fileSuffix == '.png' or fileSuffix == '.jpg' or fileSuffix == '.jpeg':
  53. compress_core(inputFile, inputFile, width)
  54. else:
  55. print "不支持该文件类型!"
  56. @click.command()
  57. @click.option('-d', "--dir", type=str, default='/', help="压缩文件夹下所有文件")
  58. @click.option('-f', "--file", type=str, default=None, help="单个文件压缩")
  59. @click.option('-s', "--size", type=str, default='1024k', help="需要压缩的文件大小")
  60. @click.option('-w', "--width", type=int, default=-1, help="图片宽度,默认不变")
  61. def run(dir, file, width, size):
  62. if file is not None:
  63. compress_file(file, width) # 仅压缩一个文件
  64. pass
  65. elif dir is not None:
  66. find_path = "find " + dir + " -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -type f -size +" + size + " | sort -nr"
  67. # print(find_path)
  68. result = os.popen(find_path).read().split('\n')
  69. for item in result:
  70. # print(item)
  71. compress_file(item, width)
  72. pass
  73. pass
  74. else:
  75. print '请指定要压缩的文件或文件夹'
  76. print "结束!"
  77. if __name__ == "__main__":
  78. run()