2014-01-13 38 views
1

我使用django-cumulus將我的媒體存儲在Rackspace雲中。django-cumulus:從django.db.models.ImageField檢索PIL.Image對象

我需要從ImageField檢索數據到PIL.Image。我需要它對這個圖像進行一些修改(裁剪,過濾器等),並將其保存到另一個積雲ImageField。

我試過這段代碼:

def field_to_image(field): 
    # field - cumulus-powered ImageField on some model 
    from StringIO import StringIO 
    from PIL import Image 
    r = field.read() # ERROR throws here! 
    image = Image.open(StringIO(r)) 
    return image 

它的工作對我的檔案一半好,但對另一半我總是收到此錯誤:

Traceback (most recent call last): 
File "tmp.py", line 78, in <module> 
    resize_photos(start) 
File "tmp.py", line 59, in resize_photos 
    photo.make_thumbs() 
File "/hosting/site/news/models.py", line 65, in make_thumbs 
    i = functions.field_to_image(self.img) 
File "/hosting/site/functions.py", line 169, in field_to_image 
    r = field.read() 
File "/usr/local/lib/python2.7/dist-packages/cumulus/storage.py", line 352, in read 
    if self._pos == self._get_size() or chunk_size == 0: 
File "/usr/local/lib/python2.7/dist-packages/cumulus/storage.py", line 322, in _get_size 
    self._size = self._storage.size(self.name) 
File "/usr/local/lib/python2.7/dist-packages/cumulus/storage.py", line 244, in size 
    return self._get_object(name).total_bytes 
AttributeError: 'bool' object has no attribute 'total_bytes' 

誰能幫助我?也許有更好的方法從rackspace檢索PIL.Image對象?

的文件,我想讀()存在並且可透過網址上Rackspace公司

+0

它已經返回一個'django.core.files.images.ImageFile'對象。你想做什麼? – codingjoe

回答