PIL库将图片位深度是1、8、32统一转换为24的方法

深度学习中通常遇到各种各样的图片,位深度有的时候各不相同,容易影响训练测试,因此为了避免麻烦,一般将图片统一为位深度是24

  1. 通用转换方法
python 复制代码
from PIL import Image

def convert_to_24bit(input_path, output_path):
    """
    将任意位深度的图片转换为24位RGB图像
    
    参数:
        input_path: 输入图片路径
        output_path: 输出图片路径
    """
    # 打开图片
    img = Image.open(input_path)
    
    # 转换为RGB模式(24位)
    rgb_img = img.convert('RGB')
    
    # 保存图片
    rgb_img.save(output_path)
    print(f"转换完成: {input_path} -> {output_path}")
    print(f"原始模式: {img.mode}, 转换后模式: {rgb_img.mode}")
    print(f"原始大小: {img.size}, 转换后大小: {rgb_img.size}")
    
    return rgb_img

# 使用示例
convert_to_24bit('input.png', 'output_24bit.jpg')
  1. 针对不同位深度的详细转换方法
python 复制代码
from PIL import Image
import numpy as np

def convert_specific_bit_depth(input_path, output_path):
    """
    针对不同位深度进行详细转换
    """
    img = Image.open(input_path)
    print(f"原始图像信息:")
    print(f"  模式: {img.mode}")
    print(f"  大小: {img.size}")
    print(f"  位深度: {img.bits} (如果可用)")
    
    # 根据原始模式进行相应处理
    if img.mode == '1':  # 1位黑白图像
        print("处理1位黑白图像...")
        # 方法1: 直接转换为RGB
        rgb_img = img.convert('RGB')
        
        # 方法2: 通过L模式中转(保留更多控制)
        # gray_img = img.convert('L')
        # rgb_img = gray_img.convert('RGB')
        
    elif img.mode == 'L':  # 8位灰度图像
        print("处理8位灰度图像...")
        # 将灰度转换为RGB(三个通道值相同)
        rgb_img = img.convert('RGB')
        
    elif img.mode == 'P':  # 调色板模式(通常是4位或8位)
        print("处理调色板图像...")
        # 先转换为RGB
        if img.palette:
            print(f"  调色板模式: {img.palette.mode}")
        rgb_img = img.convert('RGB')
        
    elif img.mode == 'RGBA':  # 32位RGBA图像
        print("处理32位RGBA图像...")
        # 创建白色背景
        background = Image.new('RGB', img.size, (255, 255, 255))
        # 将RGBA图像粘贴到白色背景上
        background.paste(img, mask=img.split()[3])  # 使用alpha通道作为掩码
        rgb_img = background
        
    elif img.mode == 'CMYK':  # 32位CMYK图像
        print("处理CMYK图像...")
        rgb_img = img.convert('RGB')
        
    elif img.mode == 'RGB':  # 已经是24位
        print("图像已经是24位RGB模式")
        rgb_img = img
        
    else:  # 其他模式
        print(f"处理未知模式: {img.mode}")
        rgb_img = img.convert('RGB')
    
    # 保存图像
    rgb_img.save(output_path, quality=95)
    print(f"转换完成 -> {output_path}")
    print(f"转换后模式: {rgb_img.mode}")
    
    return rgb_img
相关推荐
Learn-Python8 小时前
MongoDB-only方法
python·sql
小途软件9 小时前
用于机器人电池电量预测的Sarsa强化学习混合集成方法
java·人工智能·pytorch·python·深度学习·语言模型
扫地的小何尚10 小时前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
wanglei20070810 小时前
生产者消费者
开发语言·python
清水白石00811 小时前
《从零到进阶:Pydantic v1 与 v2 的核心差异与零成本校验实现原理》
数据库·python
昵称已被吞噬~‘(*@﹏@*)’~11 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战
2501_9418779811 小时前
从配置热更新到运行时自适应的互联网工程语法演进与多语言实践随笔分享
开发语言·前端·python
酩酊仙人11 小时前
fastmcp构建mcp server和client
python·ai·mcp
且去填词12 小时前
DeepSeek API 深度解析:从流式输出、Function Calling 到构建拥有“手脚”的 AI 应用
人工智能·python·语言模型·llm·agent·deepseek
rgeshfgreh12 小时前
Python条件与循环实战指南
python