Python / Django从URL下载图像,修改并保存到ImageField [英] Python/Django download Image from URL, modify, and save to ImageField

查看:227
本文介绍了Python / Django从URL下载图像,修改并保存到ImageField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种从URL下载图像的方法,对其进行一些图像操作(调整大小)操作,然后将其保存到django ImageField。使用两个伟大的帖子(下面链接),我已经能够下载并将图像保存到ImageField。但是,一旦我拥有这个文件,我一直在操作一些麻烦。具体来说,模型字段save()方法需要一个File()对象作为第二个参数。



所以我的数据最终必须是一个File()对象。以下链接的博文显示如何使用urllib2将图像URL保存到File()对象中。这很棒,但是,我也想使用PIL作为Image()对象来操作图像。 (或ImageFile对象)。



我最喜欢的方法是将图像URL直接加载到Image()对象中,进行调整大小,并将其转换为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)
如果不是s:
break
parser.feed $ s

inImage = parser.close()
#转换为RGB以避免png和tiff错误
如果inImage.mode!=RGB:
inImage = inImage.convert(RGB)

#resize可能发生在这里

#开始的代码不要SEEM工作
#我需要以某种方式转换一个图像.....

img_temp = NamedTemporaryFile(delete = True)
img_temp.write(inImage.tostring())
img_temp.flush )

file_object = File(img_temp)

#....进入Django对象将接受的文件。
#没有SEEM工作的代码结束

my_model_instance.image.save(
'some_filename',
file_object,#这必须是一个File()对象
save = True,

使用这种方法,当我将其视为图像时,会损坏。有没有人有任何方法从URL获取文件文件,允许将其作为图像操作,然后将其保存到Django ImageField?



任何帮助都是赞赏。



以编程方式将图像保存到Django ImageField



Django:从image url中添加ImageField中的图像



更新08/11/2010:我最终用StringIO,但是,当我尝试将其保存在Django ImageField中时,我被stringIO抛出异常异常。具体来说,堆栈跟踪显示名称错误:

 AttribueError exceptionStringIO实例没有属性'name'

在挖掘Django源之后,看起来这个错误是在模型保存尝试访问size属性时引起的的StringIO文件(虽然上面的错误表示这个名称的问题,但是这个错误的根本原因似乎是在StringIO映像上缺少一个size属性)。一旦我分配了一个值到大小属性的图像文件,它工作正常。

解决方案

试图用1块石头杀死2只鸟,为什么不使用(c)StringIO对象而不是NamedTemporaryFile?您不必将其存储在磁盘上,我知道这样的事情可以工作(我自己使用相似的代码)。


$ b来自cStringIO的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $))))))))))))))))))))))))
img_temp.seek(0)

file_object =文件(img_temp,filename)


I've been looking for a way to download an image from a URL, preform some image manipulations (resize) actions on it, and then save it to a django ImageField. Using the two great posts (linked below), I have been able to download and save an image to an ImageField. However, I've been having some trouble manipulating the file once I have it.

Specifically, the model field save() method requires a File() object as the second parameter. So my data has to eventually be a File() object. The blog posts linked below show how to use urllib2 to save your an image URL into a File() object. This is great, however, I also want to manipulate the image using PIL as an Image() object. (or ImageFile object).

My preferred approach would be then to load the image URL directly into an Image() object, preform the resize, and convert it to a File() object and then save it in the model. However, my attempts to convert an Image() to a File() have failed. If at all possible, I want to limit the number of times I write to the disk, so I'd like to do this object transformation in Memory or using a NamedTemporaryFile(delete=True) object so I don't have to worry about extra files laying around. (Of course, I want the file to be written to disk once it is saved via the model).

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 image .....

img_temp = NamedTemporaryFile(delete=True)
img_temp.write(inImage.tostring())
img_temp.flush()

file_object = File(img_temp)

# .... into a file that the Django object will accept. 
# END OF CODE THAT DOES NOT SEEM TO WORK

my_model_instance.image.save(
         'some_filename',
         file_object,  # this must be a File() object
         save=True,
         )

With this approach, the file appears corrupt whenever I view it as an image. Does anyone have any approach that takes a file file from a URL, allows one to manipulate it as an Image and then save it to a Django ImageField?

Any help is much appreciated.

Programmatically saving image to Django ImageField

Django: add image in an ImageField from image url

Update 08/11/2010: I did end up going with StringIO, however, I was stringIO was throwing an unusual Exception when I tried to save it in a Django ImageField. Specifically, the stack trace showed a name error:

"AttribueError exception "StringIO instance has no attribute 'name'"

After digging through the Django source, it looks like this error was caused when the model save tries to access the size attribute of the StringIO "File". (Though the error above indicates a problem with the name, the root cause of this error appears to be the lack of a size property on the StringIO image). As soon as I assigned a value to the size attribute of the image file, it worked fine.

解决方案

In an attempt to kill 2 birds with 1 stone. Why not use a (c)StringIO object instead of a NamedTemporaryFile? You won't have to store it on disk anymore and I know for a fact that something like this works (I use similar code myself).

from cStringIO import StringIO
img_temp = StringIO()
inImage.save(img_temp, 'PNG')
img_temp.seek(0)

file_object = File(img_temp, filename)

这篇关于Python / Django从URL下载图像,修改并保存到ImageField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆