Convert .tiff format pictures into visual png, jpg, bmp and other formats (code attached)

Currently commonly used .tiff format images are converted to png format images. After searching, most of them are directly converted by third-party platforms, which requires meters. In fact, it is not necessary. You can directly convert by coding yourself.

TIFF (Tagged Image File Format) is a flexible bitmap format mainly used to store images including photos and artwork. It was originally developed by Aldus Corporation in conjunction with Microsoft Corporation for PostScript printing. TIFF joins JPEG and PNG as a popular high-bit color image format. TIFF is a flexible and adaptable file format that can handle multiple images and data in one file by including "tags" in the file header.

1. .tiff format pictures

When you directly use computer tools to open .tiff format pictures on a Windows computer, you can only see white. This is because the pixel value of .tiff format pictures exceeds 255, and they are forced to be converted to 255 display when displayed. Just pick a .tiff format picture. Pictures and viewing pixel values ​​are as follows:
Insert image description here

Insert image description here

In order to visually view the content in the picture, it needs to be converted to a monitor that can display png, jpg, bmp and other visual displays normally.

2. tiff to png, jpg code

2.1 Code environment preparation

Using this code requires the Pytorch environment to run properly. For Pytorch installation, please refer to the blog post:Pytorch installation. The link to the Pytorch official website is: < /span>Pytorch official website installation

My own Pytorch version and Python version number are referenced as follows:

Insert image description here

2.2 Modification of code parameters

When scholars use the code, they only need to modify the image path and the path to save the image.

Insert image description here

2.3 Weight file

The weight file in the source code package I provided, the specific location is as follows, in the isp file:

Insert image description here

2.4 Single frame conversion code

The following is the complete code for converting tiff to png format. This code is the tif_To_png.py file in the source code package I provided.
Insert image description here


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()

To run the above code normally, you also need a script file models.py. I put this code in the source code package for scholars to download and use.
Insert image description here

2.4.1 Source code package download

The download link of source code package is:source code package, extraction code: 5kh2

I have provided 5 test images in .tiff format in the source code package, in the tif_images folder, as follows:
Insert image description here

2.5 Batch multi-frame conversion code

Scholars use the following code and only need to modify two places, see below:
Insert image description here
Insert image description here


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. Conversion effect

3.1 Conversion test scenario 1

Insert image description here

3.2 Conversion test scenario 2

Insert image description here

3.3 Conversion test scenario 3

Insert image description here

3.4 Conversion test scenario 4

In this scenario, the original .tiff image is the noise image.
Insert image description here

3.5 Conversion test scenario 5

Insert image description here

3.6 Batch conversion effect

Insert image description here

Through the above multiple scene tests, we can see that it can be converted back to the original color normally, and the image details are all present.

4. Summary

The above is the complete process of converting .tiff format pictures into visual png, jpg, bmp and other formats. In the sample code I provided, it mainly converts .tiff format pictures into .png format. If scholars want to convert to other formats For pictures, you only need to modify the suffix when saving the picture.

Among the four scenarios I tested above, the noise in some pictures is not due to conversion. It is the noise image in the original .tiff format. There is no need to worry about the details of the original image being changed after conversion.

It’s not easy to summarize. Thank you for your support!

Guess you like

Origin blog.csdn.net/qq_40280673/article/details/134778095