python reads all jpg files in the directory and displays an example of the first picture

  • 2021-06-28 13:03:32
  • OfStack

As follows:


# -*- coding: UTF-8 -*-
import numpy as np
import os
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
from glob import glob
 
 
#  Read all in the directory jpg picture 
def load_image(image_path, image_size):
  file_name=glob(image_path+"/*jpg")
  sample = []
  for file in file_name:
    pic = imread(file).astype(np.float32)
    pic = imresize(pic, (image_size, image_size)).astype(np.float32)
    sample.append(pic)
 
  sample = np.array(sample)
  return sample
 
if __name__=='__main__':
  samples=load_image("./images",150)
  #  Show 1 Picture 
  pic=samples[:1,:,:,:]
  pic=np.reshape(pic,(150,150,3)).astype(np.uint8)
  plt.imshow(pic)
  plt.show()

Related articles: