Use original file name to save image

天涯浪子 提交于 2020-01-06 07:45:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!