Can matplotlib add metadata to saved figures?

后端 未结 4 901
青春惊慌失措
青春惊慌失措 2021-01-31 16:53

I want to be able to ascertain the provenance of the figures I create using matplotlib, i.e. to know which version of my code and data created these figures. (See this essay for

4条回答
  •  一整个雨季
    2021-01-31 17:19

    I don't know of a way using matplotlib, but you can add metadata to png's with PIL:

    f = "test.png"
    METADATA = {"version":"1.0", "OP":"ihuston"}
    
    # Create a sample image
    import pylab as plt
    import numpy as np
    X = np.random.random((50,50))
    plt.imshow(X)
    plt.savefig(f)
    
    # Use PIL to save some image metadata
    from PIL import Image
    from PIL import PngImagePlugin
    
    im = Image.open(f)
    meta = PngImagePlugin.PngInfo()
    
    for x in METADATA:
        meta.add_text(x, METADATA[x])
    im.save(f, "png", pnginfo=meta)
    
    im2 = Image.open(f)
    print im2.info
    

    This gives:

    {'version': '1.0', 'OP': 'ihuston'}
    

提交回复
热议问题