import gc
SIZE = 1024*768
MEMOSIZE = 1024 # your memory size
with open('xxx', 'rb') as fp: # open the file
i = 0 # remember the number to gc in time
queue = []
while True:
if (i*(SIZE-1) < MEMOSIZE):
x = fp.read(SIZE) # if your image is single channel
queue.append(x)
# do something
else:
del queue
gc.collect()