import os
from PIL import Image
def rgb_to_hex(r, g, b):
"""将 RGB 值转换为十六进制颜色值"""
return f'{r:02x}{g:02x}{b:02x}'
def find_regions(img, width, height):
"""遍历图像的每一列,根据白色像素的比例来识别区域"""
# 初始化变量
state = 0
regions = []
file_index = 1
start_x = None
end_x = None
# 遍历每一列
for x in range(width):
white_pixel_count = 0
for y in range(height):
# 获取当前像素的颜色
pixel = img.getpixel((x, y))
# 确保像素值是 RGB 或 RGBA 格式
if len(pixel) == 3:
r, g, b = pixel
elif len(pixel) == 4:
r, g, b, _ = pixel # 忽略 Alpha 通道
# 将颜色转换为十六进制格式
hex_color = rgb_to_hex(r, g, b)
# 计数接近白色的像素
if hex_color.upper() >= 'f4fdfc':
white_pixel_count += 1
# 计算白色像素的比例
white_ratio = white_pixel_count / height
# 根据白色像素的比例来识别区域
if state == 0: # 寻找区域的开始
if white_ratio < 0.7:
start_x = x
state = 1 # 开始寻找区域的结束
elif state == 1: # 寻找区域的结束
if white_ratio > 0.95:
end_x = x
state = 2 # 区域找到,准备处理
elif state == 2: # 准备处理区域
if end_x - start_x > 150: # 检查区域是否足够宽
regions.append((start_x, end_x))
file_index += 1 # 为下一个裁剪的文件增加文件索引
state = 0 # 重置状态机
return regions, file_index
def crop_and_save(img, regions, width, height, file_index, image_path):
"""根据检测到的区域裁剪图像并保存为单独的文件"""
base_name, ext = os.path.splitext(image_path) # 解包两个元素
for region in regions:
start_x, end_x = region
cropped_img = img.crop((start_x, 0, end_x, height))
# 确保裁剪后的图像为 RGB 模式
cropped_img = cropped_img.convert('RGB')
new_filename = f"{base_name}_cropped_{file_index}{ext}"
cropped_img.save(new_filename)
file_index += 1
# 删除原始图像
os.remove(image_path)
def find_jpg_files(directory):
"""递归查找指定目录及其子目录中的所有 .jpg 文件"""
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.jpg'):
yield os.path.join(root, file)
def process_jpg_files(directory):
"""处理指定目录及其子目录中的所有 .jpg 文件"""
any_processed = False # 添加标志位,用于检查是否有文件被处理
for image_path in find_jpg_files(directory):
print(f"正在处理 {image_path}...")
img = Image.open(image_path)
print(img) # 打印图像信息
width, height = img.size
regions, file_index = find_regions(img, width, height)
if regions: # 如果找到了符合条件的区域
crop_and_save(img, regions, width, height, file_index, image_path)
any_processed = True
else:
print("未找到符合条件的区域")
if not any_processed:
print("未处理任何文件。")
# 主脚本
if __name__ == '__main__':
directory = '\python\yrkcol2' # 替换为你要处理的实际目录路径
process_jpg_files(directory)import os
from PIL import Image
def rgb_to_hex(r, g, b):
"""将 RGB 值转换为十六进制颜色值"""
return f'{r:02x}{g:02x}{b:02x}'
def find_regions(img, width, height):
"""遍历图像的每一列,根据白色像素的比例来识别区域"""
# 初始化变量
state = 0
regions = []
file_index = 1
start_x = None
end_x = None
# 遍历每一列
for x in range(width):
white_pixel_count = 0
for y in range(height):
# 获取当前像素的颜色
pixel = img.getpixel((x, y))
# 确保像素值是 RGB 或 RGBA 格式
if len(pixel) == 3:
r, g, b = pixel
elif len(pixel) == 4:
r, g, b, _ = pixel # 忽略 Alpha 通道
# 将颜色转换为十六进制格式
hex_color = rgb_to_hex(r, g, b)
# 计数接近白色的像素
if hex_color.upper() >= 'f4fdfc':
white_pixel_count += 1
# 计算白色像素的比例
white_ratio = white_pixel_count / height
# 根据白色像素的比例来识别区域
if state == 0: # 寻找区域的开始
if white_ratio < 0.7:
start_x = x
state = 1 # 开始寻找区域的结束
elif state == 1: # 寻找区域的结束
if white_ratio > 0.95:
end_x = x
state = 2 # 区域找到,准备处理
elif state == 2: # 准备处理区域
if end_x - start_x > 150: # 检查区域是否足够宽
regions.append((start_x, end_x))
file_index += 1 # 为下一个裁剪的文件增加文件索引
state = 0 # 重置状态机
return regions, file_index
def crop_and_save(img, regions, width, height, file_index, image_path):
"""根据检测到的区域裁剪图像并保存为单独的文件"""
base_name, ext = os.path.splitext(image_path) # 解包两个元素
for region in regions:
start_x, end_x = region
cropped_img = img.crop((start_x, 0, end_x, height))
# 确保裁剪后的图像为 RGB 模式
cropped_img = cropped_img.convert('RGB')
new_filename = f"{base_name}_cropped_{file_index}{ext}"
cropped_img.save(new_filename)
file_index += 1
# 删除原始图像
os.remove(image_path)
def find_jpg_files(directory):
"""递归查找指定目录及其子目录中的所有 .jpg 文件"""
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.jpg'):
yield os.path.join(root, file)
def process_jpg_files(directory):
"""处理指定目录及其子目录中的所有 .jpg 文件"""
any_processed = False # 添加标志位,用于检查是否有文件被处理
for image_path in find_jpg_files(directory):
print(f"正在处理 {image_path}...")
img = Image.open(image_path)
print(img) # 打印图像信息
width, height = img.size
regions, file_index = find_regions(img, width, height)
if regions: # 如果找到了符合条件的区域
crop_and_save(img, regions, width, height, file_index, image_path)
any_processed = True
else:
print("未找到符合条件的区域")
if not any_processed:
print("未处理任何文件。")
# 主脚本
if __name__ == '__main__':
directory = '\python\yrkcol2' # 替换为你要处理的实际目录路径
process_jpg_files(directory)
Skip to content
Posted by
By
txiaof
2024 年 7 月 25 日
使用场景:使用百度Paddle…
Posted by
By
txiaof
2024 年 7 月 23 日
gpt-crawler虽然名字…
Posted by
By
txiaof
2024 年 7 月 16 日
MultiOn是一款基于人工智…
Posted by
By
txiaof
2024 年 7 月 15 日
import cv2 impo…
Posted by
By
txiaof
2024 年 7 月 14 日
文件名后缀是.ipynb,就是…
Posted by
By
txiaof
2024 年 7 月 14 日
电脑显卡是p104 8G An…
Posted by
By
txiaof
2024 年 7 月 12 日
Kimi 智能助手是由月之暗面…
Posted by
By
txiaof
2024 年 7 月 12 日
1. 选择AI库:有很多PHP…
Posted by
By
txiaof
2024 年 7 月 12 日
A-1:安装NumpyOpen…
Posted by
By
txiaof
2024 年 7 月 12 日
市面上有很多第三方视频下载工具…
Post You Might Like
Posted by
By
txiaof
2024 年 7 月 25 日
Posted by
By
txiaof
2024 年 7 月 23 日
Posted by
By
txiaof
2024 年 7 月 16 日
Posted by
By
txiaof
2024 年 7 月 15 日
Scroll to Top