M00N KNIGHT
M00N KNIGHT

Reputation: 137

How to change where the output directory where the new images go

I have a small query I'm hoping someone can help me out within Python 3. I am resizing a dataset of 10000 images to all be 1000x1000 in dimension before I do any pytorch analysis with it. I just wanted to ask how I change my code to save the outgoing images to a new folder I have created ''train_resized'' instead of the same folder as the original files as it is doing now when I run. Thanks

# Testing dataset

from PIL import Image

import os, sys

path = (r'G:\My Drive\CATSVDOGS2.0\test1\\')
dirs = os.listdir( path )

def resize():
    for item in dirs:
        if os.path.isfile(path+item):
        im = Image.open(path+item)
        f, e = os.path.splitext(path+item)
        imResize = im.resize((1000,1000), Image.ANTIALIAS)
        imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

resize()

Upvotes: 1

Views: 716

Answers (1)

user483934
user483934

Reputation: 407

In your line

imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

you're setting the path when using the variable f, as f uses the path variable you defined. A quick way to set the path is to do something like:

imResize.save('G:\\My Drive\\Path\\To\\Folder\\' + item + ' resized.jpg', 'JPEG', quality=90)

of course specify the path to be whatever you want. Untested as I don't have Python installed on my work machine, but that is the general gist.

Upvotes: 2

Related Questions