libpng warning iCCP 錯誤處理方法
阿新 • • 發佈:2021-10-30
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()