Load just part of an image in python

后端 未结 4 1577
礼貌的吻别
礼貌的吻别 2020-11-30 07:56

This might be a silly question, but...

I have several thousand images that I would like to load into Python and then convert into numpy arrays. Obviously this goes a

4条回答
  •  北海茫月
    2020-11-30 08:23

    Oh I just realized there might be a far, far simpler way than doing what I wrote above regarding the BMP files.

    If you are generating the image files anyway, and you always know which portion you want to read, simply save that portion out as another image file while you're generating it:

    import numpy as np
    import matplotlib.pyplot as plt
    import Image
    
    #Generate sample images
    num_images = 5
    
    for i in range(0,num_images):
        Z = np.random.rand(2000, 2000)
        plt.imsave('%03i.png'%i, Z)
        snipZ = Z[200:300, 200:300]
        plt.imsave('%03i.snip.png'%i, snipZ)
    
    #load the images
    for i in range(0,num_images):
        im = Image.open('%03i.snip.png'%i)
    
        #convert them to numpy arrays
        data = np.array(im)
    

提交回复
热议问题