问题
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])
回答1:
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')
来源:https://stackoverflow.com/questions/48176514/use-original-file-name-to-save-image