title: Memory Error when plotting images using loops python
body: <p>So I have a lot of images that I am plotting for data quality control purposes. I am using nested loops to go through and extract the gzipped files and then plot and save the images as .png so they can later be referred to. I can get it to run for a while but eventually I will get a Memory Error. I don't know if I am making a dumb mistake, any help is appreciated.</p>
  
  <p>I have tried making temp folders to extract the gzipped files to and then removing them. I have also tried closing all the plots after I make them each time in the loop, however I still get a memory error after running it for a while. I did some googling and an issue with flushing and fsync, as well as the fact that the gzipped files are on a NTFS formatted external hard drive, but that is where I get lost.</p>
  
  <p>Below is the my relevant code:</p>
  
  <pre><code>while len(thelist) &gt; 0 :
      temppath = tempfile.mkdtemp()
      os.chdir(temppath)
  
      tpath = thelist.pop()
  
      myextractor.myextract(tpath)
  
      paths = glob('*HRV')+glob('*.IR120')+glob('*.WV73')+glob('*.VIS8')+glob('*.VIS120')+glob('*.VIS6')+glob('*.IR16')
  
  for x in range(0, len(paths)):
      #get the files and paths correct
      spath ='/' + paths.__getitem__(x)
      spath1 = spath.replace('\\','/')
      spath = temppath+ spath1
      spath = spath.replace('\\','/')
  
      r = 0
      r=McIdasObject.McIdasImageFile(spath)
      #the semi colon is important ..maybe
  
      fig = plt.imshow (np.array(r));
  
      #most of this is just getting the correct file and path name
      strpath = str(temppath)
      strpath = strpath.replace('c:\\users\\appdata\\local\\temp\\','/')
      folder = tpath[20]+tpath[21]+tpath[22]+tpath[23]+tpath[24]+tpath[25]+tpath[26]+tpath[27]+tpath[28]
      newpath = savepath + '/' + folder
      if not os.path.exists(newpath): os.makedirs(newpath)
          print 'saving:' + newpath + spath1 + '.png'
  
  
      plt.savefig(newpath + spath1+ '.png')
      r=0
      plt.close("all")
  
  
      gc.collect()
  os.chdir(destin)
  if len(paths) &lt; 1 :
      badimages.append(temppath)
  else:
      shutil.rmtree(temppath)
  </code></pre>
  
  <p>This is the trace back</p>
  
  <p>Traceback (most recent call last):</p>
  
  <pre class="lang-none prettyprint-override"><code>File "&lt;ipython-input-1-22dce83b27e3&gt;", line 1, in &lt;module&gt;
    runfile('C:/Users/Alex/Documents/Python Scripts/image printer.py', wdir='C:/Users/Alex/Documents/Python Scripts')
  File "C:\Users\Alex\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "C:\Users\Alex\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/Alex/Documents/Python Scripts/image printer.py", line 57, in &lt;module&gt;
    plt.savefig(newpath + spath1+ '.png')
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\figure.py", line 1476, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 161, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2211, in print_figure
    **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 521, in print_png
    FigureCanvasAgg.draw(self)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 469, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\figure.py", line 1085, in draw
    func(*args)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2110, in draw
    a.draw(renderer)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\image.py", line 373, in draw
    im = self.make_image(renderer.get_image_magnification())
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\image.py", line 597, in make_image
    transformed_viewLim)
  File "C:\Users\Alex\Anaconda\lib\site-packages\matplotlib\image.py", line 219, in _get_unsampled_image
    x = (x * 255).astype(np.uint8)
  MemoryError
  </code></pre>