Search code examples
pythonjsonpng

loading png from json file using PIL


I am trying to use PIL.Image.open to open the image from an opened json file. Should I just put the name of the png file inside the Image.open() or is it the directory that leads to the json file?

Thanks


Solution

  • Here is a full example which is probably what you're looking for:

    
        import json
        from PIL import Image
        
        with open('yourfilename.txt', 'r') as f:
            data = json.load(f)
        
            # Load the file path from  the json
            imgpath = data['yourkey']
            
            # Place the image path into the open method
            img = Image.open(imgpath)
    
    

    The imgpath variable should return a string of the path to your image.

    Pillows Image.open docs