Read and Save multiple images in a folder: Python
17,470
You have an asterisk in the save()
function which makes no sense, it has nothing to match. It will not automatically substitute the *
match from glob function. I think what you wanted to do something like this
src_fname, ext = os.path.splitext(x) # split filename and extension
# construct output filename, basename to remove input directory
save_fname = os.path.join(outpath, os.path.basename(src_fname)+'.tif')
im.save(save_fname)
Remember to use os.path
module functions instead of concatenating filenames with slashes as they handle edge cases correctly on every platform.
Author by
Gopal Krishna
Updated on June 05, 2022Comments
-
Gopal Krishna almost 2 years
I am a beginner in python. I am trying to read all of the ascii files from one folder and after converting them into Image, I want all of them to be saved into a different folder in same directory. The code is running but I am not getting any output and no error is displayed.
Code without loop, for single image is working perfectly.
Please help.
import os import glob import numpy as np from PIL import Image path = r'D:\user\ASCII' outpath = 'D:\user\ASCII\TIFF' filenames = glob.glob(path + "/*.asc") #read all files in the path mentioned for x in filenames: myarray = np.loadtxt(x, skiprows=9) im = Image.fromarray(myarray) im.save(outpath + '/*.tif')