How To Append The Filenames Of Randomly Selected Images Used In Creating A Single Image To A Csv File?
Solution 1:
Me again ;)
I think it would make sense to split up the functionality of the program into separate functions. I would start maybe with a function called something like discover_image_paths
, which discovers (via glob
) all image paths. It might make sense to store the paths according to what kind of circle they represent - I'm envisioning a dictionary with "big"
and "medium"
keys, and lists of paths as associated values:
def discover_image_paths():
from pathlib importPathkeys= ("bigcircle", "mediumcircle")
return dict(zip(keys, (list(Path(directory).glob("*.png")) for directory in(key+"/"for key in keys))))
def main():
global pathspaths= discover_image_paths()
return0if__name__== "__main__":
import sys
sys.exit(main())
In the terminal:
>>> paths["bigcircle"][WindowsPath('bigcircle/big1.png'), WindowsPath('bigcircle/big2.png'), WindowsPath('bigcircle/big3.png')]
>>> paths["mediumcircle"][WindowsPath('mediumcircle/med1.png'), WindowsPath('mediumcircle/med2.png'), WindowsPath('mediumcircle/med3.png')]
>>>
As you can see, to test the script, I created some dummy image files - three for each category.
Extending this by adding a function to generate an output image (given an iterable of paths to combine and an output file name) and a main loop to generate num_images
number of images (sorry, I'm not familiar with numpy):
defgenerate_image(paths, color, output_filename):
from PIL import Image
dimensions = (4961, 4961)
image = Image.new("RGB", dimensions, color=color)
for path in paths:
layer = Image.open(path, "r")
image.paste(layer, (0, 0), layer)
image.save(output_filename)
defdiscover_image_paths(keys):
from pathlib import Path
returndict(zip(keys, (list(Path(directory).glob("*.png")) for directory in (key+"/"for key in keys))))
defmain():
from random import choice, choices
from csv import DictWriter
field_names = ["filename", "color"]
keys = ["bigcircle", "mediumcircle"]
paths = discover_image_paths(keys)
num_images = 5withopen("data.csv", "w", newline="") as file:
writer = DictWriter(file, fieldnames=field_names+keys)
writer.writeheader()
for image_no inrange(1, num_images + 1):
selected_paths = {key: choice(category_paths) for key, category_paths in paths.items()}
file_name = "output_{}.png".format(image_no)
color = tuple(choices(range(0, 256), k=3))
generate_image(map(str, selected_paths.values()), color, file_name)
row = {**dict(zip(field_names, [file_name, color])), **{key: path.name for key, path in selected_paths.items()}}
writer.writerow(row)
return0if __name__ == "__main__":
import sys
sys.exit(main())
Example output in the CSV:
filename,color,bigcircle,mediumcircle
output_1.png,"(49, 100, 190)",big3.png,med1.png
output_2.png,"(228, 37, 227)",big2.png,med3.png
output_3.png,"(251, 14, 193)",big1.png,med1.png
output_4.png,"(35, 12, 196)",big1.png,med3.png
output_5.png,"(62, 192, 170)",big2.png,med2.png
Post a Comment for "How To Append The Filenames Of Randomly Selected Images Used In Creating A Single Image To A Csv File?"