opencv-python将.tif格式的图像转为png格式

在做UNET的眼球血管分割的时候,眼球的图像使用的tif格式存储的,习惯了使用jpg或者png了,先转一下。直接用opencv读进来以后重写一下就可以。

# !/usr/bin/python3
# -*- coding:utf-8 -*-
# Author:WeiFeng Liu
# @Time: 2021/12/9 下午6:41

import cv2
import numpy as np
import os

def tif_to_png(image_path,save_path):
    """
    :param image_path: *.tif image path
    :param save_path: *.png image path
    :return:
    """
    img = cv2.imread(image_path,3)
    # print(img)
    # print(img.dtype)
    filename = image_path.split('/')[-1].split('_')[0]
    # print(filename)
    save_path = save_path + '/' + filename + '.png'
    cv2.imwrite(save_path,img)

if __name__ == '__main__':
    root_path = r'视网膜血管数据集/training/images/'
    save_path = r'Dataset/train/image/'
    image_files = os.listdir(root_path)
    for image_file in image_files:
        tif_to_png(root_path + image_file,save_path)


你可能感兴趣的:(图像处理,opencv,python,计算机视觉)