app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import cv2 import numpy as np import os # Load image image = cv2.imread("profile.jpg") height, width = image.shape[:2] # Output folder output_folder = "output_shapes" os.makedirs(output_folder, exist_ok=True) # Circle Mask circle_mask = np.zeros((height, width), dtype=np.uint8) cv2.circle(circle_mask, (width // 2, height // 2), min(width, height) // 3, 255, -1) circle_result = cv2.bitwise_and(image, image, mask=circle_mask) cv2.imwrite(os.path.join(output_folder, "circle_crop.png"), circle_result) # Triangle Mask triangle_mask = np.zeros((height, width), dtype=np.uint8) triangle_points = np.array([ [width // 2, height // 4], [width // 4, 3 * height // 4], [3 * width // 4, 3 * height // 4] ]) cv2.fillPoly(triangle_mask, [triangle_points], 255) triangle_result = cv2.bitwise_and(image, image, mask=triangle_mask) cv2.imwrite(os.path.join(output_folder, "triangle_crop.png"), triangle_result) # Rectangle Mask rect_mask = np.zeros((height, width), dtype=np.uint8) cv2.rectangle(rect_mask, (width // 4, height // 4), (3 * width // 4, 3 * height // 4), 255, -1) rect_result = cv2.bitwise_and(image, image, mask=rect_mask) cv2.imwrite(os.path.join(output_folder, "rectangle_crop.png"), rect_result) # Ellipse Mask ellipse_mask = np.zeros((height, width), dtype=np.uint8) cv2.ellipse(ellipse_mask, (width // 2, height // 2), (width // 4, height // 3), 0, 0, 360, 255, -1) ellipse_result = cv2.bitwise_and(image, image, mask=ellipse_mask) cv2.imwrite(os.path.join(output_folder, "ellipse_crop.png"), ellipse_result) print("All shapes cropped and saved to:", output_folder) |