title: What's the fastest way to read images from urls?
body: <p>I want to make a generator that generates batches of images from urls to train a <code>keras</code> model. I have another generator that feeds me images urls.</p>
  
  <p>What I currently do is download the image to disk and then load the image from the disk.</p>
  
  <pre><code>def loadImage(URL):
      with urllib.request.urlopen(URL) as url:
          with open('temp.jpg', 'wb') as f:
              f.write(url.read())
  
      img_path = 'temp.jpg'
      img = image.load_img(img_path, target_size=(125, 125))
      os.remove(img_path)
      x = image.img_to_array(img)
      return x
  
  def imageGenerator(batch_size):
      i = 0
      batch = []
      for URL in imageUrlGenerator():
          if i&gt;batch_size:
              yield batch
              batch = []
              i=0
          batch.append(loadImage(URL))
          i+=1
  </code></pre>
  
  <p>This works but I wonder if there isn't a faster way to load images from the web without having to write and read in/from disk.</p>