0

I have a Python program which processes images and save the resulting output to a file. I am receiving the input and output file names via console using input(). But I want to save the output automatically with the file name as something like "input_file_name_out". Note the string "_out" attached to the input file name

This will help me get user input only for Input file names thereby saving little time for the user since he doesn't need to think everytime what should be the output file name

Sample code snippet

if __name__ == "__main__":

    in_file = input()
    out_file = input()

    processed = process_image(in_file,out_file)

Expected code snippet

if __name__ == "__main__":

    in_file = input()
    # out_file = input() ------Not getting the file name from user thereby supplying only one argument to the below function

        processed = process_image(in_file)

# And within the function
def process_image(img):
    .
    .
    .
    out_file = img+"_out.jpg" ##### What should come here to achieve my requirement ########
    cv2.imwrite(out_file,processed_image)

3 Answers 3

2

you could make use of the Path class from the pathlib module:

from pathlib import Path

in_file = Path(input())
insert = '_out'

out_file = in_file.parent / (in_file.stem + insert + in_file.suffix)

Example:

in_file = Path('D:/folder/test.jpg')
insert = '_out'
out_file = in_file.parent / (in_file.stem + insert + in_file.suffix)

print(out_file)
D:\folder\test_out.jpg
Sign up to request clarification or add additional context in comments.

3 Comments

while @Redowan's answer is the simplest I have to acknowledge that this is the technically correct answer
@JKC: as in most cases, more generally applicable solutions come with a bit of overhead ;-) If you're not familiar with pathlib yet, make sure to check it out. For specific cases or quick hacks, an approach like Redowan suggested is also fine I guess.
Yeah, this approach generalizes better!!
1

All you have to do is,

  • Take the input_img.jpg that you get from user input
  • Split the input_img.jpg with delimiter . and that will give you this list: ['input_img', 'jpg']
  • Pick up the first element and add _out.jpg suffix
def process_image(img):

    # you should split with delimiter ('.'), take the first 
    # element of the list and add "_out.jpg" suffix

    split_name = img.split('.')
    out_file = split_name[0] + "_out" + "." + split_name[1] 
    cv2.imwrite(out_file, processed_image)


if __name__ == "__main__":

    in_file = input()
    processed = process_image(in_file)

4 Comments

Thanks for your prompt response. This helps
The OP asked specifically for .jpg case. Multiple things can be done if the extension is different. One way can be hardcoding .jpg, .png etc. Another way can be asking the user about extension and applying that. Numerous things can be done.
what I'm trying to point out is that hardcoding '.jpg' makes the code unflexible / very specific. Since you already have the result of .split('.'), why not at least append the original extension?
Oh sorry! yeah that's a better approach. My bad! I didn't get that from your earlier comment.
-1

Well that's simple! Just take the "in_file" string you asked for, and concat "_out" to the end and pass it as the out_file.

If you only process ".jpg"

cv2.imwrite(in_file[:-4]+"_out.jpg",processed_image)

if its variable:

cv2.imwrite(in_file[:-4]+"_out"+in_file[-4:],processed_image)

3 Comments

But the problem here is the in_file ends with extension as input_file.jpg. So my output file name should be input_file_out.jpg. So I need to first split the input file name and then concat _out and extension
Right. Sorry... updated the answer. You can splice the last 4 characters from the input and concatinate them back, either manually or automatically.
hardcoding the index -4 is pretty unflexible; what if the file extension has 4 characters, e.g. 'jpeg'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.