Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django中从同一图像生成两个缩略图_Python_Django_Python Imaging Library_Thumbnails - Fatal编程技术网

Python 在Django中从同一图像生成两个缩略图

Python 在Django中从同一图像生成两个缩略图,python,django,python-imaging-library,thumbnails,Python,Django,Python Imaging Library,Thumbnails,这似乎是一个很容易的问题,但我不知道这里发生了什么。 基本上,我想做的是从Django模型上的一个图像创建两个不同的缩略图。最终发生的情况是,它似乎在循环并重新创建同一个图像(同时每次都在图像上添加下划线),直到抛出文件名太大的错误。所以,你最终会得到这样的结果: OSError: [Errno 36] File name too long: 'someimg________________etc.jpg' 以下是代码(保存方法在艺术家模型上): 为了简洁起见,我没有显示导入。假设艺术家模型

这似乎是一个很容易的问题,但我不知道这里发生了什么。 基本上,我想做的是从Django模型上的一个图像创建两个不同的缩略图。最终发生的情况是,它似乎在循环并重新创建同一个图像(同时每次都在图像上添加下划线),直到抛出文件名太大的错误。所以,你最终会得到这样的结果:

OSError: [Errno 36] File name too long: 'someimg________________etc.jpg'
以下是代码(保存方法在艺术家模型上):


为了简洁起见,我没有显示导入。假设艺术家模型上有两个图像字段:thumb_large和thumb_small

我测试这是否有效的方法是,在shell中:

artist = Artist.objects.get(id=1)
artist.save() 
#error here after a little wait (until I assume it generates enough images that the OSError gets raised)

如果这不是正确的方法,我将感谢任何反馈。谢谢

一般来说,我喜欢尽可能多地为模板作者提供缩略图功能。这样他们可以调整模板中内容的大小。而将其构建到业务逻辑层则更为固定。不过你可能有理由

此模板筛选器应在第一次加载时生成文件,然后在将来的加载中加载文件。这是很久以前从某个博客上借来的,不过我想我添加了中心裁剪功能。很可能还有其他更具特色的产品

{% load thumbnailer %}
...
<img src="{{someimage|thumbnail_crop:'200x200'}}" />
{%load thumbnailer%}
...
文件appname/templatetags/thumbnaller.py

import os
import Image
from django.template import Library

register.filter(thumbnail)
from settings import MEDIA_ROOT, MEDIA_URL

def thumbnail_crop(file, size='104x104', noimage=''):
    # defining the size
    x, y = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    try:
        filehead, filetail = os.path.split(file.path)
    except:
        return '' # '/media/img/noimage.jpg'

    basename, format = os.path.splitext(filetail)
    #quick fix for format
    if format.lower() =='.gif':
        return (filehead + '/' + filetail).replace(MEDIA_ROOT, MEDIA_URL)

    miniature = basename + '_' + size + format
    filename = file.path
    miniature_filename = os.path.join(filehead, miniature)
    filehead, filetail = os.path.split(file.url)
    miniature_url = filehead + '/' + miniature
    if os.path.exists(miniature_filename) and os.path.getmtime(filename)>os.path.getmtime(miniature_filename):
        os.unlink(miniature_filename)
    # if the image wasn't already resized, resize it
    if not os.path.exists(miniature_filename):
        try:
            image = Image.open(filename)
        except:
            return noimage

        src_width, src_height = image.size
        src_ratio = float(src_width) / float(src_height)
        dst_width, dst_height = x, y
        dst_ratio = float(dst_width) / float(dst_height)

        if dst_ratio < src_ratio:
            crop_height = src_height
            crop_width = crop_height * dst_ratio
            x_offset = float(src_width - crop_width) / 2
            y_offset = 0
        else:
            crop_width = src_width
            crop_height = crop_width / dst_ratio
            x_offset = 0
            y_offset = float(src_height - crop_height) / 3
        image = image.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height)))
        image = image.resize((dst_width, dst_height), Image.ANTIALIAS)
        try:
            image.save(miniature_filename, image.format, quality=90, optimize=1)
        except:
            try:
                image.save(miniature_filename, image.format, quality=90)
            except:
                return '' #'/media/img/noimage.jpg'

    return miniature_url

register.filter(thumbnail_crop)
导入操作系统
导入图像
从django.template导入库
register.filter(缩略图)
从设置导入媒体\u根目录、媒体\u URL
def缩略图_裁剪(文件,大小为104x104',noimage=“”):
#定义大小
x、 y=[int(x)表示x的大小。拆分('x')]
#定义文件名和微型文件名
尝试:
filehead,filetail=os.path.split(file.path)
除:
返回'#'/media/img/noimage.jpg'
basename,format=os.path.splitext(filetail)
#快速修复格式
如果format.lower()='.gif':
返回(filehead+'/'+filetail)。替换(MEDIA\u ROOT,MEDIA\u URL)
微型=基本名称+''.'+大小+格式
filename=file.path
minimal\u filename=os.path.join(文件头,minimal)
filehead,filetail=os.path.split(file.url)
minimal_url=filehead+'/'+minimal
如果os.path.exists(minimal\u文件名)和os.path.getmtime(文件名)>os.path.getmtime(minimal\u文件名):
取消链接(微型文件名)
#如果图像尚未调整大小,请调整其大小
如果不存在os.path.exists(文件名):
尝试:
image=image.open(文件名)
除:
返回图像
src_宽度,src_高度=image.size
src_比率=浮动(src_宽度)/浮动(src_高度)
dst_宽度,dst_高度=x,y
dst_比率=浮动(dst_宽度)/浮动(dst_高度)
如果dst_比率
通常,我喜欢尽可能多地为模板作者提供缩略图功能。这样他们可以调整模板中内容的大小。而将其构建到业务逻辑层则更为固定。不过你可能有理由

此模板筛选器应在首次加载时生成文件,然后在将来加载时加载该文件。这是很久以前从某个博客上借来的,不过我想我添加了中心裁剪功能。很可能还有其他更具特色的产品

{% load thumbnailer %}
...
<img src="{{someimage|thumbnail_crop:'200x200'}}" />
{%load thumbnailer%}
...
文件appname/templatetags/thumbnaller.py

import os
import Image
from django.template import Library

register.filter(thumbnail)
from settings import MEDIA_ROOT, MEDIA_URL

def thumbnail_crop(file, size='104x104', noimage=''):
    # defining the size
    x, y = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    try:
        filehead, filetail = os.path.split(file.path)
    except:
        return '' # '/media/img/noimage.jpg'

    basename, format = os.path.splitext(filetail)
    #quick fix for format
    if format.lower() =='.gif':
        return (filehead + '/' + filetail).replace(MEDIA_ROOT, MEDIA_URL)

    miniature = basename + '_' + size + format
    filename = file.path
    miniature_filename = os.path.join(filehead, miniature)
    filehead, filetail = os.path.split(file.url)
    miniature_url = filehead + '/' + miniature
    if os.path.exists(miniature_filename) and os.path.getmtime(filename)>os.path.getmtime(miniature_filename):
        os.unlink(miniature_filename)
    # if the image wasn't already resized, resize it
    if not os.path.exists(miniature_filename):
        try:
            image = Image.open(filename)
        except:
            return noimage

        src_width, src_height = image.size
        src_ratio = float(src_width) / float(src_height)
        dst_width, dst_height = x, y
        dst_ratio = float(dst_width) / float(dst_height)

        if dst_ratio < src_ratio:
            crop_height = src_height
            crop_width = crop_height * dst_ratio
            x_offset = float(src_width - crop_width) / 2
            y_offset = 0
        else:
            crop_width = src_width
            crop_height = crop_width / dst_ratio
            x_offset = 0
            y_offset = float(src_height - crop_height) / 3
        image = image.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height)))
        image = image.resize((dst_width, dst_height), Image.ANTIALIAS)
        try:
            image.save(miniature_filename, image.format, quality=90, optimize=1)
        except:
            try:
                image.save(miniature_filename, image.format, quality=90)
            except:
                return '' #'/media/img/noimage.jpg'

    return miniature_url

register.filter(thumbnail_crop)
导入操作系统
导入图像
从django.template导入库
register.filter(缩略图)
从设置导入媒体\u根目录、媒体\u URL
def缩略图_裁剪(文件,大小为104x104',noimage=“”):
#定义大小
x、 y=[int(x)表示x的大小。拆分('x')]
#定义文件名和微型文件名
尝试:
filehead,filetail=os.path.split(file.path)
除:
返回'#'/media/img/noimage.jpg'
basename,format=os.path.splitext(filetail)
#快速修复格式
如果format.lower()='.gif':
返回(filehead+'/'+filetail)。替换(MEDIA\u ROOT,MEDIA\u URL)
微型=基本名称+''.'+大小+格式
filename=file.path
minimal\u filename=os.path.join(文件头,minimal)
filehead,filetail=os.path.split(file.url)
minimal_url=filehead+'/'+minimal
如果os.path.exists(minimal\u文件名)和os.path.getmtime(文件名)>os.path.getmtime(minimal\u文件名):
取消链接(微型文件名)
#如果图像尚未调整大小,请调整其大小
如果不存在os.path.exists(文件名):
尝试:
image=image.open(文件名)
除:
返回图像
src_宽度,src_高度=image.size
src_比率=浮动(src_宽度)/浮动(src_高度)
dst_宽度,dst_高度=x,y
dst_比率=浮动(dst_宽度)/浮动(dst_高度)
如果dst_比率