[Python] cropping the image by removing the white spaces

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to identify the empty spaces in the image and if there is no image, then I would like to crop it by eliminating the spaces. Just like in the images below.

    [​IMG] --> [​IMG]

    I would be grateful for your help. Thanks in advance!

    I was using the following code, but was not really working.

    import cv2


    import numpy as np

    def crop_empty_spaces_refined(image_path, threshold_percentage=0.01): image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)

    if image is None:
    print(f"Error: Could not read image at {image_path}")
    return None

    if image.shape[2] == 4: # RGBA image
    gray = image[:, :, 3] # Use alpha channel
    else:
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    _, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)

    kernel = np.ones((3, 3), np.uint8)
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)

    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if contours:
    image_area = image.shape[0] * image.shape[1]
    min_contour_area = image_area * threshold_percentage

    valid_contours = [cnt for cnt in contours if cv2.contourArea(cnt) >= min_contour_area]

    if valid_contours:
    # ***Corrected Bounding Box Calculation***
    x_coords = []
    y_coords = []

    for cnt in valid_contours:
    x, y, w, h = cv2.boundingRect(cnt)
    x_coords.extend([x, x + w]) # Add both start and end x
    y_coords.extend([y, y + h]) # Add both start and end y

    x_min = min(x_coords)
    y_min = min(y_coords)
    x_max = max(x_coords)
    y_max = max(y_coords)

    cropped_image = image[y_min:y_max, x_min:x_max]

    return cropped_image
    else:
    print("No valid contours found after filtering. Returning original image.")
    return image
    else:
    return image


    image_path = '/mnt/data/Untitled.png' # file path cropped_image = crop_empty_spaces_refined(image_path, threshold_percentage=0.0001)

    if cropped_image is not None: cv2.imwrite('/mnt/data/cropped_output.png', cropped_image) print("Image Cropped and saved") else: print("Could not crop image")

    Continue reading...

Compartilhe esta Página