当前位置:   article > 正文

libpng warning iCCP 错误处理方法

libpng warning

png图片缺乏某些库,导致损坏,或者多余了一些数据会导致以下报错:

libpng warning: iCCP: known incorrect sRGB profile
libpng warning iccp extra compressed data

  • 1
  • 2
  • 3

一些可能的解决方案:

已有方案

来自:https://blog.csdn.net/qq_37924224/article/details/119181028

libpng 1.6及以上版本增强了ICC profiles检查,所以发出警告。从 PNG 图像中删除 iCCP profiles。

可以先读取,再重新保存:

import cv2
from skimage import io
image = io.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGRA)
cv2.imencode('.png',image)[1].tofile(path)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

本文提供方案

观察到报错的图片根本无法打开,选择删除对应图片。

文件结构:

- all
  - train
    - folder1
      - img1.png
      - ....
      - imgn.png
    - foler2
      - ...
  - test
    - folder1
      - img1.png
      - ....
      - imgn.png
    - foler2
      - ...

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

实现功能:

重命名文件夹,重命名文件名称(ps: 原来是有中文的),使用convert命令转换png为jpg

import os
import cv2 
import random 

dir_path = "all/train"


def rename_folder():
    for idx, subdir in enumerate(os.listdir(dir_path)):
        new_folder = "%s_" % (str(random.randint(0,10000))) + str(idx)
        whole_subdir = os.path.join(dir_path, subdir)
        whole_newdir = os.path.join(dir_path, new_folder)
        # print(f"rename {whole_subdir} to {whole_newdir}")
        os.rename(whole_subdir, whole_newdir)


def rename():
    # rename files
    for subdir in os.listdir(dir_path):
        whole_path = os.path.join(dir_path, subdir)
        for idx, img_name in enumerate(os.listdir(whole_path)):
            postfix = "png" if img_name.endswith("png") else "jpg"
            new_img_name = str(idx) + "." + postfix

            org_path = os.path.join(whole_path, img_name)
            new_path = os.path.join(whole_path, new_img_name)

            #     print(f"rename {org_path} to {new_path}")
            os.rename(org_path, new_path)


def convert_png2jpg():
    # convert
    for subdir in os.listdir(dir_path):
        whole_path = os.path.join(dir_path, subdir)
        for img_name in os.listdir(whole_path):
            img_whole_path = os.path.join(whole_path, img_name)
            if img_name.endswith("png"):
                # print(img_whole_path)
                img = cv2.imread(img_whole_path)
                cv2.imwrite(img_whole_path, img)
                os.system(
                    "convert %s %s"
                    % (img_whole_path, img_whole_path.replace("png", "jpg"))
                )


def main():
    # run with nohup
    # 1. rm nohup.out
    # 2. nohup python convert_png.py
    rename_folder()
    rename()
    convert_png2jpg()


if __name__ == "__main__":
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

这个时候有可能会出现报错:

在这里插入图片描述

这些无法convert的文件就是需要删除的对象,通过正则化方法匹配对应文件, 并删除。

import os


def main():
    rm_list = greps()
    rm(rm_list)


def greps():
    import re

    rm_list = []
    file = "nohup.out"
    f = open(file, "r")
    for line in f.readlines():
        res = re.findall("`.+'", line)
        if res:
            rm_list.append(*res)
    return rm_list


def rm(rm_list):
    rm_list = [item.lstrip("`").rstrip("'") for item in rm_list]
    for item in rm_list:
        print("rm: ", item)
        os.system("rm %s" % item)


if __name__ == "__main__":
    main() 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/721990
推荐阅读
相关标签
  

闽ICP备14008679号