loop through ever image in a folder, crop t hem with the following code: from PIL import…
- User
loop through ever image in a folder, crop t hem with the following code:
from PIL import Image
img = Image.open("../../challenge_images/3d_rollball_animals/ff80a07e00fa7879.png")
w, h = img.size if (w % 200) != 0 and (h % 200) != 0: print("sumn wrong 1") exit()
if (h / 200) != 2: print("sumn wrong 2") exit()
hand = img.crop((0, 200, 130, 400)).show()
animals = []
for i in range(int(w / 200)): animals.append(img.crop((i*200, 0, (i+1)*200, 200)))
after cropping you img.show() the hand plus the animal, then you put a yes/no input asaking if it matches, if i put y (yes) it will create a folder named with the hand images hash that you get with this code:
import img_utils.hash_algo as hash_algo hash_algo.hash_image(Image.open(BytesIO(blah blah blah)))
and put the animal image in there. after that you move onto the next image and crop it, and loop
- Assistant
Here's an example of how you can loop through every image in a folder, crop them using the provided code, and ask for a yes/no input to determine if it matches:
import os from PIL import Image # Input folder path folder_path = "../../challenge_images/3d_rollball_animals" # Output folder path output_folder = "output_folder" # Create output folder if it doesn't exist if not os.path.exists(output_folder): os.makedirs(output_folder) # Loop through every file in the folder for filename in os.listdir(folder_path): # Check if the file is an image if filename.endswith(".png") or filename.endswith(".jpg"): # Open the image file img_path = os.path.join(folder_path, filename) img = Image.open(img_path) # Perform cropping w, h = img.size if (w % 200) != 0 and (h % 200) != 0: print("sumn wrong 1") exit() if (h / 200) != 2: print("sumn wrong 2") exit() hand = img.crop((0, 200, 130, 400)) animals = [] for i in range(int(w / 200)): animals.append(img.crop((i*200, 0, (i+1)*200, 200))) # Display the cropped images hand.show() for animal in animals: animal.show() # Ask for yes/no input match = input("Does the hand match the animal? (y/n): ") # Create the output folder named with the hand images hash if match.lower() == "y": hash_value = hash_algo.hash_image(hand) output_path = os.path.join(output_folder, hash_value) # Create the output folder if it doesn't exist if not os.path.exists(output_path): os.makedirs(output_path) # Save the animal image in the output folder for i, animal in enumerate(animals): animal_path = os.path.join(output_path, f"animal_{i}.png") animal.save(animal_path) # Close the image file img.close() - User
no, you present the animals one by one with the same hand that you cropped from the same image. once I mark a matching animal you only put that one animal picture (the filename being the animals hash) into the folder which is named after than hand images hash.
- Assistant