i have a bunch of dark object on white background. write a python script that cuts off…
- User
i have a bunch of dark object on white background. write a python script that cuts off each one of them and saves as different images
- Assistant
Sure! To accomplish this task, you can use OpenCV package in Python which provides a wide range of image processing functions. Here's a Python script that can help you achieve your goal:
import cv2 import os def crop_objects(image_path): # Read the image image = cv2.imread(image_path) # Convert image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Applying threshold to get binary image _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # Find contours contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Create a directory to save cropped images output_dir = 'cropped_images' os.makedirs(output_dir, exist_ok=True) # Crop and save each object as separate images for i, contour in enumerate(contours): # Get bounding rectangle coordinates x, y, w, h = cv2.boundingRect(contour) # Crop the object from the original image cropped_image = image[y:y+h, x:x+w] # Save the cropped image output_path = os.path.join(output_dir, f"object_{i}.png") cv2.imwrite(output_path, cropped_image) print(f"Object {i+1}: Saved as {output_path}") # Replace 'image.jpg' with the path to your input image crop_objects('image.jpg') - User
make the boundaries a little bigger so there a little white space around images
- Assistant
Certainly! To add a little white space around the cropped images, you can expand the bounding rectangle by a certain margin. Here's an updated version of the script:
import cv2 import os def crop_objects(image_path, margin=10): # Read the image image = cv2.imread(image_path) # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Applying threshold to get binary image _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # Find contours contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Create a directory to save cropped images output_dir = 'cropped_images' os.makedirs(output_dir, exist_ok=True) # Crop and save each object as separate images for i, contour in enumerate(contours): # Get bounding rectangle coordinates x, y, w, h = cv2.boundingRect(contour) # Expand the bounding rectangle by the specified margin x -= margin y -= margin w += 2*margin h += 2*margin # Crop the object from the original image cropped_image = image[max(0, y):y+h, max(0, x):x+w] # Save the cropped image output_path = os.path.join(output_dir, f"object_{i}.png") cv2.imwrite(output_path, cropped_image) print(f"Object {i+1}: Saved as {output_path}") # Replace 'image.jpg' with the path to your input image crop_objects('image.jpg', margin=10)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.