一、写在前面
需求:文件夹内有若干张图片,文本文件内有若干字段,希望将图片按顺序命名为文本文件中的内容,使用python简单实现。
二、源码
#rename picture
train_txt='./data/data/test.txt'
train_words = list(open(train_txt, "r", encoding='utf-8').readlines())
train_words=train_words[0]
from PIL import Image
for i in range(1,101):
print('i=',i)
img = Image.open("C:/Users/Administrator/Desktop/test_results/"+str(i)+".png")
img = img.convert("L")
img = img.resize((960,96))
print('train_words[i]=',train_words[i])
img.save("C:/Users/Administrator/Desktop/testresults/"+train_words[i]+".png", "PNG")
三、提示
train_text='./data/data'#图片路径
txt文件需要是utf-8格式。