数据处理常用脚本

转换图像格式为JPG

以下脚本将指定目录中的所有支持格式的图片文件(如PNG、BMP等)转换为JPG格式,并删除原始文件:

点击查看代码
import os
import imghdr
from PIL import Image


def convert_images_to_jpg(directory):
    # 使用os.scandir()可以提高目录遍历效率
    for entry in os.scandir(directory):
        if entry.is_file():
            file_path = entry.path

            try:
                # 使用imghdr检查文件类型,避免无效文件
                if imghdr.what(file_path):
                    with Image.open(file_path) as img:
                        # 如果文件已经是JPEG格式,跳过转换
                        if img.format.lower() != 'jpeg':
                            # 生成新的文件名,替换扩展名为.jpg
                            new_filename = os.path.splitext(entry.name)[0] + '.jpg'
                            new_file_path = os.path.join(directory, new_filename)

                            # 转换并保存为jpg格式
                            img.convert('RGB').save(new_file_path, 'JPEG')
                            print(f"Converted {entry.name} to {new_filename}")

                            # 删除原始文件
                            if file_path != new_file_path:
                                os.remove(file_path)
                                print(f"Deleted original file {entry.name}")
                        else:
                            print(f"Skipping already JPEG file: {entry.name}")
                else:
                    print(f"Skipping non-image file: {entry.name}")
            except Exception as e:
                print(f"Error converting {entry.name}: {e}")


if __name__ == '__main__':
    # 使用示例
    input_dir = r'path/to/your/directory'  # 替换为你的目录路径
    convert_images_to_jpg(input_dir)

批量重命名文件

以下脚本通过传入新的文件前缀和目标目录,自动为目录下的所有文件按指定格式重命名:

点击查看代码
import os


def rename_files_in_directory(directory, new_filename):
    # 获取目录下所有文件
    files = os.listdir(directory)

    # 如果目录为空,直接退出
    if not files:
        print("No files to rename.")
        return

    # 遍历文件并重命名
    for index, file in enumerate(files, start=1):
        # 构造新文件名
        file_ext = os.path.splitext(file)[1]  # 获取文件扩展名
        new_name = f'{new_filename}_{index:04d}{file_ext}'

        # 获取旧文件路径和新文件路径
        old_path = os.path.join(directory, file)
        new_path = os.path.join(directory, new_name)

        try:
            # 重命名文件
            os.rename(old_path, new_path)
            print(f'Renamed {file} to {new_name}')
        except Exception as e:
            print(f"Error renaming {file}: {e}")

    print("文件重命名完成")


if __name__ == '__main__':
    # 设置目标目录路径
    input_dir = r'path/to/your/directory'  # 替换为你的目录路径
    new_filename = "new"
    rename_files_in_directory(input_dir, new_filename)

提取图像中的主体部分

以下脚本通过提取图像中的前若干个最大轮廓,并将对应区域裁剪出来保存为带透明背景的 PNG 图像:

点击查看代码
import cv2
import numpy as np
import os

def process_image(image_path, num_contours=2, output_dir="output"):
    try:
        # 读取图像
        image_bgr = cv2.imread(image_path)
        if image_bgr is None:
            raise ValueError(f"无法读取图像:{image_path}")

        # 转换为灰度图
        gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)

        # 应用阈值操作来二值化图像
        _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        # 查找图像中的轮廓
        contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        # 按照轮廓面积排序
        contours = sorted(contours, key=cv2.contourArea, reverse=True)

        # 确保输出目录存在
        os.makedirs(output_dir, exist_ok=True)

        for i in range(min(num_contours, len(contours))):
            contour = contours[i]

            # 创建一个与原图相同大小的黑色掩码
            mask = np.zeros_like(image_bgr, dtype=np.uint8)

            # 绘制当前轮廓的白色区域
            cv2.drawContours(mask, [contour], -1, (255, 255, 255), thickness=cv2.FILLED)

            # 计算轮廓的外接矩形
            x, y, w, h = cv2.boundingRect(contour)

            # 从原图中裁剪出外接矩形区域
            cropped_image = image_bgr[y:y + h, x:x + w]

            # 将裁剪图像转换为 BGRA(增加 Alpha 通道)
            cropped_image_rgba = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2BGRA)

            # 创建掩码,设置 Alpha 通道为 0(透明),并直接应用掩码
            alpha_channel = mask[y:y + h, x:x + w][:, :, 0]  # 获取掩码的 Alpha 通道
            cropped_image_rgba[:, :, 3] = alpha_channel  # 更新 Alpha 通道

            # 保存裁剪的图像
            output_filename = os.path.join(output_dir, f"contour_{i + 1}.png")
            cv2.imwrite(output_filename, cropped_image_rgba)

            print(f"保存轮廓 {i + 1} 的图像为 {output_filename}")

    except Exception as e:
        print(f"处理图像时发生错误: {e}")

if __name__ == '__main__':
    # 使用示例
    image_path = r'path/to/your/image'  # 替换为你的图像路径
    save_dir = r'output'  # 指定保存目录
    num_contours = 2
    process_image(image_path, num_contours, save_dir)

压缩图片质量

此脚本用于根据指定的压缩质量将指定图像文件压缩,并将压缩后的图片保存为新的JPG文件:

点击查看代码
import os
from PIL import Image


def compress_image(image_path, quality=70):
    # 检查文件是否存在
    if not os.path.exists(image_path):
        print("File does not exist.")
        return

    # 获取文件扩展名
    file_ext = os.path.splitext(image_path)[1].lower()

    # 只处理图片文件
    if file_ext not in ['.jpg', '.jpeg', '.png']:
        print("Not a valid image file.")
        return

    try:
        # 打开图像文件
        with Image.open(image_path) as img:
            # 如果是PNG文件,将其转换为RGB模式后保存为JPG格式
            if file_ext == '.png':
                img = img.convert('RGB')
                # 设置新文件路径,转换为JPG格式
                directory = os.path.dirname(image_path)
                file_name = os.path.basename(image_path)
                new_file_name = f"compressed_{os.path.splitext(file_name)[0]}.jpg"
                new_path = os.path.join(directory, new_file_name)
            else:
                # 如果是JPG或JPEG文件,直接使用原始路径
                directory = os.path.dirname(image_path)
                file_name = os.path.basename(image_path)
                new_path = os.path.join(directory, f"compressed_{file_name}")

            # 保存压缩后的图片,设置质量
            img.save(new_path, 'JPEG', quality=quality)
            print(f'Compressed image saved as {new_path}')

    except Exception as e:
        print(f"Error compressing {image_path}: {e}")

    print("图片压缩完成")


if __name__ == '__main__':
    # 设置目标图像路径
    image_path = r'path/to/your/image.jpg'  # 替换为你的图片路径
    compress_quality = 70  # 设置压缩质量,范围是1-100,默认70
    compress_image(image_path, compress_quality)
posted @ 2025-01-14 00:48  gokamisama  阅读(27)  评论(0)    收藏  举报