图片命名问题(一)-str.format
图片命名问题(二)-输出固定位数的数字
复现TSM过程中,在__getitem__函数中碰到的,这里本可以不求甚解,但决定还是把它搞明白吧
代码是这样的:
if self.image_tmpl == 'flow_{}_{:05d}.jpg':
file_name = self.image_tmpl.format('x', 1)
full_path = os.path.join(self.root_path, record.path, file_name)
这里的self.image_tmpl代表的生成图片的名字
但是具体是什么呢
这里运行一下相似的程序:
image_tmpl = 'flow_{}_{:05d}.jpg'
file_name_1 = image_tmpl.format('x', 1)
image_tmpl = 'flow_{}_{:08d}.jpg'
file_name_2 = image_tmpl.format('y', 2)
file_name_1, file_name_2
结果:
('flow_x_00001.jpg', 'flow_y_00000002.jpg')
这里显而易见了,知识点就是Python中的format函数应用和固定字符位数了
format函数应用在图片命名问题(一)中,本人就不班门弄斧了
固定字符位数,这里使用了{:数字d}来实现,这个以后记住就行了