图像大小与django? [英] Image resizing with django?

查看:138
本文介绍了图像大小与django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django(和Python)的新手,我一直在尝试自己制作一些东西,然后再跳入使用其他人的应用程序。在Django(或Python)的做事方式中,我无法理解哪些东西很合适。我想要解决的是如何调整图像的大小,一旦它被上传。我的模型设置很好,插入管理员,图像上传到目录:

I'm new to Django (and Python) and I have been trying to work out a few things myself, before jumping into using other people's apps. I'm having trouble understanding where things 'fit' in the Django (or Python's) way of doing things. What I'm trying to work out is how to resize an image, once it's been uploaded. I have my model setup nicely and plugged into the admin, and the image uploads fine to the directory:

from django.db import models

# This is to list all the countries
# For starters though, this will be just United Kingdom (GB)
class Country(models.Model):
    name = models.CharField(max_length=120, help_text="Full name of country")
    code = models.CharField(max_length=2, help_text="This is the ISO 3166 2-letter country code (see: http://www.theodora.com/country_digraphs.html)")
    flag = models.ImageField(upload_to="images/uploaded/country/", max_length=150, help_text="The flag image of the country.", blank=True)

    class Meta:
        verbose_name_plural = "Countries"

    def __unicode__(self):
        return self.name

我现在遇到的问题是拿起该文件并将新文件放入缩略图。就像我说的,我想知道如何做,而不使用他人的应用程序(现在)。我从DjangoSnippets得到这个代码:

The thing I'm now having trouble with is taking that file and making a new file into a thumbnail. Like I say, I'd like to know how to do it without using others' apps (for now). I have got this code from DjangoSnippets:

from PIL import Image
import os.path
import StringIO

def thumbnail(filename, size=(50, 50), output_filename=None):
    image = Image.open(filename)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = image.resize(size, Image.ANTIALIAS)

    # get the thumbnail data in memory.
    if not output_filename:
        output_filename = get_default_thumbnail_filename(filename)
    image.save(output_filename, image.format) 
    return output_filename

def thumbnail_string(buf, size=(50, 50)):
    f = StringIO.StringIO(buf)
    image = Image.open(f)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = image.resize(size, Image.ANTIALIAS)
    o = StringIO.StringIO()
    image.save(o, "JPEG")
    return o.getvalue()

def get_default_thumbnail_filename(filename):
    path, ext = os.path.splitext(filename)
    return path + '.thumb.jpg'

...但这最终困惑了我..由于我不知道这个适合到我的Django应用程序?真的,只是简单地制作已成功上传图片的缩略图,是最好的解决方案吗?任何人都有可能告诉我一个好的,坚实的,体面的方式,像我这样的初学者可以学会如何正确地做到这一点?像在那里,知道在哪里放这种代码(models.py?forms.py?...)以及它在上下文中如何工作? ...我只需要一点帮助,了解并解决这个问题。

...but this has ultimately confused me... As I don't know how this 'fits in' to my Django app? And really, is it the best solution for simply making a thumbnail of an image that has been successfully uploaded? Can anyone possibly show me a good, solid, decent way that a beginner like me can learn to do this properly? As in, knowing where to put that sort of code (models.py? forms.py? ...) and how it would work in context? ... I just need a bit of help understanding and working this problem out.

谢谢!

推荐答案

如果可以,你有一个Django应用程序准备好,完全按照你想要的方式执行:
https://github.com/sorl/sorl-thumbnail

If it's OK for you, there is a Django application ready, doing exactly what you want: https://github.com/sorl/sorl-thumbnail

这篇关于图像大小与django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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