使用原始文件名保存图像 [英] Use original file name to save image
本文介绍了使用原始文件名保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用原始文件名保存图像,例如,如果原始文件名是hero
,则处理后的图像名应为hero_Zero.png
.
我不知道如何在def run(dirs, img):
中传递文件名
下面的代码是正确的并已修改.
I want to save image with original file name for example if original file name is hero
so the processed image name should be hero_Zero.png
.
I dont know how to pass file name in def run(dirs, img):
Below code is correct and modified.
def run(dirs, img, file_):
for (x, y) in labels:
component = uf.find(labels[(x, y)])
labels[(x, y)] = component
if labels[(x, y)]==0:
Zero[y][x]=int(255)
count=count+1
if count<=43:
continue
elif count>43:
Zeroth = Image.fromarray(Zero)
Zeroth.save(os.path.join(dirs, file_+'Zero.png'), 'png')
return (labels, output_img)
def main():
path='E:/Dataset/1/'
for root, dirs, files in sorted(os.walk(path)):
for file_ in files:
#print (dirs)
# print (files)
full_file_path = os.path.join(root, file_)
img = Image.open(full_file_path)
(labels, output_img) = run(root, img, file_[:-4])
推荐答案
在函数定义中添加一个额外的参数.除了传递目录和图像,还传递文件名.结果将如下所示:
Add an extra argument to your function definition. Instead of just taking the directory and image, also pass the filename. The result will look like this:
def run(dirs, img, f_name):
for (x, y) in labels:
component = uf.find(labels[(x, y)])
labels[(x, y)] = component
if labels[(x, y)]==0:
Zero[y][x]=int(255)
count=count+1
if count<=43:
continue
elif count>43:
Zeroth = Image.fromarray(Zero)
Zeroth.save(os.path.join(dirs, f_name + '_Zero.png'), 'png')
这篇关于使用原始文件名保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文