修改tools/jpg格式转换
This commit is contained in:
parent
5815969878
commit
a989385b41
@ -1,14 +1,41 @@
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
from multiprocessing import Pool
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
def convert_image(file_name, img_dir, output_dir, convert_format):
|
||||||
|
input_path = os.path.join(img_dir, file_name)
|
||||||
|
output_path = os.path.join(output_dir, file_name.replace(".jpg", f".{convert_format}"))
|
||||||
|
|
||||||
|
# 打开并转换图像
|
||||||
|
img = Image.open(input_path)
|
||||||
|
img.save(output_path)
|
||||||
|
|
||||||
|
def main():
|
||||||
convert_format = "tif"
|
convert_format = "tif"
|
||||||
img_dir = r"E:\datasets\UAV\134\project\images"
|
img_dir = r"E:\datasets\UAV\134\project\images"
|
||||||
output_dir = r"E:\datasets\UAV\134_tif\project\images"
|
output_dir = r"E:\datasets\UAV\134_tif\project\images"
|
||||||
|
|
||||||
for file_name in os.listdir(img_dir):
|
# 如果输出目录存在,先删除
|
||||||
img = Image.open(os.path.join(img_dir, file_name))
|
if os.path.exists(output_dir):
|
||||||
if convert_format == "png":
|
shutil.rmtree(output_dir)
|
||||||
img.save(os.path.join(output_dir, file_name.replace(".jpg", ".png")))
|
|
||||||
elif convert_format == "tif":
|
|
||||||
img.save(os.path.join(output_dir, file_name.replace(".jpg", ".tif")), "TIFF")
|
|
||||||
|
|
||||||
|
# 创建输出目录
|
||||||
|
os.makedirs(output_dir)
|
||||||
|
|
||||||
|
# 获取所有文件名
|
||||||
|
file_names = os.listdir(img_dir)
|
||||||
|
|
||||||
|
# 创建部分函数,固定除文件名外的其他参数
|
||||||
|
convert_partial = partial(convert_image,
|
||||||
|
img_dir=img_dir,
|
||||||
|
output_dir=output_dir,
|
||||||
|
convert_format=convert_format)
|
||||||
|
|
||||||
|
# 使用进程池并行处理
|
||||||
|
with Pool() as pool:
|
||||||
|
pool.map(convert_partial, file_names)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user