1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【python】批量转换图片格式tif--png

【python】批量转换图片格式tif--png

时间:2022-04-24 23:29:34

相关推荐

【python】批量转换图片格式tif--png

1.配置wand

windows 下实现图片格式转换,需要安装一个exe,在此下载

其中安装的时候要注意:

一定选择Install development headers and libraries for C and C++

2.代码实现

我在这里实现的是tif转为png格式

from wand.image import Imageimport os# from PIL import Image # 一开始在这里报错是因为import 一个文件的时候,不能重名,在windows下需要安装一个exedef 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--

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。