相关疑难解决方法(0)

Python/Django从URL下载图像,修改并保存到ImageField

我一直在寻找一种从URL下载图像,在其上执行一些图像处理(调整大小)动作,然后将其保存到django ImageField的方法.使用两个很棒的帖子(下面链接),我已经能够下载图像并将其保存到ImageField.但是,一旦我拥有它,我一直在操作文件时遇到一些麻烦.

具体来说,模型字段save()方法需要File()对象作为第二个参数.所以我的数据最终必须是一个File()对象.下面链接的博客文章显示了如何使用urllib2将图像URL保存到File()对象中.这很好,但是,我还想使用PIL作为Image()对象来操作图像.(或ImageFile对象).

我首选的方法是将图像URL直接加载到Image()对象中,预先形成resize,然后将其转换为File()对象,然后将其保存在模型中.但是,我将Image()转换为File()的尝试失败了.如果可能的话,我想限制我写入磁盘的次数,所以我想在内存中使用这个对象转换或使用NamedTemporaryFile(delete = True)对象,所以我不必担心周围的额外文件.(当然,我希望通过模型保存文件后将文件写入磁盘).

import urllib2
from PIL import Image, ImageFile    
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

inStream = urllib2.urlopen('http://www.google.com/intl/en_ALL/images/srpr/logo1w.png')

parser = ImageFile.Parser()
while True:
    s = inStream.read(1024)
    if not s:
        break
    parser.feed(s)

inImage = parser.close()
# convert to RGB to avoid error with png and tiffs
if inImage.mode != "RGB":
    inImage = inImage.convert("RGB")

# resize could occur here

# START OF CODE THAT DOES NOT SEEM TO WORK
# I need to somehow convert an …
Run Code Online (Sandbox Code Playgroud)

python django file urllib2 python-imaging-library

10
推荐指数
1
解决办法
8857
查看次数

django:将url中的图像保存在另一个模型中

class Item(models.Model):
    name = models.CharField(max_length = 200)
    image = models.ImageField(upload_to = 'read', blank=True)
    creative_url = models.CharField(max_length = 200)
    description = RichTextField()

    def save(self, *args, **kwargs):
        content = urllib2.urlopen(self.creative_url).read()
        self.image.save("test.jpg", File(content))
        super(Item, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

给出异常:'str'对象没有属性'name'

我试图按照这个答案(http://stackoverflow.com/questions/1393202/django-add-image-in-an-imagefield-from-image-url)但它没有帮助摆脱异常.


AttributeError at /admin/collection/item/1/ 'str' object has no attribute 'name' Request Method:    POST Request
URL:    http://127.0.0.1:8000/admin/collection/item/1/ Django
Version:    1.2.5 Exception Type:   AttributeError Exception Value:  'str'
object has no attribute 'name' Exception
Location:   D:\FF\django\core\files\base.py in _get_size, line 39 Python
Executable: C:\Python27\python.exe Python Version:  2.7.2 Python
Path:   ['D:\\FF', …
Run Code Online (Sandbox Code Playgroud)

python django

6
推荐指数
2
解决办法
4760
查看次数

有没有办法将数据直接从 python 请求流到 minio 存储桶

我正在尝试向服务器发出 GET 请求以检索 tiff 图像。然后我想使用 MinIO python SDK 中的 put_object 方法将它直接流式传输到 MinIO。

我知道我可以通过将图像保存到临时文件然后上传来做到这一点,但我想看看是否可以跳过这一步。

我试过直接插入字节响应并使用 BytesIO 来包装它,但我想我错过了一些东西。

r = requests.get(url_to_download, stream=True)
Minio_client.put_object("bucket_name", "stream_test.tiff", r.content, r.headers['Content-length'])
Run Code Online (Sandbox Code Playgroud)

我得到以下错误

AttributeError: 'bytes' 对象没有属性 'read'

任何帮助深表感谢!

python stream python-3.x python-requests minio

5
推荐指数
1
解决办法
4301
查看次数

以编程方式将文件添加到Django ImageField

我正在尝试从URL下载图像文件,然后将该图像分配给Django ImageField.我按照这里的例子和[这里](

我的模型在相关部分看起来像这样:

class Entity(models.Model):

     logo = models.ImageField(upload_to=_getLogoPath,null=True)
Run Code Online (Sandbox Code Playgroud)

_getLogoPath回调非常简单:

def _getLogoPath(self,filename):
    path = "logos/" + self.full_name
    return path
Run Code Online (Sandbox Code Playgroud)

获取和保存图像文件的代码也很简单,作为我计划作为定期计划的cron作业运行的自定义django-admin命令的一部分:

...
img_url = "http://path.to.file/img.jpg"
img = urllib2.urlopen(img)
entity.logo.save(img_filename,img,True)
...
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到此错误:

AttributeError: addinfourl instance has no attribute 'chunks'
Run Code Online (Sandbox Code Playgroud)

我也尝试添加read()到图像但导致类似的错误.我也尝试将图像写入临时文件,然后尝试上传,但我得到了同样的错误.

python django urllib2

4
推荐指数
1
解决办法
5166
查看次数