how can i get the file name without its root and path. only a name so i can save the images i want to convert with the same original name?
import sys
import os,glob
from PIL import Image, ImageFilter
path = os.getcwd()
if not os.path.exists('converted'):
save_to = os.makedirs('converted')
for i in glob.glob(f'{path}/*.jpg'):
img = Image.open(i)
img.save(???????) ######## <---------?????????
#i want to save the imege whith its original name,
# but with a different extention, and i want to save it to a different folder then cwd
CodePudding user response:
Use os.path.basename
to get filename without path.
Use os.path.splitext
to split filename into stem and suffix.
CodePudding user response:
Did you wanted something like this?
for i in glob.glob(f'{path}/*.png'):
img = Image.open(i)
basename = os.path.basename(i)
root_ext = os.path.splitext(basename)[0]
ext = ".png"
img.save(path "/converted/" root_ext ext)