【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--

转载自:https://blog.csdn.net/ACBattle/article/details/80800747

You may also like...

退出移动版