Tuesday, 23 March 2021

Get images from folder(s), merging two images and then output variant image(s) into new folder w/ unique file names

So I have two folders, one containing shirts with different colors and the other with logos.

I'm trying to take the images contained in the shirts folder and storing them into an array. I kinda figured how to "store" them using the tkinter module. But kinda confused. I would like to be able to do the same with the logos. Once stored in the array, I want to fetch the images and merge them into every possible image combination for each shirt/logo color OR better yet, specify which colors should have which logos depending on color.

Within the merging process, I want it to resize the shirt image and logo image to a standard size and place the logo image in a specific spot on the shirt image.

Once all that's done, I want to export the new images into a folder with unique names (thinking of a loop that adds shirt_n.jpg, n = 1,2,3 etc.)

I tried looking it all up and I do find pieces of what I want but I can't figure out how to put it together to work.

Heres my code for merging images and exporting:

import os
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from PIL import Imagee

Tk().withdraw()
imFile = askopenfilename()

# Getting images and resize    

shirt = Image.open(imFile)
shirt = shirt.resize((1200, 1800))
logo = Image.open("images/logos/abtb.png")
logo = logo.resize((150, 80))

# Logo Placement 650 x 650
shirt.paste(logo, (650, 650), logo)
shirt.show()

# Save  image
shirt.save('images/output/new_shirt.jpg')
path = "images/final/"
path = os.path.realpath(path)
os.startfile(path)

I wanted to be able to fetch the file path and then have it store all images within that directory into an array. Heres my code:

# paste filepath for images
print("Paste image file path:")
imPath = input()

# storing images in array
imported_imgs = []

# insert code that searches though folder of all .jpg images to store into imported_imgs array here

Then I tried looking up how to save them into a folder with unique names. Couldn't really find anything really straight forward. If you could explain the thought process behind your solution, that would help me understand! Thank you so much!



from Get images from folder(s), merging two images and then output variant image(s) into new folder w/ unique file names

No comments:

Post a Comment