6 examples of 'how to read images from a folder in python' in Python

Every line of 'how to read images from a folder in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
30def __init__(self, folder_path):
31 self.fnames = sorted(glob.glob(folder_path + "/*"))
32 self.cnt_imgs = 0
33 self.cur_filename = ""
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

97def read_image(images_folder):
98 """ It reads a single image file into a numpy array and preprocess it
99
100 Args:
101 images_folder: path where to random choose an image
102
103 Returns:
104 im_array: the numpy array of the image [width, height, channels]
105 """
106 # random image choice inside the folder
107 # (randomly choose an image inside the folder)
108 image_path = os.path.join(images_folder, random.choice(os.listdir(images_folder)))
109
110 # load and normalize image
111 im_array = preprocess_image(image_path)
112 # im_array = read_k_patches(image_path, 1)[0]
113
114 return im_array
86def read_images_from(path=Data_PATH):
87 images = []
88 png_files_path = glob.glob(os.path.join(path, 'train/', '*.[pP][nN][gG]'))
89 for filename in png_files_path:
90 im = Image.open(filename) # .convert("L") # Convert to greyscale
91 im = np.asarray(im, np.uint8)
92 # print(type(im))
93 # get only images name, not path
94 image_name = filename.split('/')[-1].split('.')[0]
95 images.append([int(image_name), im])
96
97 images = sorted(images, key=lambda image: image[0])
98
99 images_only = [np.asarray(image[1], np.uint8) for image in images] # Use unint8 or you will be !!!
100 images_only = np.array(images_only)
101
102 print(images_only.shape)
103 return images_only
33def process_images(path):
34 if isinstance(path, list):
35 for path_i in path:
36 process_images(path_i)
37 return
38
39 if path.is_file():
40 extract_and_save(path)
41 else:
42 for file in path.glob('*.jpg'):
43 extract_and_save(file)
10def get_images(path):
11 extensions = ['.jpg', '.jpeg', '.JPG', '.png']
12
13 images = []
14 for ext in extensions:
15 images.extend(glob.glob(path + '/*' + ext))
16 return images
26def read_images(sz=None):
27 path="./data/at/"
28 c = 0
29 X,y = [], []
30 for dirname, dirnames, filenames in os.walk(path):
31 for subdirname in dirnames:
32 subject_path = os.path.join(dirname, subdirname)
33 for filename in os.listdir(subject_path):
34 try:
35 if (filename == ".directory"):
36 continue
37 filepath = os.path.join(subject_path, filename)
38 im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
39 if (sz is not None):
40 im = cv2.resize(im, (200, 200))
41 X.append(np.asarray(im, dtype=np.uint8))
42 y.append(c)
43 except IOError, (errno, strerror):
44 print "I/O error({0}): {1}".format(errno, strerror)
45 except:
46 print "Unexpected error:", sys.exc_info()[0]
47 raise
48 c = c+1
49 return [X,y]

Related snippets