1. 程式人生 > >【python】批量轉換圖片格式tif--png

【python】批量轉換圖片格式tif--png

###1.配置wand
windows 下實現圖片格式轉換,需要安裝一個exe,在此下載
其中安裝的時候要注意:
一定選擇Install development headers and libraries for C and C++
這裡寫圖片描述
###2.程式碼實現
我在這裡實現的是tif轉為png格式

from wand.image import Image
import os
# from PIL import Image # 一開始在這裡報錯是因為import 一個檔案的時候,不能重名,在windows下需要安裝一個exe
def get_imlist(path):
    """返回目錄中所有tif影象的檔名列表"""
    return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(".tif")]
if __name__ == '__main__':
    path = "G:/Test/6-28/HBsAg_tif/"
    listdir = get_imlist(path)

    for dir in listdir:
        print(dir)
        with Image(filename = str(dir)) as img:
            img.resize(4096,4096) # width, height
            # 存的目錄為"G:/Test/6-28/HBsAg_png/",用了一步replace,換了個目錄
            img.save(filename = (str(dir)[:-3]+'png').replace("HBsAg_tif","HBsAg_png")) # png, jpg, bmp, gif, tiff All OK--