Search code examples
pythonimagedirectorypython-imaging-library

Opening a photo from a directory


I am trying to open images from a kaggle dataset, which I have imported into my python program and have loaded I am unable to open the photo, when I run the code it simply produces a blank space

I tried the following code below (this is a section of my wider code), yet it only produces a with

def loadImages(path):
    # return array of images

    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(path + image)
        loadedImages.append(img)

    return loadedImages
path = "embryo-classification-based-on-microscopic-images/test/0/"

imgs = loadImages(path)
for img in imgs:
  img.show()

Solution

  • Someone had a solution on a website, I filled in the information in the code, and it was this:

    from matplotlib import pyplot as plt
    from PIL import Image
    import os
    
    def load(path):
        # Return array of images
        imagesList = os.listdir(path)
        loadedImages = []
        for image in imagesList:
            img_path = os.path.join(path, image)
            img = Image.open(img_path)
            loadedImages.append(img)
        return loadedImages
    
    path = "embryo-classification-based-on-microscopic-images/test/0/"
    imgs = load(path)
    
    # Display images using matplotlib
    for img in imgs:
        plt.imshow(img)
        plt.axis('off')  # Hide axis
        plt.show()
    

    The dataset path is inserted in the load function so it contains the image. Use matplotlib to display, if any problem happens, please tell me to edit my answer, thank you!