下载远程图像并将其保存到Django模型

May*_*ain 30 python django django-models

我正在编写一个Django应用程序,它将获取特定URL的所有图像并将它们保存在数据库中.

但我没有介绍如何在Django中使用ImageField.

Settings.py

MEDIA_ROOT = os.path.join(PWD, "../downloads/")

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "htp://media.example.com/"
MEDIA_URL = '/downloads/'
Run Code Online (Sandbox Code Playgroud)

models.py

class images_data(models.Model):
        image_id =models.IntegerField()
        source_id = models.IntegerField()
        image=models.ImageField(upload_to='images',null=True, blank=True)
        text_ind=models.NullBooleanField()
        prob=models.FloatField()
Run Code Online (Sandbox Code Playgroud)

download_img.py

def spider(site):
        PWD = os.path.dirname(os.path.realpath(__file__ ))
        #site="http://en.wikipedia.org/wiki/Pune"
        hdr= {'User-Agent': 'Mozilla/5.0'}
        outfolder=os.path.join(PWD, "../downloads")
        #outfolder="/home/mayank/Desktop/dreamport/downloads"
        print "MAYANK:"+outfolder
        req = urllib2.Request(site,headers=hdr)
        page = urllib2.urlopen(req)
        soup =bs(page)
        tag_image=soup.findAll("img")
        count=1;
        for image in tag_image:
                print "Image: %(src)s" % image
                filename = image["src"].split("/")[-1]
                outpath = os.path.join(outfolder, filename)
                urlretrieve('http:'+image["src"], outpath)
                im = img(image_id=count,source_id=1,image=outpath,text_ind=None,prob=0)
                im.save()
                count=count+1
Run Code Online (Sandbox Code Playgroud)

我在一个视图中调用download_imgs.py

        if form.is_valid():
                url = form.cleaned_data['url']
                spider(url)
Run Code Online (Sandbox Code Playgroud)

roc*_*ier 46

Django文档始终是一个好的开始

class ModelWithImage(models.Model):
    image = models.ImageField(
        upload_to='images',
    )
Run Code Online (Sandbox Code Playgroud)

更新

所以这个脚本有效.

  • 循环图像下载
  • 下载图片
  • 保存到临时文件
  • 适用于模特
  • 保存模型

.

import requests
import tempfile

from django.core import files

# List of images to download
image_urls = [
    'http://i.thegrindstone.com/wp-content/uploads/2013/01/how-to-get-awesome-back.jpg',
]

for image_url in image_urls:
    # Steam the image from the url
    request = requests.get(image_url, stream=True)

    # Was the request OK?
    if request.status_code != requests.codes.ok:
        # Nope, error handling, skip file etc etc etc
        continue

    # Get the filename from the url, used for saving later
    file_name = image_url.split('/')[-1]

    # Create a temporary file
    lf = tempfile.NamedTemporaryFile()

    # Read the streamed image in sections
    for block in request.iter_content(1024 * 8):

        # If no more file then stop
        if not block:
            break

        # Write image block to temporary file
        lf.write(block)

    # Create the model you want to save the image to
    image = Image()

    # Save the temporary image to the model#
    # This saves the model so be sure that is it valid
    image.image.save(file_name, files.File(lf))
Run Code Online (Sandbox Code Playgroud)

一些参考链接:

  1. 请求 - "人类的HTTP",我更喜欢urllib2
  2. tempfile - 保存temporay文件而不是磁盘
  3. Django filefield 保存

  • 我知道这不是一个可接受的评论,但这对我有帮助!谢谢,@ rockingskier (2认同)

Mic*_*tes 20

如果您想保存下载的图像而不先将它们保存到磁盘(不使用NamedTemporaryFile等),那么就可以轻松实现.

这比下载文件并将其写入磁盘稍快一些,因为它全部在内存中完成.请注意,此示例是为Python 3编写的 - 该过程在Python 2中类似,但略有不同.

from django.core import files
from io import BytesIO
import requests

url = "https://example.com/image.jpg"
resp = requests.get(url)
if resp.status_code != requests.codes.ok:
    #  Error handling here

fp = BytesIO()
fp.write(resp.content)
file_name = url.split("/")[-1]  # There's probably a better way of doing this but this is just a quick example
your_model.image_field.save(file_name, files.File(fp))
Run Code Online (Sandbox Code Playgroud)

your_model您要保存的模型实例在哪里,并且.image_field是该名称ImageField.

有关更多信息,请参阅io的文档.


小智 5

# this is my solution
from django.core import files
from django.core.files.base import ContentFile

import requests
from .models import MyModel

def download_img():
    r = requests.get("remote_file_url", allow_redirects=True)
    filename = "remote_file_url".split("/")[-1]

    my_model = MyModel(
        file=files.File(ContentFile(r.content), filename)
    )
    my_model.save()

    return
Run Code Online (Sandbox Code Playgroud)