I have a python script to procces jpeg on a folder and overwrite the names after done
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
#import jpegs from Repaired folder
im = Image.open('photo_2022-06-09_19-58-19.JPG')
# Crop
im = ImageOps.crop(im, border=17)
# Constract
im = ImageOps.autocontrast(im, cutoff = 1)
#sharpness
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
#color
enhancer = ImageEnhance.Color(im).enhance(3)
#save all jpegs and overwrite original file names
im.save("photo_2022-06-09_19-58-19.JPG", quality="maximum")
How can i process all jpeg files in that dir have a folder "repaired" and Overwrite the name of each processed file with the original file name?
CodePudding user response:
I would use glob
and do something like this:
import glob
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
for fname in glob.glob('./repaired/*.JPG'):
im = Image.open(fname)
im = ImageOps.crop(im, border=17)
im = ImageOps.autocontrast(im, cutoff = 1)
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
enhancer = ImageEnhance.Color(im).enhance(3)
im.save(fname, quality="maximum")
You can print fname
to see what it looks like, and process that string if you need to get the filename out, e.g. to write the file into a different folder.
CodePudding user response:
So it should be like this
import os
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
#import jpegs from Repaired folder
for filename in os.listdir():
if filename.endswith(".jpg"):
im = Image.open(filename)
# Crop
im = ImageOps.crop(im, border=17)
# Constract
im = ImageOps.autocontrast(im, cutoff = 1)
#sharpness
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
#color
enhancer = ImageEnhance.Color(im).enhance(3)
#save all jpegs and overwrite original file names
im.save(filename, quality="maximum")
else:
continue