tinyimg.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "lC3jHypLlbSCrpCCL14qTNb960kTnbkf",# m@swz.com
  22. "pmYS1vP6nHhRYmZlGnmlCMlsBSrf1CVs",# 244591203@qq.com
  23. "NL7qKdJQ7LkY0N57xm8w9PF5QCc1Krq0",# mengmengde1@qq.com
  24. "THrhX541L3CGNZV0z3xw1n1qzd0tHkm8",# mengmengde1@qq.com
  25. "9kznMYZh85fHtyXsB6RRnyddX9qg7mQ4",# 18981831779@qq.com
  26. ]
  27. online_key_list_iter = iter(online_key_list)
  28. online_key = next(online_key_list_iter)
  29. version = "1.0.1" # 版本
  30. # 压缩的核心
  31. def compress_core(inputFile, outputFile, img_width):
  32. global online_key
  33. compresskey = online_key
  34. tinify.key = compresskey
  35. print "file = %s" % inputFile
  36. try:
  37. source = tinify.from_file(inputFile)
  38. if img_width is not -1:
  39. resized = source.resize(method="scale", width=img_width)
  40. resized.to_file(outputFile)
  41. else:
  42. source.to_file(outputFile)
  43. except tinify.AccountError, e:
  44. online_key = next(online_key_list_iter)
  45. compress_core(inputFile, outputFile, img_width) # 递归方法 继续读取
  46. except tinify.ClientError, e:
  47. print(e)
  48. pass
  49. # 仅压缩指定文件
  50. def compress_file(inputFile, width):
  51. print "compress_file-------------------------------------"
  52. if not os.path.isfile(inputFile):
  53. print "这不是一个文件,请输入文件的正确路径!"
  54. return
  55. basename = os.path.basename(inputFile)
  56. fileName, fileSuffix = os.path.splitext(basename)
  57. if fileSuffix == '.png' or fileSuffix == '.jpg' or fileSuffix == '.jpeg':
  58. compress_core(inputFile, inputFile, width)
  59. else:
  60. print "不支持该文件类型!"
  61. @click.command()
  62. @click.option('-d', "--dir", type=str, default='/', help="压缩文件夹下所有文件")
  63. @click.option('-f', "--file", type=str, default=None, help="单个文件压缩")
  64. @click.option('-s', "--size", type=str, default='1024k', help="需要压缩的文件大小")
  65. @click.option('-w', "--width", type=int, default=-1, help="图片宽度,默认不变")
  66. def run(dir, file, width, size):
  67. if file is not None:
  68. compress_file(file, width) # 仅压缩一个文件
  69. pass
  70. elif dir is not None:
  71. find_path = "find " + dir + " -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -type f -size +" + size + " | sort -nr"
  72. # print(find_path)
  73. result = os.popen(find_path).read().split('\n')
  74. for item in result:
  75. # print(item)
  76. compress_file(item, width)
  77. pass
  78. pass
  79. else:
  80. print '请指定要压缩的文件或文件夹'
  81. print "结束!"
  82. if __name__ == "__main__":
  83. run()