我有一个文件夹,其中包含很多图像,水平和垂直图像的混合。我试图过滤掉图像,它显示的图像看起来是水平的,并且图像的顶部和底部有很多空白,这里是示例
虽然我还有其他类似这样的图片
所有图像的尺寸相同,均为 400x400,我试图根据图像顶部和底部的空白量来过滤图像。
这是我尝试过的
from PIL import Image
import os
import shutil
def has_white_borders(image_path, threshold_top_bottom=0.2):
# Open the image
img = Image.open(image_path)
# Get image size
width, height = img.size
# Define the number of rows to consider for top and bottom
rows_to_check = int(height * 0.05) # Consider top and bottom 5% of the image
# Get the top and bottom rows
top_rows = img.crop((0, 0, width, rows_to_check))
bottom_rows = img.crop((0, height - rows_to_check, width, height))
# Calculate the ratio of white pixels in the top and bottom rows
top_white_pixels = sum(top_rows.convert("L").point(lambda p: p > 200 and 1 or 0).getdata())
bottom_white_pixels = sum(bottom_rows.convert("L").point(lambda p: p > 200 and 1 or 0).getdata())
top_ratio = top_white_pixels / (width * rows_to_check)
bottom_ratio = bottom_white_pixels / (width * rows_to_check)
# Check if the ratio of white pixels on top and bottom exceeds the threshold
return top_ratio > threshold_top_bottom and bottom_ratio > threshold_top_bottom
def find_images_with_white_borders(input_folder, output_folder):
# Get a list of all files in the input folder
files = os.listdir(input_folder)
# Filter out non-image files
image_files = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff'))]
# Check each image for white borders
images_with_white_borders = [img for img in image_files if has_white_borders(os.path.join(input_folder, img))]
# Create the output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Copy images with white borders to the output folder
for img_name in images_with_white_borders:
source_path = os.path.join(input_folder, img_name)
destination_path = os.path.join(output_folder, img_name)
shutil.copy2(source_path, destination_path)
if __name__ == "__main__":
# Specify the input folder containing your images
input_image_folder = r'folder/path'
# Specify the output folder for filtered images
output_image_folder = r'folder/path'
# Find images with more white borders on top and bottom and copy them to the output folder
find_images_with_white_borders(input_image_folder, output_image_folder)
print(f"Images with more white borders on top and bottom copied to: {output_image_folder}")
当我运行代码时,它仍然向我显示顶部和底部几乎没有白色边框的图像,或者向我显示侧面带有白色边框的图像..我想找到像第一张图像一样水平的图像。
你可以检查第一行和最后一行是否都是白色的:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def is_hori(img):
return img[0].min() == 255 and img[-1].min() == 255
fns = ['img1.jpeg', 'img2.jpeg', 'img3.png']
imgs = [np.asarray(Image.open(f'../{fn}')) for fn in fns]
plt.imshow(np.hstack(imgs))
plt.title((' ' * 32).join([str(is_hori(img)) for img in imgs]))
plt.axis('off')
plt.tight_layout()