from PIL import Image, ImageDraw, ImageFilter import numpy as np import random import math class SimplePolygon: pow2 = None def __init__(self, file_name, depth_name, tags_name,resolution=(512, 512), init_color=(255, 255, 255)): self.file_name = file_name self.depth_name = depth_name self.tags_name = tags_name self.width = resolution[0] self.height = resolution[1] self.components= [set([(wi, hi) for wi in range(512) for hi in range(512)])] self.img1 = np.full((self.width, self.height, 3), np.random.randint(0, 256, size=(3,), dtype=np.uint8), dtype=np.uint8) self.img2 = np.zeros([self.width, self.height, 3], dtype=np.uint8) self.count = 0 if self.pow2 is None: self.pow2 = [x**2 for x in range(max(resolution[0],resolution[1])+1)] def component_check(self, comp): retv = set() for wi, hi in comp: if 0<=wi r: continue for hi in range(self.height): if xx + w2 * self.pow2[abs(hi-center[1])] <= r: poly.add((wi, hi)) self.components.append(self.rotate_component(poly, random.uniform(-math.pi, math.pi))) return poly def rotate_component(self, comp, rad): retv = set() for wi, hi in comp: new_wi = math.cos(rad)*(wi-self.width/2)-math.sin(rad)*(hi-self.height/2)+self.width/2 new_hi = math.sin(rad)*(wi-self.width/2)+math.cos(rad)*(hi-self.height/2)+self.height/2 new_wi = round(new_wi) new_hi = round(new_hi) for i in range(-1, 2): # -1,0,1 for j in range(-1, 2): retv.add((new_wi+i, new_hi+j)) return retv def save(self): Image.fromarray(self.img1).filter(filter=ImageFilter.GaussianBlur(random.randint(0, 1))).save(self.file_name) Image.fromarray(self.img2).save(self.depth_name) with open(self.tags_name, "w") as file: if self.layer_max is None: file.write("") else: file.write(f"{self.layer_max+1}depth") import uuid import concurrent.futures from tqdm import tqdm import os import threading def process_polygon(): random_uuid = str(uuid.uuid4()) polygon = SimplePolygon(f"conditioning_images/{random_uuid}.png", f"images/{random_uuid}.png", f"conditioning_images/{random_uuid}.txt") for _ in range(random.randint(4, 20)): if random.randint(0,1)==0: polygon.add_ellipse() else: polygon.add_rect() polygon.generate() polygon.save() def main(): if not os.path.exists("conditioning_images"): os.makedirs("conditioning_images") if not os.path.exists("images"): os.makedirs("images") num_processes = 24 total_runs = 21037 with tqdm(total=total_runs, ncols=80) as pbar: with concurrent.futures.ProcessPoolExecutor(max_workers=num_processes) as executor: futures = [executor.submit(process_polygon) for _ in range(total_runs)] for future in concurrent.futures.as_completed(futures): try: result = future.result() pbar.update(1) except Exception as e: print(f"Error occurred: {e}") pbar.update(1) if __name__ == "__main__": main()