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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| from PIL import Image, ImageDraw import os
def create_rounded_image(image_path, output_path, corner_radius=50, final_size=(64, 64)): """Create a rounded corner image, resize it to 64x64, and save it.""" try: img = Image.open(image_path) img = img.resize(final_size, Image.Resampling.LANCZOS).convert("RGBA") mask = Image.new('L', img.size, 0) draw = ImageDraw.Draw(mask) draw.rounded_rectangle([(0, 0), img.size], corner_radius, fill=255) img.putalpha(mask) img.save(output_path) print(f"Processed and saved: {os.path.basename(output_path)}") except Exception as e: print(f"Error processing {os.path.basename(image_path)}: {e}")
def create_collage(image_paths, output_path, images_per_row=7, spacing=10): """Create a collage of images with transparent background.""" try: images = [Image.open(path).convert("RGBA") for path in image_paths] image_width, image_height = images[0].size total_width = image_width * images_per_row + spacing * (images_per_row - 1) total_rows = (len(images) + images_per_row - 1) // images_per_row total_height = image_height * total_rows + spacing * (total_rows - 1) collage = Image.new('RGBA', (total_width, total_height), (0, 0, 0, 0)) for index, image in enumerate(images): row_num = index // images_per_row col_num = index % images_per_row x = col_num * (image_width + spacing) y = row_num * (image_height + spacing) collage.paste(image, (x, y), image) collage.save(output_path) print("Collage created successfully.") except Exception as e: print(f"Error creating collage: {e}")
image_folder = "/Users/achao/Downloads" output_folder = image_folder rounded_images = []
for i in range(1, 15): input_path = os.path.join(image_folder, f"image_{i}.jpg") output_path = os.path.join(output_folder, f"rounded_{i}.png") create_rounded_image(input_path, output_path, corner_radius=50, final_size=(64, 64)) rounded_images.append(output_path)
collage_output_path = os.path.join(output_folder, "collage.png") create_collage(rounded_images, collage_output_path)
|