将.tiff格式图片转换为可视化的png,jpg,bmp等格式(附代码)

36 篇文章 14 订阅
11 篇文章 2 订阅

目前常用的.tiff格式图片转png格式图片,搜了一下,多数都是第三方平台直接转换,需要米,其实大可不必,自己撸代码就可以直接转换。

TIFF(Tagged Image File Format)是一种灵活的位图格式,主要用来存储包括照片和艺术图在内的图像。它最初由Aldus公司与微软公司一起为PostScript打印开发。TIFF与JPEG和PNG一起成为流行的高位彩色图像格式。TIFF是一个灵活适应性强的文件格式,通过在文件头中包含“标签”它能够在一个文件中处理多幅图像和数据。

一、.tiff格式图片

在Windows电脑端直接使用电脑工具打开.tiff格式图片,只看得到白色,这是因为.tiff格式的图片像素值超过255了,显示时都被强制转为了255显示,随便挑一副.tiff格式图片及查看像素值,如下:
在这里插入图片描述

在这里插入图片描述

为了直观查看图片中内容,需要转换为显示器能正常显示了png,jpg,bmp等可视化显示。

二、tiff转png,jpg代码

2.1 代码环境准备

使用此代码需要在Pytorch环境中才能正常运行,Pytorch的安装参考博文:Pytorch安装,Pytorch官网链接为:Pytorch官网安装

我自己的Pytorch版本及Python版本号参考如下:

在这里插入图片描述

2.2 代码参数修改

学者使用代码时只需修改图片路径和保存图片的路径即可。

在这里插入图片描述

2.3 权重文件

在我提供的源码包中权重文件,具体位置如下,在isp文件中:

在这里插入图片描述

2.4 单帧转换代码

下面是tiff转换png格式的完整代码,该代码就是我提供的源码包中的tif_To_png.py文件。
在这里插入图片描述


import cv2
import numpy as np
import torch

def pack_gbrg_raw(raw):                                               # 定义一个名为pack_gbrg_raw的函数,它接受一个参数raw,这个参数是一个GBRG格式的Bayer原始图像
    #pack GBRG Bayer raw to 4 channels
    black_level = 240                                                 # 定义黑色和白色的级别。这两个值用于后续的图像归一化。
    white_level = 2**12-1
    im = raw.astype(np.float32)                                       # 将输入的原始图像转换为浮点数类型
    im = np.maximum(im - black_level, 0) / (white_level-black_level)  # 对图像进行归一化处理,使其值在0到1之间

    im = np.expand_dims(im, axis=2)                                   # 在第三个维度(即通道维度)上为图像增加一个维度。
    img_shape = im.shape
    H = img_shape[0]                                                  # 获取图像的形状,并将高度和宽度分别存储在H和W中。
    W = img_shape[1]

    out = np.concatenate((im[1:H:2, 0:W:2, :],          # r           # 将图像的四个通道(R,Gr,B,Gb)沿着第三个维度(即通道维度)进行拼接。
                          im[1:H:2, 1:W:2, :],          # gr
                          im[0:H:2, 1:W:2, :],          # b
                          im[0:H:2, 0:W:2, :]), axis=2) # gb
    return out

def tensor2numpy(raw):  # raw: 1 * 4 * H * W
    input_full = raw.permute((0, 2, 3, 1))   # 1 * H * W * 4
    input_full = input_full.data.cpu().numpy()
    output = np.clip(input_full,0,1)
    return output

def preprocess(raw):
    input_full = raw.transpose((0, 3, 1, 2))
    input_full = torch.from_numpy(input_full)
    input_full = input_full.cuda()
    return input_full


img = cv2.imread("tif_images/4.tiff",-1)                           # 读入tiff格式图片
img = np.expand_dims(pack_gbrg_raw(img), axis=0)

isp = torch.load('isp/ISP_CNN.pth')

test_gt = (img - 240) / (2 ** 12 - 1 - 240)

gt_raw_frame = test_gt * (2 ** 12 - 1 - 240) + 240
gt_srgb_frame = tensor2numpy(isp(preprocess(gt_raw_frame)))[0]

img_png = np.uint8(gt_srgb_frame * 255)

cv2.imwrite("result_images/4.png",img_png)                         # 保存转换后的png或jpg图片路径及图片名称
cv2.imshow("img",img_png)
cv2.waitKey()
cv2.destroyAllWindows()

想要正常运行上面代码,还需要一个脚本文件models.py,该代码我放在了源码包中,学者自行下载使用。
在这里插入图片描述

2.4.1 源码包下载

源码包的下载链接为:源码包,提取码:5kh2

源码包中我提供了5张.tiff格式的测试图片,在tif_images文件夹中,如下:
在这里插入图片描述

2.5 批量多帧转换代码

学者使用下面代码,只需要修改两个地方即可,见下:
在这里插入图片描述
在这里插入图片描述


import cv2
import numpy as np
import torch
import os
import sys

def pack_gbrg_raw(raw):                                               # 定义一个名为pack_gbrg_raw的函数,它接受一个参数raw,这个参数是一个GBRG格式的Bayer原始图像
    #pack GBRG Bayer raw to 4 channels
    black_level = 240                                                 # 定义黑色和白色的级别。这两个值用于后续的图像归一化。
    white_level = 2**12-1
    im = raw.astype(np.float32)                                       # 将输入的原始图像转换为浮点数类型
    im = np.maximum(im - black_level, 0) / (white_level-black_level)  # 对图像进行归一化处理,使其值在0到1之间

    im = np.expand_dims(im, axis=2)                                   # 在第三个维度(即通道维度)上为图像增加一个维度。
    img_shape = im.shape
    H = img_shape[0]                                                  # 获取图像的形状,并将高度和宽度分别存储在H和W中。
    W = img_shape[1]

    out = np.concatenate((im[1:H:2, 0:W:2, :],          # r           # 将图像的四个通道(R,Gr,B,Gb)沿着第三个维度(即通道维度)进行拼接。
                          im[1:H:2, 1:W:2, :],          # gr
                          im[0:H:2, 1:W:2, :],          # b
                          im[0:H:2, 0:W:2, :]), axis=2) # gb
    return out

def tensor2numpy(raw):  # raw: 1 * 4 * H * W
    input_full = raw.permute((0, 2, 3, 1))   # 1 * H * W * 4
    input_full = input_full.data.cpu().numpy()
    output = np.clip(input_full,0,1)
    return output

def preprocess(raw):
    input_full = raw.transpose((0, 3, 1, 2))
    input_full = torch.from_numpy(input_full)
    input_full = input_full.cuda()
    return input_full

img_path = "CRVD_dataset/indoor_raw_noisy/indoor_raw_noisy_scene7/scene7/ISO1600"                        # tiff格式图片路径
save_path = "CRVD_dataset/indoor_raw_noisy/indoor_raw_noisy_scene7/scene7/ISO1600_png"                   # 转换后的保存路径

file = os.listdir(path=img_path)

for item in file:
    img = cv2.imread(os.path.join(img_path,item),-1)

    img = np.expand_dims(pack_gbrg_raw(img), axis=0)

    isp = torch.load('isp/ISP_CNN.pth')
    test_gt = (img - 240) / (2 ** 12 - 1 - 240)                   # 计算地面真实图像的标准化值
    gt_raw_frame = test_gt * (2 ** 12 - 1 - 240) + 240            # 计算输出的最终值
    gt_srgb_frame = tensor2numpy(isp(preprocess(gt_raw_frame)))[0]   # 将预处理后的图像转换为sRGB格式

    img_png = np.uint8(gt_srgb_frame * 255)


    out_file = item[:-5] + ".png"
    print('图片{}转换为png成功!'.format(item))

    cv2.imwrite(os.path.join(save_path,out_file), img_png)

    key = cv2.waitKey(30) & 0xff
    if key == 27:
        sys.exit(0)

三、转换效果

3.1 转换测试场景1

在这里插入图片描述

3.2 转换测试场景2

在这里插入图片描述

3.3 转换测试场景3

在这里插入图片描述

3.4 转换测试场景4

该场景下,原始的.tiff图像就是噪声图。
在这里插入图片描述

3.5 转换测试场景5

在这里插入图片描述

3.6 批量转换效果

在这里插入图片描述

通过以上多场景测试,可以看到能够正常的转换回原始的色彩,图像细节都在。

四、总结

以上就是将.tiff格式图片转换为可视化的png,jpg,bmp等格式的完整过程,在我提供的示例代码中主要是将.tiff格式图片转为.png格式,学者如果想转为其它格式的图片,只需要在保存图片时修改后缀即可。

我上面测试的四个场景中,有的图片有噪声不是转换后才有的,是原始的.tiff格式就是噪声图,不用担心转换后改变原始图像细节。

总结不易,多多支持,谢谢!

  • 25
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用MATLAB中的imwrite函数来将jpg图片转换tiff格式。使用方法如下: 首先,读取jpg图片: ``` img = imread('image.jpg'); ``` 然后使用imwrite函数将图片保存为tiff格式: ``` imwrite(img, 'image.tiff'); ``` 如果要保存为压缩的tiff格式,可以使用以下代码: ``` imwrite(img, 'image.tiff', 'compression', 'jpeg'); ``` 其中,compression参数指定压缩算法,可以设置为'jpeg'(表示使用JPEG压缩算法)、'packbits'(表示使用PackBits压缩算法)或'none'(表示不压缩)。 希望这些信息能帮到你! ### 回答2: 用MATLAB可以很方便地将jpg图片格式转换tiff格式。下面是一个简单的示例代码: ```matlab % 读入jpg图片 img = imread('input.jpg'); % 获取输入图片的信息 info = imfinfo('input.jpg'); % 将jpg格式转换tiff格式 imwrite(img, 'output.tiff', 'Compression', 'lzw', 'ResolutionUnit', info.Unit, 'XResolution', info.XResolution, 'YResolution', info.YResolution); % 打印转换功的消息 disp('转换功!'); ``` 代码步骤解析: 1. 使用`imread`函数读入jpg图片,将图片数据存储在`img`变量中。 2. 使用`imfinfo`函数获取输入图片的信息,存储在`info`变量中,这些信息包括单位和分辨率等。 3. 使用`imwrite`函数将`img`数据以tiff格式写入到文件`output.tiff`中,`Compression`参数设为`lzw`表示使用LZW压缩算法。 4. 将`tiff`图片的单位和分辨率等信息设置为与输入图片相同,使用`info.Unit`,`info.XResolution`和`info.YResolution`等参数。 5. 使用`disp`函数打印转换功的消息。 以上代码示例了如何使用MATLAB将jpg图片格式转换tiff格式,你可以替换`input.jpg`为你自己的jpg图片文件路径,然后运行代码就可以得到你想要的tiff格式图片了。 ### 回答3: 可以使用MATLAB的Image Processing Toolbox中的imread函数来读取jpg图片,并使用imwrite函数将其转换tiff格式。 下面是一个简单的MATLAB代码示例: ```matlab % 读取jpg图片 image = imread('input.jpg'); % 将jpg图片转换tiff格式并保存 imwrite(image, 'output.tiff'); ``` 在这个示例中,首先使用imread函数读取名为input.jpgjpg图片,并将其存储在名为image的变量中。然后,使用imwrite函数将这个图片变量以tiff格式保存到名为output.tiff的文件中。 在实际使用时,你需要将'input.jpg'替换为你实际的jpg图片路径,并将'output.tiff'替换为你想要保存的tiff图片路径。 需要注意的是,MATLAB中的imwrite函数支持多种图片格式转换,包括tiff格式。你可以根据需要在函数的第二个参数中指定想要的图片格式。 希望以上回答对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐咖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值