鉴于大家这么喜欢webp格式的图片,送大家个批量图片处理吧 Python

shuke 2018-5-3 1732

# coding:utf-8
# pip install pillow 安装第三方图片库 PILimport glob
import os
import threading

from PIL import Image


def create_image(infile, index):
  os.path.splitext(infile)
  im = Image.open(infile)
  im.save("img_webp/webp_" + str(index) + ".webp", "WEBP")


def start():
  index = 0  for infile in glob.glob("img/*.jpg"):
    t = threading.Thread(target=create_image, args=(infile, index,))
    t.start()
    t.join()
    index += 1if __name__ == "__main__":
  start()

效果很不错

最新回复 (1)
全部楼主
  • shuke 2018-5-3
    2

    现在再来一个完整版本的

    import glob
    import os
    import threading
    from PIL import Image
    def create_image(infile, index):
      os.path.splitext(infile)
      im = Image.open(infile)
      im.save("./img_webp/webp_" + str(index) + ".webp", "WEBP")
    def start():
      index = 0  for infile in glob.glob("img/*"):
        if os.path.isfile(infile):
          hz = infile.split(".")[1]
          if hz == "jpg" or hz == "png" or hz == "gif":
            t = threading.Thread(target=create_image, args=(infile, index,))
            t.start()
            t.join()
            index += 1      else:
            print(infile+" 不是图片格式,已跳过" )
    if __name__ == "__main__":
      start()


返回