相关疑难解决方法(0)

将通过PIL创建的图像保存到django模型

我已使用以下代码成功创建并旋转了通过电子邮件上传到我服务器上的目录的图像:

      image = ContentFile(b64decode(part.get_payload()))
      im = Image.open(image)
      tempfile = im.rotate(90)
      tempfile.save("/srv/www/mysite.com/public_html/media/images/rotate.jpg", "JPEG")
      img = Photo(user=user)
      img.img.save('rotate.jpg', tempfile)
      img.save()
Run Code Online (Sandbox Code Playgroud)

旋转的图像存在于目录中,但是当我尝试将该图像添加到我的模型时,它不会保存.我错过了什么?任何帮助将不胜感激.

python django django-models python-imaging-library

12
推荐指数
1
解决办法
8676
查看次数

将用魔杖生成的图像保存到Django ImageField

我正在尝试为django模型中存储的“覆盖”配置生成预览,而不是稍后将其应用于其他模型。我对使用python操作文件没有太多经验... =(

这是我的代码:

import io
from django.conf import settings
from django.db import models
from wand.image import Image
from PIL.ImageFile import ImageFile, Parser, Image as PilImage

class Overlay(models.Model):
    RELATIVE_POSITIONS = (...)
    SIZE_MODES = (...)

    name = models.CharField(max_length=50)
    source = models.FileField(upload_to='overlays/%Y/%m/%d')
    sample = models.ImageField(upload_to='overlay_samples/%Y/%m/%d', blank=True)
    px = models.SmallIntegerField(default=0)
    py = models.SmallIntegerField(default=0)
    position = models.CharField(max_length=2, choices=RELATIVE_POSITIONS)
    width = models.SmallIntegerField(default=0)
    height = models.SmallIntegerField(default=0)
    size_mode = models.CharField(max_length=1, choices=SIZE_MODES, default='B')
    last_edit = models.DateTimeField(auto_now=True)

    def generate_sample(self):
        """
        Generates the sample image and saves it in the "sample" …
Run Code Online (Sandbox Code Playgroud)

python django imagemagick django-models wand

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

如何使用Django显示在后端生成的图像

我是django的新手,我对图像显示问题很困惑.现在我有一个在后端生成的词云的图像(比方说,topicWords.py),我不知道如何应对它.(1)如何将其存储在模型UserProfile的图像字段中?在models.py中,我有:

class UserProfile(models.Model):
user = models.OneToOneField(User)
tagcloud = models.ImageField(upload_to ='rap_song/raptle/pic/')
Run Code Online (Sandbox Code Playgroud)

直接做是这样的:

user.userprofile.tagcloud = wc #wc is the image generated
Run Code Online (Sandbox Code Playgroud)

(2)MEDIA_ROOT和MEDIA_URL的设置应该是什么?

(3)我应该如何在.html中显示它?

<img src = "???">
Run Code Online (Sandbox Code Playgroud)

非常感谢你!

python django python-imaging-library python-3.x web

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

Django:上传前调整图片大小

我想在上传之前调整图片大小(Pillow),我写下面的代码却不行! 并得到错误:

/ myapp/list /的AttributeError

_committed

请求方法:POST

请求URL:http://127.0.0.1:8000 / myapp/ list / Django版本:1.8异常类型:AttributeError异常值:

_committed

例外地点:

/usr/local/lib/python3.4/dist-packages/Pillow-2.8.1-py3.4-linux-x86_64.egg/PIL/Image.py

getattr中,第622行Python可执行文件:/usr/bin/python3.4 Python版本:3.4.0

views.py

def list(request):
# Handle file upload
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        imga = request.FILES['docfile']
        size = (600, 400)
        im = Image.open(imga)
        imga = im.resize(size)
        request.FILES['docfile'] = imga
        newdoc = Document(docfile = request.FILES['docfile'], namefile=request.POST['namefile'])
        newdoc.save()

        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
else:
    form = …
Run Code Online (Sandbox Code Playgroud)

python django python-imaging-library

3
推荐指数
2
解决办法
5852
查看次数

使用PIL时,“ JpegImageFile”对象没有属性“ _committed”错误

我正在使用PIL压缩上传的图片(FileField)。但是我遇到一个错误,我认为这是双重保存的问题?(保存我的图像,然后保存包括该图像的整个表单)。我想commit=False在保存图像时执行,但没有出现,这是可能的。这是我的代码:

...
if form_post.is_valid():
    instance = form_post.save(commit=False)
    instance.user = request.user

if instance.image:
    filename = instance.image
    instance.image = Image.open(instance.image)
    instance.image.thumbnail((220, 130), Image.ANTIALIAS)
    instance.image.save(filename, quality=60)

instance.save()
Run Code Online (Sandbox Code Playgroud)

'JpegImageFile' object has no attribute '_committed'在最后一行(instance.save())返回错误

有人可以找出问题所在吗?-知道我该如何解决吗?

完整回溯:

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/zorgan/Desktop/project/site/post/views.py" in post
  68. …
Run Code Online (Sandbox Code Playgroud)

python django python-imaging-library

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