0

I have a class method to which I have to pass in image path as argument, the method looks like this

self.add_image(
    "nucleas",
    image_id=image_id,
    path=os.path.join(dataset_dir, image_id, "images/{}.jpg".format(image_id)))

but my folder has both, jpg and jpeg images, is there way it can give options like jpg/jpeg.

I was thinking of using try/except for the argument, but I'm not sure if that will work.

Any suggestions would be helpful.

2
  • 3
    Do you know the correct extension beforehand? Then you could just pass the extension as a second format paramter. If you don't know the extension, and you just want the path to somehow match both files, I don't see how this is related to format. Commented Oct 22, 2018 at 9:31
  • "{} - {}".format(1, 2) gives 1 - 2 if that is what you are asking for. Commented Oct 22, 2018 at 9:39

1 Answer 1

1

It's not possible to give the Path module a selection of extensions and have it pick the right one.

I'd suggest writing a little helper function to get the right path:

def get_image_path(dataset_dir, image_id):
    jpg_path = os.path.join(dataset_dir, image_id, "images/{}.jpg".format(image_id)
    if jpg_path.exists():
        return jpg_path

    jpeg_path = os.path.join(dataset_dir, image_id, "images/{}.jpeg".format(image_id)
    if jpeg_path.exists():
        return jpeg_path

    # Neither path exists, maybe raise an exception? Or just return None

Then in your existing code:

self.add_image(
    "nucleas",
    image_id=image_id,
    path=get_image_path(dataset_dir, image_id)
)
Sign up to request clarification or add additional context in comments.

1 Comment

This should work, caught up without something else at the moment , will try this and let you know, in the mean time...

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.