• 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

Python – 解决模型训练时读取png图片libpng warning: iccp: known incorrect srgb profile的问题

Python 发布于2025-03-13 阅读 1,344次 0次评论 0次点赞 本文共2306个字,阅读需要6分钟。

1 libpng warning: iccp: known incorrect srgb profile

今天在微调模型进行训练时,我的微调数据中有png数据,在数据加载每次读取png图片时都会报libpng warning: iccp: known incorrect srgb profile的warning,非常影响正常查看训练时的loss情况。

这个原因主要是因为libpng-1.6以上版本增强了ICC profiles检查,所以发出警告。此警告可以忽略,若要消除警告则要从原图像中去掉ICCP chunk。

所以我写了以下代码去除png图片的icc信息,

import shutil
from PIL import Image

def remove_image_icc_profile_info(input_image_path, output_image_path):
    if not os.path.exists(input_image_path):
        raise FileNotFoundError(f'{input_image_path} is not exist')

    with Image.open(input_image_path) as img:
        # Remove the ICC profile
        data = list(img.getdata())
        img_without_profile = Image.new(img.mode, img.size)
        img_without_profile.putdata(data)

        # Save the image without the ICC profile
        img_without_profile.save(output_image_path, format='PNG')

通过仅读取图片的图像数据另外存储到另一个图片文件中,就可以去除png图片多余的icc信息。

如果要批量处理一个文件夹下所有的png图片,可使用以下批量处理代码

'''
移除png的icc profile信息,避免opencv读取时出现错误
'''

import os
import shutil
from PIL import Image
import cv2
import glob
import numpy as np

def remove_image_icc_profile_info(input_image_path, output_image_path):
    if not os.path.exists(input_image_path):
        raise FileNotFoundError(f'{input_image_path} is not exist')

    with Image.open(input_image_path) as img:
        # Remove the ICC profile
        data = list(img.getdata())
        img_without_profile = Image.new(img.mode, img.size)
        img_without_profile.putdata(data)

        # Save the image without the ICC profile
        img_without_profile.save(output_image_path, format='PNG')

        #cv2_img = cv2.imdecode(np.fromfile(output_image_path, dtype=np.uint8), cv2.IMREAD_COLOR)


def remove_dir_all_image_icc_profile_info(input_image_dir, output_image_dir):
    if not os.path.exists(input_image_dir):
        raise FileNotFoundError(f'{input_image_dir} is not exist')

    if not os.path.exists(output_image_dir):
        os.makedirs(output_image_dir)
    else:
        shutil.rmtree(output_image_dir)
        os.makedirs(output_image_dir)

    image_extensions = ('*.jpg', '*.jpeg', '*.png', '*.gif')
    input_image_dir_image_list = sorted([img for ext in image_extensions for img in glob.glob(f'{input_image_dir}/{ext}')])

    for i, image_path in enumerate(input_image_dir_image_list):
        print(f'process image {i}/{len(input_image_dir_image_list)}')
        image_name = os.path.basename(image_path)
        output_image_path = os.path.join(output_image_dir, image_name)

        remove_image_icc_profile_info(image_path, output_image_path)

if __name__ == '__main__':
    input_image_dir = './input_images'
    output_image_dir = './input_images_no_icc_profile_info'
    remove_dir_all_image_icc_profile_info(input_image_dir, output_image_dir)

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Python – 解决模型训练时读取png图片libpng warning: iccp: known incorrect srgb profile的问题

原文链接:https://www.stubbornhuang.com/3134/

发布于:2025年03月13日 17:45:03

修改于:2025年03月13日 17:45:03

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
使用自定义数据集微调Real-ESRGAN图片超分模型
图片超分
下一篇
Real-ESRGAN在模型训练和模型微调时打印的各个loss项所代表的含义
图片超分
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号