剔除文件夹中无效图片--支持文件夹中有中文名称(或特殊符号)--且保持目录结构

在工作学习时,有时候你搜集的各种网络上的图片,有些是损坏的,或者是图片非常小,不可读。这些图片,应该被过滤掉。并且我们希望,过滤后的图片,保持原有的目录结构,还需要原来的图片名称乱码、中文之类的,都可以解决。

此时,你可以用下面的代码。

对于不支持的名称编码,直接转成ascii编码了,所以名称是没有保持的。

import os
from PIL import Image
import re
import unicodedata

# set the directory path
dir_path = "/home/work/imagenet"

# create a new directory for the converted images
converted_dir_path = "/home/work/imagenet_converted"
if not os.path.exists(converted_dir_path):
    os.mkdir(converted_dir_path)


# loop over all subdirectories in the directory
for subdir_name in os.listdir(dir_path):
    subdir_path = os.path.join(dir_path, subdir_name)
    if not os.path.isdir(subdir_path) or subdir_name.startswith("."):
        continue  # skip if it's not a non-hidden directory

    # create a new subdirectory in the converted directory for the current category
    converted_subdir_path = os.path.join(converted_dir_path, subdir_name)
    if not os.path.exists(converted_subdir_path):
        os.makedirs(converted_subdir_path)

    # loop over all files in the subdirectory
    for file_name in os.listdir(subdir_path):
        # skip hidden files
        if file_name.startswith("."):
            continue
        # replace any special characters, spaces, and Chinese characters with underscores
        new_file_name = re.sub(r'[^\w\s-]', '', file_name)
        new_file_name = unicodedata.normalize("NFKD", new_file_name).encode("ascii", "ignore").decode("ascii")
        new_file_name = new_file_name.strip().replace(" ", "_")
        # check if the file is an image
        if not new_file_name.lower().endswith(".jpg") and not new_file_name.lower().endswith(".jpeg"):
            # open the image using PIL
            image_path = os.path.join(subdir_path, file_name)
            try:
                with Image.open(image_path) as img:
                    # check if the image has an alpha channel (RGBA)
                    if img.mode == "RGBA":
                        # convert the image to RGB format
                        img = img.convert("RGB")
                    elif img.mode == "P":
                        # convert the image to RGB format
                        img = img.convert("RGB")
                    # create a new file path for the JPEG image
                    jpeg_file_name = os.path.splitext(new_file_name)[0] + ".jpg"
                    jpeg_path = os.path.join(converted_subdir_path, jpeg_file_name)
                    # save the image as JPEG
                    img.save(jpeg_path, "JPEG")
            except Exception as e:
                print(f"Skipping file: {file_name}, Error: {str(e)}")
        else:
            # copy the JPEG image to the converted directory
            image_path = os.path.join(subdir_path, file_name)
            jpeg_path = os.path.join(converted_subdir_path, new_file_name)
            os.system(f"cp \"{image_path}\" \"{jpeg_path}\"")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值