libpng warning iCCP 错误处理方法

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

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

一些可能的解决方案:

已有方案

来自: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)

本文提供方案

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

文件结构:

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

实现功能:

重命名文件夹,重命名文件名称(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()

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

在这里插入图片描述

这些无法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() 
posted @ 2021-10-30 19:20  pprp  阅读(3993)  评论(0编辑  收藏  举报