0

I tried to resize some image files and save them in other directory. and there's not any error but files didn't save in directory I designated.

path = r"C:\Users\abc123\Desktop\various_image"
valid_images = [".jpg",".gif",".png",".tga"]

for f in os.listdir(path):
    ext = os.path.splitext(f)[1]
    if ext.lower() not in valid_images:
        continue
    im = Image.open(os.path.join(path,f))
    resize_im = im.resize((256, 256), Image.ANTIALIAS)
    ext = ".jpg"
    resize_im.save(r"C:\Users\abc123\Desktop\Movie" + f + ext, "JPEG" )
    break

I wanted to test 1 image before running the whole code so I put 'break'

what I wanted to test is 'f.jpg' is saved in C:\Users\abc123\Desktop\Movie as I said above there's not any error but file is not saved in directory. what's wrong with my code? thanks for your time.

PudgeKim
  • 131
  • 1
  • 5
  • 11

1 Answers1

0

'+' does not actually add an element to the path, but concatenates with the string directly. So the program did in fact create a file, but not where you intended, but at "C:\Users\abc123\Desktop\MovieFile.jpg", assuming that there is file called File in the "C:\Users\abc123\Desktop\various_image" folder.

I think what you were trying to write was

resize_im.save(os.path.join(r"C:\Users\abc123\Desktop\Movie", f + ext), "JPEG" )
Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • Thanks for your answer but there's still problem.. 1. if there's no 'MoveFile.jpg' folder, im.save() create a folder and save in there? if it is, there's no folder named MovieFile.jpg 2. how can i find my resize file that saved in "C:\Users\abc123\Desktop\MovieFile.jpg" ? 3. I forgot os.path.join until I saw your answer and I think it's answer to do but when I run it, my resize file still doesn't appear in \desktop\Movie what's wrong..? – PudgeKim Jul 21 '19 at 09:01
  • Have you maybe tried to print the result of `os.path.join`? Because I'm pretty sure the file is exactly where that string points you. – Finomnis Jul 21 '19 at 09:04
  • I printed print(os.path.join(r"C:\Users\abc123\Desktop\Movie", f + ext)) and nothing's printed.. what is it..?? – PudgeKim Jul 21 '19 at 09:14
  • Did you check that *anything* inside the loop gets executed, and you don't just continuously run into `continue`? I strongly suggest looking into an IDE like PyCharm and learning how to step through code with the debugger. – Finomnis Jul 21 '19 at 09:38
  • I resolved it. problem is 'f + ext' cause f already includes '.jpg' Anyway, thanks for answering. – PudgeKim Jul 21 '19 at 10:34
  • Well, of course, but a file called `image.jpg.jpg` should work, too. Also, you store files as jpg but accept png and others, which means if you don't add `.jpg` you might end up with a jpg image with .png extension. – Finomnis Jul 21 '19 at 14:27