所以我有两个文件夹,一个包含不同颜色的衬衫,另一个包含徽标
我正在尝试拍摄shirts文件夹中包含的图像,并将它们存储到一个数组中。我想知道如何使用tkinter模块“存储”它们。但是有点困惑。我想能够做同样的标志。一旦存储在数组中,我想获取图像并将它们合并到每个衬衫/徽标颜色的每个可能的图像组合中或更好的是,根据颜色指定哪些颜色应具有哪些徽标
在合并过程中,我希望它将衬衫图像和徽标图像调整为标准大小,并将徽标图像放置在衬衫图像上的特定位置
完成所有这些之后,我想将新图像导出到一个具有唯一名称的文件夹中(考虑添加shirt_n.jpg、n=1,2,3等的循环)
我试着把所有的东西都找出来,我确实找到了我想要的东西,但我不知道如何把它们组合起来工作
编辑:我有点想知道如何得到我想要的结果,但正如你在下面看到的,下面的代码非常简单,没有经过优化。我不知道如何优化,将其压缩成更少的代码行
# python imports
import os
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from PIL import Image
Tk().withdraw()
imFile = askopenfilename()
print("What shade is the shirt? Dark or Light?")
respShade = input()
print("What's the logo location? Right or Left?:")
respLocation = input()
print("Color abriv. name?")
respName = input()
shirt = Image.open(imFile)
shirt_01 = shirt.resize((1200, 1800))
shirt_02 = shirt.resize((1200, 1800))
shirt_03 = shirt.resize((1200, 1800))
# For Dark Apparel
if "ark" in respShade:
logo_hfwl = Image.open("images/logos/randazzo/hfox-wht.png")
logo_hfwl = logo_hfwl.resize((150, 80))
logo_hrtw = Image.open("images/logos/randazzo/htld-wht.png")
logo_hrtw = logo_hrtw.resize((150, 80))
logo3_rzwl = Image.open("images/logos/randazzo/rand-wht.png")
logo3_rzwl = logo3_rzwl.resize((150, 80))
if "lef" in respLocation:
shirt_01.paste(logo_hfwl, (660, 660), logo_hfwl)
shirt_02.paste(logo_hrtw, (660, 660), logo_hrtw)
shirt_03.paste(logo3_rzwl, (660, 660), logo3_rzwl)
elif "rig" in respLocation:
shirt_01.paste(logo_hfwl, (410, 660), logo_hfwl)
shirt_02.paste(logo_hrtw, (410, 660), logo_hrtw)
shirt_03.paste(logo3_rzwl, (410, 660), logo3_rzwl)
shirt_01.save('images/final/' + respName + '-hfwl.jpg')
shirt_02.save('images/final/' + respName + '-hrtw.jpg')
shirt_03.save('images/final/' + respName + '-rwl.jpg')
# For Light Apparel
if "ght" in respShade:
logo_hfwl = Image.open("images/logos/randazzo/hfox.png")
logo_hfwl = logo_hfwl.resize((150, 80))
logo_hrtw = Image.open("images/logos/randazzo/htld.png")
logo_hrtw = logo_hrtw.resize((150, 80))
logo3_rzwl = Image.open("images/logos/randazzo/rand.png")
logo3_rzwl = logo3_rzwl.resize((150, 80))
if "lef" in respLocation:
shirt_01.paste(logo_hfwl, (660, 660), logo_hfwl)
shirt_02.paste(logo_hrtw, (660, 660), logo_hrtw)
shirt_03.paste(logo3_rzwl, (660, 660), logo3_rzwl)
elif "rig" in respLocation:
shirt_01.paste(logo_hfwl, (410, 660), logo_hfwl)
shirt_02.paste(logo_hrtw, (410, 660), logo_hrtw)
shirt_03.paste(logo3_rzwl, (410, 660), logo3_rzwl)
shirt_01.save('images/final/' + respName + '-hfwl.jpg')
shirt_02.save('images/final/' + respName + '-hrtw.jpg')
shirt_03.save('images/final/' + respName + '-rwl.jpg')
# shirt.save('images/final/change_me.jpg')
path = "images/final/"
path = os.path.realpath(path)
os.startfile(path)
此外,我还尝试查找如何将它们保存到具有唯一名称的文件夹中。无法直接找到任何东西。如果你能解释你的解决方案背后的思维过程,那将有助于我理解!非常感谢你
可以使用os.listdir()函数搜索目录中的文件。尝试将此添加到代码中
我相信使用askdirectory函数比粘贴它更简单
首先,我建议将图像组织如下:
然后我们需要定义一个函数来读取目录中的所有图像,而无需获取用户输入。这就是为什么我认为我们根本不需要tkinter
现在最重要的功能是将“.png”格式的徽标合并到“.jpeg”格式的衬衫上。此功能包括三个部分:
这是该功能的主体:
这是我在“final/left”文件夹中得到的结果示例。我有两件衬衫和三个标志,所以结果是六张照片
最后,这是全部代码
相关问题 更多 >
编程相关推荐