我制作了一个脚本,在Excel表格中循环,如下所示:
然后在.jpg
图像文件夹中循环
当匹配时,它会在图像上写入一些文本,并将其另存为新文本
如何完成此循环,以便在图像名称匹配时使用第2、3、4和5列中的数据替换变量txt1
、txt2
、txt3
、fontsize
等
我是个新手,所以请随意指出我的代码有多难看
import os
from PIL import Image,ImageFont,ImageDraw, features
import pandas as pd
path='./'
txt1= "name..."
W1 = 1200
H1 = 200
fontSize1 = 142
txt2= "country..."
W2 = 1200
H2 = 400
fontSize2 = 132
txt3= "code..."
W3 = 1200
H3 = 600
fontSize3 = 124
def setText(name,file,txt,fontSize,w,h):
arial = ImageFont.truetype(r'./font.ttf', fontSize)
draw = ImageDraw.Draw(file)
draw.text((w, h), txt, font=arial, fill='#ff0000',
direction="rtl",align="right",anchor="rm",features='rtla')
file.save(f'done {name}')
df = pd.read_excel (r'./data.xlsx')
files = []
for (dirpath, dirnames, filenames) in os.walk(path):
files.extend(filenames)
items= []
for index, row in df.iterrows():
items.append(row["item"])
for i in items:
if i in files:
imageName = i
imgfile = Image.open(f"./{imageName}")
setText(imageName,imgfile,txt1,fontSize1,W1,H1)
setText(imageName,imgfile,txt2,fontSize2,W2,H2)
setText(imageName,imgfile,txt3,fontSize3,W3,H3)
每当您有一系列重复的变量声明时,如下所示:
这是一个很好的线索,你最好有一份清单:
现在可以在循环中设置这些属性,以便:
变成这样:
在我自己的代码中,我可能会使用
NamedTuple
而不是普通的tuple
,但这是另一个主题。:)(编辑)如果要将CSV数据交换为
txt
值,我认为您要做的是将键名粘贴在其中(不带点),如下所示:然后在
iterrows
循环中执行此操作(您已经拥有所有数据),而不是构建items
列表:相关问题 更多 >
编程相关推荐