zoukankan      html  css  js  c++  java
  • python直接下载图片到内存

    1. 使用requests(推荐)

    from PIL import Image
    import requests
    Image.open(requests.get(url, stream=True).raw)

    2. 使用StringIO

    from PIL import Image
    from StringIO import StringIO
    import requests
    
    r = requests.get("http://a/b/c")
    im = Image.open(StringIO(r.content))
    im.size

    # =======================
    from PIL import Image
    import urllib2 as urllib
    from StringIO import StringIO
    
    fd = urllib.urlopen("http://a/b/c")
    im = Image.open(StringIO(fd.read()))
    im.size
     

    3. 使用io.BytesIO

    from PIL import Image
    import urllib2 as urllib
    import io
    
    fd = urllib.urlopen("http://a/b/c")
    image_file = io.BytesIO(fd.read())
    im = Image.open(image_file)

     ====

    #!/usr/bin/env python
    # coding=utf-8
    
    from urllib2 import urlopen
    import random
    import time
    
    from multiprocessing import Pool 
    
    url_root = 'http://www.beianbeian.com/gaoji/validate_code?a='
    cnt = 1000
    
    def download_img(url, path='static/uploads/'):
        global cnt
        while cnt < 2000:
            fname = path + "BA%d.jpg" % cnt
            with open(fname, 'wb') as fw:
                try:
                    fw.write(urlopen(url).read())
                    print fname, 'done'
                    cnt += 1
                except Exception as e:
                    print 'Error', e
                    continue
    
            time.sleep(0.2)
    
    if __name__ == "__main__":
    
        pool = Pool(processes=4)
        for i in range(10):
            randNum = random.random()
            url = url_root + str(randNum)
            pool.apply(download_img, args=(url,))
            pool.close()
            pool.join()
    每天一小步,人生一大步!Good luck~
  • 相关阅读:
    TSP-UK49687
    维度建模的基本原则
    差分约束系统
    随机过程初步
    随机过程——维纳过程
    Xilinx FPGA复位信号设计
    10 Row Abacus
    Python
    FX2LP与FPGA的简单批量回环
    DFT公式的一个简单示例
  • 原文地址:https://www.cnblogs.com/jkmiao/p/6295732.html
Copyright © 2011-2022 走看看