Load just part of an image in python

后端 未结 4 1594
礼貌的吻别
礼貌的吻别 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:21

    I have run some timing tests and I am sorry to say I don't think you can get much faster than the PIL crop command. Even with manual seeking/low level reading you still have to read the bytes. Here is the timing results:

    %timeit im.crop((1000-50,1000-50,1000+50,1000+50))
    fid = open('003.png','rb')
    %timeit fid.seek(1000000)
    %timeit fid.read(1)
    print('333*100*100/10**(9)*1000=%.2f ms'%(333*100*100/10**(9)*1000))
    
    
    100000 loops, best of 3: 3.71 us per loop
    1000000 loops, best of 3: 562 ns per loop
    1000000 loops, best of 3: 330 ns per loop
    333*100*100/10**(9)*1000=3.33 ms
    

    As can be seen the bottom calculation we have a read 1 byte *10000 bytes (100x100 subimage)*333ns per byte=3.33ms which is the same as the crop command above

提交回复
热议问题