如何在一个循环中同时创建多个循环?

2024-10-02 20:39:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个脚本,在Excel表格中循环,如下所示:

^{tb1}$

然后在.jpg图像文件夹中循环

当匹配时,它会在图像上写入一些文本,并将其另存为新文本

如何完成此循环,以便在图像名称匹配时使用第2、3、4和5列中的数据替换变量txt1txt2txt3fontsize

我是个新手,所以请随意指出我的代码有多难看

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)

Tags: namein图像文本importforitemsfiles
1条回答
网友
1楼 · 发布于 2024-10-02 20:39:42

每当您有一系列重复的变量声明时,如下所示:

txt1= "name..."
W1 = 1200
H1 = 200
fontSize1 = 142

txt2= "country..."
W2 = 1200
H2 = 400
fontSize2 = 132

txt3= "code..."
W3 = 1200
H3 = 600
fontSize3 = 124

这是一个很好的线索,你最好有一份清单:

text_props = [
    ("name...", 1200, 200, 142),
    ("country...", 1200, 400, 132),
    ("code...", 1200, 600, 124),
]

现在可以在循环中设置这些属性,以便:

        setText(imageName,imgfile,txt1,fontSize1,W1,H1)
        setText(imageName,imgfile,txt2,fontSize2,W2,H2)
        setText(imageName,imgfile,txt3,fontSize3,W3,H3)

变成这样:

        for txt, w, h, font in text_props:
            setText(imageName, imgfile, txt, font, w, h)

在我自己的代码中,我可能会使用NamedTuple而不是普通的tuple,但这是另一个主题。:)

(编辑)如果要将CSV数据交换为txt值,我认为您要做的是将键名粘贴在其中(不带点),如下所示:

text_props = [
    ("name", 1200, 200, 142),
    ("country", 1200, 400, 132),
    ("code", 1200, 600, 124),
]

然后在iterrows循环中执行此操作(您已经拥有所有数据),而不是构建items列表:

for _, row in df.iterrows():
    item = row["item"]
    if item not in files:
        continue
    imgfile = Image.open(f"./{item}")
    for key, w, h, default_font_size in text_props:
        # key is one of 'name', 'country', or 'code'.
        # Not all keys have a font size, so check for one but
        # use default_font_size if none is in the table.
        font_size = row.get(key+'fontsize', default_font_size)
        setText(
            item,      # name (the image filename)
            imgfile,   # file (from Image.open())
            row[key],  # txt (from the table, e.g. row['name'])
            font_size, # fontSize (see above where this is determined)
            w,         # w (width)
            h          # h (height)
        )

相关问题 更多 >