# from PIL import Image
# import numpy as np
#
# # 读取图片(确保图片路径正确)
# image_path = '2/0001.png'
# img = Image.open(image_path).convert("RGBA")
#
# # 将图片转换为NumPy数组,方便数据分析
# data = np.array(img)
#
# # 提取透明通道(即Alpha通道)
# alpha_channel = data[:, :, 3]
#
# # 分析透明通道的特性
# # 1. 计算透明通道的最小值、最大值和平均值
# alpha_min = alpha_channel.min()
# alpha_max = alpha_channel.max()
# alpha_mean = alpha_channel.mean()
#
# # 2. 统计完全透明(0值)和完全不透明(255值)的像素数量
# transparent_pixels = np.sum(alpha_channel == 0)
# opaque_pixels = np.sum(alpha_channel == 255)
# total_pixels = alpha_channel.size
#
# # 输出分析结果
# print(f"Alpha通道最小值: {alpha_min}")
# print(f"Alpha通道最大值: {alpha_max}")
# print(f"Alpha通道平均值: {alpha_mean:.2f}")
# print(f"完全透明的像素数量: {transparent_pixels} ({(transparent_pixels / total_pixels) * 100:.2f}%)")
# print(f"完全不透明的像素数量: {opaque_pixels} ({(opaque_pixels / total_pixels) * 100:.2f}%)")
#
# print("\n")
from PIL import Image
import numpy as np
for i in range(10, 33):
# 读取图片(确保图片路径正确)
image_path = '5/00{}.png'.format(i)
img = Image.open(image_path).convert("RGBA")
# 将图片转换为NumPy数组,方便数据分析
data = np.array(img)
# 提取透明通道(即Alpha通道)
alpha_channel = data[:, :, 3]
# 分析透明通道的特性
# 1. 计算透明通道的最小值、最大值和平均值
alpha_min = alpha_channel.min()
alpha_max = alpha_channel.max()
alpha_mean = alpha_channel.mean()
# 2. 统计完全透明(0值)和完全不透明(255值)的像素数量
transparent_pixels = np.sum(alpha_channel == 0)
opaque_pixels = np.sum(alpha_channel == 255)
total_pixels = alpha_channel.size
# 输出分析结果
print(f"Alpha通道最小值: {alpha_min}")
print(f"Alpha通道最大值: {alpha_max}")
print(f"Alpha通道平均值: {alpha_mean:.2f}")
print(f"完全透明的像素数量: {transparent_pixels} ({(transparent_pixels / total_pixels) * 100:.2f}%)")
print(f"完全不透明的像素数量: {opaque_pixels} ({(opaque_pixels / total_pixels) * 100:.2f}%)")
print("\n")
# from PIL import Image
# import numpy as np
#
# # 读取图片(确保图片路径正确)
# image_path = '5/0001.png'
# img = Image.open(image_path).convert("RGBA")
#
# # 将图片转换为NumPy数组,方便数据分析
# data = np.array(img)
#
# # 提取透明通道(即Alpha通道)
# alpha_channel = data[:, :, 3]
#
# # 分析透明通道的特性
# # 1. 计算透明通道的最小值、最大值和平均值
# alpha_min = alpha_channel.min()
# alpha_max = alpha_channel.max()
# alpha_mean = alpha_channel.mean()
#
# # 2. 统计完全透明(0值)和完全不透明(255值)的像素数量
# transparent_pixels = np.sum(alpha_channel == 0)
# opaque_pixels = np.sum(alpha_channel == 255)
# total_pixels = alpha_channel.size
#
# # 输出分析结果
# print(f"Alpha通道最小值: {alpha_min}")
# print(f"Alpha通道最大值: {alpha_max}")
# print(f"Alpha通道平均值: {alpha_mean:.2f}")
# print(f"完全透明的像素数量: {transparent_pixels} ({(transparent_pixels / total_pixels) * 100:.2f}%)")
# print(f"完全不透明的像素数量: {opaque_pixels} ({(opaque_pixels / total_pixels) * 100:.2f}%)")
【Python】Picture`s Alpha Analyze
于 2024-10-11 14:37:41 首次发布