How to read tif format files (based on PIL)

Background introduction

In many machine learning tasks, the training data sets of most image types are stored in tif format. In this case, how to read the data in tif format is crucial.


tif format

TIF (Tagged Image File Format) format, also known as TIFF, is a flexible bitmap format mainly used to store various types of pictures including photos and artistic images. The TIF format was first developed by Aldus in 1986 for high-resolution printers. It is now owned by Adobe. Here are some key features about the TIF format:

  •  Lossless Compression: TIF files can use lossless compression, which means that the quality of the image is not compromised during the compression and decompression process.
  •  High Quality: Because TIF supports lossless compression, it can save high-quality image data, making it ideal for professional-grade or commercial-grade images.
  • Color Depth: The TIF format supports multiple color depths, including black and white (1bit), grayscale (8bit), true color (24bit), and even 48bit or higher.
  • Large File Size: Since the TIF format focuses on image quality, TIF files usually have larger file sizes when using lossless compression or no compression.
  • Metadata: The TIF format supports rich metadata, including but not limited to date, time, author, copyright, photography equipment information, etc.
  • Multiple Pages: TIF format files support storing multiple page images, which makes some scanning software like to use this format to store multi-page scan results. In actual use, the TIF format is widely used in various fields that require high-quality images, such as publishing, photography, printing, and design.

Reading of tif format files

Here we use python's PTL library to read files in tif format.

Environment dependent libraries

from PIL import Image # 读取tif格式文件
import numpy as np    # 将PIL.Image转换为numpy类型
import matplotlib.pyplot as plt  # 对图像进行可视化

What we read here is the training data set for retinal blood vessel separation, and the images are stored in tif format.

data_path = r"你的tif格式文件路径"
data = Image.open(data_path)
print(data)

Output results

<PIL.TiffImagePlugin.TiffImageFile image mode=RGB size=565x584 at 0x1FA40F099D0>

This is not very intuitive. We convert the data into numpy type and print out the shape of the tif format image.

data_np = np.array(data)
print(data_np.shape)

Result output

(584, 565, 3)

You can intuitively see that the read tif is a 584\times 565three-channel image of one pixel

Visualization

Let’s display the read data and see

plt.imshow(data)
plt.show()

Note that plt.imshow here can directly display data in PIL.Image format, and can also pass in data converted to numpy format for visualization.

Everyone is welcome to discuss and exchange~


Guess you like

Origin blog.csdn.net/weixin_57506268/article/details/135371197