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!