0

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])
Miki
  • 40,887
  • 13
  • 123
  • 202
pixelthread
  • 93
  • 1
  • 1
  • 8

1 Answers1

0

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')
bendl
  • 1,583
  • 1
  • 18
  • 41
  • i already did that and it returns `run() missing 1 required positional argument: 'file'` this error – pixelthread Jan 09 '18 at 21:07
  • how did you call the `run()` method? – bendl Jan 09 '18 at 21:07
  • `if __name__ == "__main__": main()` full code is above already given – pixelthread Jan 09 '18 at 21:10
  • @pixelthread but nowhere in that above code is `run()` actually called... In any case, my best guess is `run(dirs, img, file[:-4])` – bendl Jan 09 '18 at 21:13
  • @pixelthread you're going to have to help me help you. I realize I made a typo in my above comment, it should have been `run(dirs, img, file_[:-4])`. If that doesn't work, you're going to have to show me the actual code where you're calling `run()` because otherwise there is no way to help – bendl Jan 09 '18 at 21:24
  • `[:-4]` is exactly the part that removes the `.png` part. For real, you're going to have to edit your question – bendl Jan 09 '18 at 21:30
  • @pixelthread the syntax error is because you're assigning the return values of `run()` to `labels` and `output_img`, but `run()` doesn't actually return a value. My best guess is that you should add one last line at the end of your function, outside all the loops: `return (labels, Zeroth)` – bendl Jan 09 '18 at 21:35
  • I'm not really sure what you want... You've already gone way past the scope of the question. If you want to ask another you can do that, but you're going to have to give much more detail about what it is you're trying to solve. I don't know which questions to ask because I don't know your code or your application – bendl Jan 09 '18 at 21:41
  • i did `return (labels, Zeroth)` but still syntax error – pixelthread Jan 09 '18 at 21:47