Given a django image field, how do I create a PIL image and vice-versa?
给定django图像字段,如何创建PIL图像,反之亦然?
Simple question, but hard to google :(
简单的问题,但很难谷歌:(
(I'm going to use django-imagekit 's processor to rotate an image already stored as model attribute.)
(我将使用django-imagekit的处理器来旋转已经存储为模型属性的图像。)
edit
编辑
In [41]: m.image_1.__class__
Out[41]: django.db.models.fields.files.ImageFieldFile
In [42]: f = StringIO(m.image_1.read())
In [43]: Image.open(f)
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-43-39949b3b74b3> in <module>()
----> 1 Image.open(f)
/home/eugenekim/virtualenvs/zibann/local/lib/python2.7/site-packages/PIL/Image.pyc in open(fp, mode)
2023 pass
2024
-> 2025 raise IOError("cannot identify image file")
2026
2027 #
IOError: cannot identify image file
In [44]:
2 个解决方案
#1
14
The first question:
第一个问题:
import Image
pil_image_obj = Image.open(model_instance.image_field)
The second question:
第二个问题:
from cStringIO import StringIO
from django.core.files.base import ContentFile
f = StringIO()
try:
pil_image_obj.save(f, format='png')
s = f.getvalue()
model_instance.image_field.save(model_instance.image_field.name,
ContentFile(s))
#model_instance.save()
finally:
f.close()
UPDATE
UPDATE
According to OP's comment, replacing import Image
with from PIL import Image
solved his problem.
根据OP的评论,用PIL import Image替换import Image解决了他的问题。
#2
18
To go from PIL image to Django ImageField, I used falsetru's answer, but I had to update it for Python 3.
要从PIL图像转到Django ImageField,我使用了falsetru的答案,但我不得不为Python 3更新它。
First, StringIO has been replaced by io as per: StringIO in python3
首先,StringIO已经被io替换为python3中的StringIO
Second, When I tried io.StringIO()
, I recieved an error saying: "*** TypeError: string argument expected, got 'bytes'"
. So I changed it to io.BytesIO()
and it all worked.
第二,当我尝试io.StringIO()时,我收到一条错误说:“*** TypeError:字符串参数预期,得到'字节'”。所以我把它改成了io.BytesIO(),这一切都奏效了。
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
f = BytesIO()
try:
pil_image_obj.save(f, format='png')
model_instance.image_field.save(model_instance.image_field.name,
ContentFile(f.getvalue()))
#model_instance.save()
finally:
f.close()
#1
14
The first question:
第一个问题:
import Image
pil_image_obj = Image.open(model_instance.image_field)
The second question:
第二个问题:
from cStringIO import StringIO
from django.core.files.base import ContentFile
f = StringIO()
try:
pil_image_obj.save(f, format='png')
s = f.getvalue()
model_instance.image_field.save(model_instance.image_field.name,
ContentFile(s))
#model_instance.save()
finally:
f.close()
UPDATE
UPDATE
According to OP's comment, replacing import Image
with from PIL import Image
solved his problem.
根据OP的评论,用PIL import Image替换import Image解决了他的问题。
#2
18
To go from PIL image to Django ImageField, I used falsetru's answer, but I had to update it for Python 3.
要从PIL图像转到Django ImageField,我使用了falsetru的答案,但我不得不为Python 3更新它。
First, StringIO has been replaced by io as per: StringIO in python3
首先,StringIO已经被io替换为python3中的StringIO
Second, When I tried io.StringIO()
, I recieved an error saying: "*** TypeError: string argument expected, got 'bytes'"
. So I changed it to io.BytesIO()
and it all worked.
第二,当我尝试io.StringIO()时,我收到一条错误说:“*** TypeError:字符串参数预期,得到'字节'”。所以我把它改成了io.BytesIO(),这一切都奏效了。
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
f = BytesIO()
try:
pil_image_obj.save(f, format='png')
model_instance.image_field.save(model_instance.image_field.name,
ContentFile(f.getvalue()))
#model_instance.save()
finally:
f.close()