Thank you massively to Paul M who posted the following code in response to my first query on how to compile a "stack" of randomly selected images into a unique image:
from pathlib import Path
from random import choice
layers = [list(Path(directory).glob("*.png")) for directory in ("bigcircle/", "mediumcircle/")]
selected_paths = [choice(paths) for paths in layers]
img = Image.new("RGB", (4961, 4961), color=(0, 220, 15))
for path in selected_paths:
layer = Image.open(str(path), "r")
img.paste(layer, (0, 0), layer)
I have the code sat in a for _ in itertools.repeat(None, num):
where num
defines the number of different images being generated. I end the loop with the following to save each image with a unique (incremental) file name:
i = 0
while os.path.exists("Finished Pieces/image %s.png" % i):
i += 1
img.save("Finished Pieces/image %s.png" % i,)
So far, so good. The challenge I'm struggling with now is how to append to a data.csv
file with the details of each image created.
For example, in loop 1 bigcircle1.png
is selected from bigcircle/
folder and mediumcircle6.png
from mediumcircle/
, loop 2 uses bigcircle3.png
and mediumcircle2.png
, and so on. At the end of this loop the data.csv
file would read:
Filename,bigcircle,mediumcircle
image 0,bigcircle1.png,mediumcircle6.png
image 1,bigcircle3.png,mediumcircle2.png
I've tried the following, which I know wouldn't give the desired result, but which I thought might be a good start for me to run and tweak until right, but it doesn't generate any output (and I am importing numpy
as np
):
np.savetxt('data.csv', [p for p in zip(img, layer)], delimiter=',', fmt='%s')
If it's not too much of an ask, ideally the first iteration of the loop would create data.csv
and store the first record, with the second iteration onwards appending this file.
Aucun commentaire:
Enregistrer un commentaire