1

I'm trying to create a script that changes the contrast and sharpness of all the images in a folder, and save it to a new folder as filename_edited.jpg

I'm using pythong 3.7, importing os and PIL. I have a folder with some images - DJI_001.jpg, DJI_002.jpg and so forth. The folder with the images is called test.

The folder I want them to go to is called 'novo' in ..\test\novo

Initially I had this. It works, but it saves the images in the same folder:

import PIL
from PIL import Image, ImageEnhance
import os.path, sys

path = "C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test" 
dirs = os.listdir(path)


def teste():
    for item in dirs:
        fullpath = os.path.join(path,item)
        if os.path.isfile(fullpath):
            img = Image.open(fullpath) 
            f, e = os.path.splitext(fullpath) 
            sharpness = ImageEnhance.Sharpness(img) 
            sharp = sharpness.enhance(10.0)
            newimg = sharp
            contrast = ImageEnhance.Contrast(newimg)
            cont = contrast.enhance(2.3)
            head, tail = os.path.split(fullpath)
            cont.save(f + "_edited.jpg")

teste()

So after some research I tried to split fullpath in head and tail. The tail variable gets me the file name.

I did this so each time it looped, It could save to the filepath of my new folder + tail.

So I tried this:

def sha():
    for item in dirs:
        fullpath = os.path.join(path,item)
        if os.path.isfile(fullpath):
            img = Image.open(fullpath) 
            #f, e = os.path.splitext(fullpath) #don't need this here
            sharpness = ImageEnhance.Sharpness(img) 
            sharp = sharpness.enhance(10.0)
            newimg = sharp
            contrast = ImageEnhance.Contrast(newimg)
            cont = contrast.enhance(2.3)
            head, tail = os.path.split(fullpath)
            cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo" + tail)
            print("this is the filepath: " + head)
            print("this is the filename: " + tail)

sha()

I thought this would work, but it saves the files on the same directory, as novoDJI_001.jpg, novoDJI_002.jpg and so forth.

I added a couple images if it helps:

Saving in the same folder and Trying to save on a new folder

So, on my second attempt (more like 20th but well), as you can see I inerted the filepath, but the new folder called novo, in \test\novo ended up in the file name.

Any help is greatly appreciated, I'm sure this is simple but I've spent the last 5 hours on this, but I can't find why it does this! Thank you!

1 Answer 1

1

There's no path separator in this line betweeen "novo" and tail:

cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo" + tail)

Please, replace it with something like this:

cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo\\" + tail)    // '\\' added after 'novo'

or you may use this as well:

new_name = os.path.join( head, "novo", tail )
cont.save( new_name )
Sign up to request clarification or add additional context in comments.

2 Comments

This worked! Thank you so much, this is the first time I managed to automate something useful I ended up using: new_name = os.path.join( head, "novo", tail ) cont.save( new_name ) With this I can put all the new files in the folder and with the suffix I want. Thanks again
you're welcome! if you find the answer useful, you may upvote and/or accept it.

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.