Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14301

Why does the output image not resemble polygon art despite in the provided script?

$
0
0

Certainly! Here's the text written in the first person:


I've been experimenting with creating polygon art using a custom script in Python. The script I'm using incorporates algorithms to generate polygons and fill them with colors from an input image. However, when I run the script with my image as input, the resulting output doesn't quite resemble polygon art as I expected.

I've ensured that the script iteratively generates polygons, fills them with colors from the underlying image, and terminates appropriately based on certain conditions such as maximum iterations or coverage threshold. Despite my efforts, the output doesn't exhibit the characteristic polygon-like appearance.

Could someone help me understand why the output image fails to represent polygon art despite following the provided script? Are there any specific adjustments or considerations I might be missing to achieve the desired polygon art effect?

I appreciate any insights or suggestions to enhance the functionality of my script for generating polygon art effectively.

import cv2import numpy as npdef polygon_fill(image, points):"""Fills a polygon with colors from the image below."""    for i in range(image.shape[0]):        for j in range(image.shape[1]):            dist = cv2.pointPolygonTest(np.array(points), (j, i), True)            if dist >= 0:  # Point is inside the polygon                color = image[max(i - 1, 0), j]  # Get color from pixel below (avoid out-of-bounds)                image[i, j] = color  # Set polygon pixel to the retrieved color    return imagedef generate_polygon_art(image_path, max_iterations=10000):"""Generates polygon art from an image, with stopping condition."""    image = cv2.imread(image_path)    output_image = image.copy()  # Create a copy for polygon fill    # Define a starting point for the first polygon    start_point = (0, 0)  # Replace with desired starting coordinates    # Iteratively generate and fill polygons    for _ in range(max_iterations):        # Create a new polygon based on the starting point        polygon_points = create_new_polygon(start_point)        # Check if polygon goes outside image boundaries        if any(point[0] < 0 or point[0] >= image.shape[1] or point[1] < 0 or point[1] >= image.shape[0] for point in polygon_points):            continue  # Skip polygons outside image        # Fill the polygon with colors from the underlying image        output_image = polygon_fill(output_image, polygon_points)        # Check if the image is fully covered or maximum iterations reached        if is_image_covered(output_image) or _ == max_iterations - 1:            break        # Update starting point for the next polygon based on previous polygon's center        start_point = get_next_starting_point(polygon_points)    # Display or save the resulting image    cv2.imshow("Polygon Art", output_image)    cv2.waitKey(0)    cv2.destroyAllWindows()def create_new_polygon(start_point, max_width=30, max_height=20):"""Generates a random polygon with size and angle constraints."""    import random    # Randomly choose width and height within specified limits    width = random.randint(10, max_width)    height = random.randint(10, max_height)    # Randomly choose an angle for the polygon    angle = random.uniform(0, 360)    # Define a base polygon centered at the starting point    base_points = [        (-width // 2, -height // 2),        (width // 2, -height // 2),        (width // 2, height // 2),        (-width // 2, height // 2),    ]    # Rotate the base polygon based on the chosen angle    rotation_matrix = cv2.getRotationMatrix2D((0, 0), angle, 1.0)    points = np.array(base_points).reshape((-1, 1, 2))    points = cv2.transform(points, rotation_matrix).reshape((-1, 2))    # Translate the rotated polygon to the starting point    points += np.array([start_point[0], start_point[1]])    return points.tolist()def is_image_covered(image, threshold=0.9):"""Checks if a certain percentage of pixels in the image have been colored."""    colored_pixels = np.count_nonzero(image != [0, 0, 0])    total_pixels = image.size    coverage = colored_pixels / total_pixels    return coverage >= thresholddef get_next_starting_point(polygon_points):"""Chooses a random point within the last polygon's bounding rectangle as the next starting point."""    min_x, min_y, max_x, max_y = cv2.boundingRect(np.array(polygon_points))    width = max_x - min_x    height = max_y - min_y    return (random.randint(min_x, max_x), random.randint(min_y, max_y))# Replace "path/to/your/image.jpg" with the actual path to your imagegenerate_polygon_art("/home/red-x/Pictures/me.jpg")

Viewing all articles
Browse latest Browse all 14301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>