自動辨識圖像格式可視化
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)