自动辨识图像格式可视化
import numpy as np
import matplotlib.pyplot as plt
from PIL import Imagedef convert_to_numpy(image_input):"""自动检测输入图像类型,并将其转换为NumPy数组。"""if isinstance(image_input, np.ndarray):# 输入已经是NumPy数组,直接返回return image_inputelif 'Tensor' in str(type(image_input)):# 输入是Tensor类型# 检查是否需要转换(依赖于Tensor所属的库,如PyTorch, TensorFlow等)if hasattr(image_input, 'detach'):# 假设是PyTorch Tensorimage_input = image_input.detach().cpu().numpy()else:# 假设是TensorFlow Tensor或其他框架的Tensorimage_input = image_input.numpy()# 如果Tensor有通道维度在最前面(如CHW),则需要转换为HWCif image_input.ndim == 3 and image_input.shape[0] in (1, 3):image_input = image_input.transpose(1, 2, 0)elif isinstance(image_input, Image.Image):# 输入是Pillow图像,转换为NumPy数组image_input = np.array(image_input)else:raise TypeError("Unsupported image type")# 如果图像是单通道的,且在最后一个维度(例如HxWx1),去掉该维度if image_input.ndim == 3 and image_input.shape[-1] == 1:image_input = image_input.squeeze(-1)image_np = image_input if image_np.ndim == 3 and image_np.shape[-1] == 3:plt.imshow(image_np)else:plt.imshow(image_np, cmap='viridis')plt.title(title)plt.axis('off')plt.show()def visualize_image(image_np, title="Image"):"""可视化NumPy格式的图像"""if image_np.ndim == 3 and image_np.shape[-1] == 3:plt.imshow(image_np)else:plt.imshow(image_np, cmap='gray')plt.title(title)plt.axis('off')plt.show()# 示例使用
# image_tensor, image_np, image_pil 分别代表Tensor, NumPy数组, Pillow图像的输入
# 将它们转换为NumPy数组
# image_np = convert_to_numpy(image_tensor)
# image_np = convert_to_numpy(image_np)
# image_np = convert_to_numpy(image_pil)# # 可视化图像
# visualize_image(image_np)
可视化
张量可视化
import torch
from torchvision.transforms.functional import to_pil_image
from PIL import Image
def tensor_to_pil(tensor):# 确保tensor是在CPU上tensor = tensor.cpu()# 如果tensor有一个批次维度,去除它if tensor.dim() == 4 and tensor.shape[0] == 1:tensor = tensor.squeeze(0)# 转换为PIL图像pil_image = to_pil_image(tensor)# 返回PIL图像return pil_image
tensor_to_pil( ).show()
可视化已经图像信息
def draw_np(pic_np):pic_np = np.squeeze(pic_np)plt.imshow(pic_np)# 隐藏坐标轴plt.axis('on')# 显示数据标尺plt.colorbar()# 显示图像plt.show()
def get_image_info(image):# 获取图像的模式、格式和尺寸mode = image.modeformat_ = image.formatsize = image.size# 根据图像模式推断每个通道的位数if mode in ("1", "L", "P"):bits_per_channel = 8 # 通常是8位elif mode == "RGB":bits_per_channel = 8 # 通常是8位,3通道elif mode == "RGBA":bits_per_channel = 8 # 通常是8位,4通道elif mode == "I":bits_per_channel = 32 # 整数像素模式elif mode == "F":bits_per_channel = 32 # 浮点像素模式else:bits_per_channel = 'unknown' # 未知或不常见的模式# 计算总位数total_bits = image.getbands().__len__() * bits_per_channel# 打印图像信息print(f"Image mode: {mode}")print(f"Image format: {format_}")print(f"Image size: {size}")print(f"Bits per channel: {bits_per_channel}")print(f"Total bits per pixel: {total_bits}")#%%
import numpy as npdef get_array_info(np_array):"""获取并打印NumPy数组的详细信息。参数:np_array: NumPy数组。"""# 获取数组的形状shape = np_array.shape# 获取数组的总元素数量size = np_array.size# 获取数组的数据类型dtype = np_array.dtype# 获取数组单个元素的大小(以字节为单位)itemsize = np_array.itemsize# 获取数组的维度数量ndim = np_array.ndim# 获取数组的总字节数nbytes = np_array.nbytes# 打印数组信息print(f"Array Shape: {shape}")print(f"Array Size: {size}")print(f"Array Data Type: {dtype}")print(f"Item Size: {itemsize} bytes")print(f"Array Dimensions: {ndim}")print(f"Total Bytes: {nbytes} bytes")
def read_pic(path_pic):# 加载图像image = Image.open(path_pic)print(image.size)print(image.format)return imagedef pic_to_np(pic):np_depth = np.array(pic)return np_depthdef draw_np(pic_np):pic_np = np.squeeze(pic_np)plt.imshow(pic_np)# 隐藏坐标轴plt.axis('on')# 显示数据标尺plt.colorbar()# 显示图像plt.show()def pic_info(path):raw_image = read_pic(path)raw_np = pic_to_np(raw_image)get_image_info(raw_image)get_array_info(raw_np)raw_image.show()draw_np(raw_np)