0

I am adjusting a script.

I have 4427 images in a specified folder, named

  • (1).png
  • (2).png
  • (3).png
  • etc.

Along with those, I have another 14 images, named:

  • 1.png
  • 2.png
  • 3.png
  • etc.

Basically the script should:

  1. Take a specific image I tell it to open of the 4427
  2. Then, open one of the 14 images at random
  3. Merge the two and save it to a specified directory.

Code

import os
import random
from PIL import Image

path = r"C:\Users\17379\Desktop\images\Low effort glasses"
random_filename = random.choice([
    x for x in os.listdir(path)
    if os.path.isfile(os.path.join(path, x))
])
print(random_filename)

x = Image.open(r"(1).png").convert("RGBA")
y = Image.open(random_filename)
z = Image.alpha_composite(x, y)

z.save(r"C:\Users\17379\Desktop\images\Resized punks\Resized punks\punk1.png")

How to do this to all 4427 images and then save each file to the specified directory?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Walter
  • 3
  • 3
  • What is the problem ? You are not asking a question, nor showing an error. – Lenormju Jan 31 '22 at 10:43
  • I wonder how to edit this so it outputs what I want it to. It does this to one image and then saves that image to a directory. I want it to do all 4,427 images – Walter Jan 31 '22 at 19:51

1 Answers1

0

The pseudo-code for your task is:

for file_name in source_image_list:
    # 1. Take a specific image I tell it to open of the 4427
    source_image = open_source(file_name)
    # 2. Then, open one of the 14 images at random
    random_image = open_random_of(random_image_list)
    # 3. Merge the two and save it to a specified directory.
    target_image = merge(source_image, random_image)
    save(target_image, file_name, directory)

Translate this to Python:

import os
import glob
import random
from PIL import Image


def open_random(random_image_list):
    random_filename = random.choice(in random_image_list)
    print(f"Random: {random_filename}")
    return Image.open(random_filename)


def open_source(file_name):
    return Image.open(file_name).convert("RGBA")


def merge(source, random):
    return Image.alpha_composite(source, random)


def save(image, original_name, directory):
    target = os.path.join(directory, os.path.basename(original_name))
    print(f"Saving: {target}")
    image.save(target)


if __name__ == '__main__':
    source_path = r"C:\Users\17379\Desktop\images"
    random_path = r"C:\Users\17379\Desktop\images\Low effort glasses"
    directory =  r"C:\Users\17379\Desktop\images\Resized punks\Resized punks"
    
    random_image_list = os.listdir(random_path)  # can also use glob here to filter for (specific) images only
    source_image_list = glob.glob(f"{source_path}/\([0-9]+\).png")

    for file_name in source_image_list:
        print(f"Source: {file_name}")
        # 1. Take a specific image I tell it to open of the 4427
        source_image = open_source(file_name)
        # 2. Then, open one of the 14 images at random
        random_image = open_random_of(random_image_list)
        # 3. Merge the two and save it to a specified directory.
        target_image = merge(source_image, random_image)
        save(target_image, file_name, directory)

Note: I had to replace os.listdir(source_path) for glob.glob because it accepts a regular-expression to filter for specific files only. See also Python3 create list of image in a folder

hc_dev
  • 8,389
  • 1
  • 26
  • 38