Django,sorl-thumbnail作物图片头

时间:2009-10-04 15:55:50

标签: django sorl-thumbnail

伙计们,我想知道sorl-thumbnail是否有从底部到顶部裁剪的任何选项...我有一个垃圾问题,在一些图片中,sorl-thumbnail正在裁剪图片中人物的头部。

由于

4 个答案:

答案 0 :(得分:5)

我不相信这已经内置到solr-thumbnails中了,但是这里是一个插件,我从reddit中抄下来完成了你所追求的东西。它并不完美,但它往往可以完成工作。它不会从底部到顶部进行裁剪,而是使用切片的熵来确定裁剪的最终结果。这对reddit版本略有改进,因为它处理纵向或横向图像。

import Image, ImageFile, math
#from ImageEnhance import Color
#import os, sys


def image_entropy(im):
    """From Reddit: Calculate the entropy of an image"""
    hist = im.histogram()
    hist_size = sum(hist)
    hist = [float(h) / hist_size for h in hist]
    return -sum([p * math.log(p, 2) for p in hist if p != 0])

def square_image(im, requested_size, opts):
    """From Reddit: if the image is taller than it is wide, square it off. determine
    which pieces to cut off based on the entropy pieces.

    This version is improved as it squares images that are wider than it is tall.
    """
    if 'autosquare' in opts:
        x,y = im.size

        # if the image is taller than it is wide:
        if y > x:
            while y > x:
                #slice 10px at a time until square
                slice_height = min(y - x, 10)

                bottom = im.crop((0, y - slice_height, x, y))
                top = im.crop((0, 0, x, slice_height))

                #remove the slice with the least entropy
                if image_entropy(bottom) < image_entropy(top):
                    im = im.crop((0, 0, x, y - slice_height))
                else:
                    im = im.crop((0, slice_height, x, y))

                x,y = im.size

        # If the image is wider than it is tall
        else:
            while y < x:
                #slice 10px at a time until square
                slice_width = min(x - y, 10)

                left = im.crop((0,0, y, slice_width))
                right = im.crop((0,y - slice_width, x, y))

                #remove the slice with the least entropy
                if image_entropy(left) < image_entropy(right):
                    im = im.crop((0, 0, x - slice_width, y))
                else:
                    im = im.crop((slice_width, 0, x, y))

                x,y = im.size

        im = im.resize(requested_size, resample=Image.ANTIALIAS)

    return im
square_image.valid_options = ('autosquare',) 

答案 1 :(得分:5)

我刚刚发布了一个新版本的sorl-thumbnail(3.2.5),其边缘智能裁剪的裁剪受到btol45答案的启发。

引用文档:

  

默认情况下,图像在裁剪之前居中。来自的作物   edge,传递包含xy的逗号分隔字符串   百分比偏移(负值从右/底部开始)。一些   示例如下:

     
      
  • crop="0,0"将从左边缘和上边缘裁剪。

  •   
  • crop="-10,-0"将从右边缘裁剪(偏移量为10%)和   底边。

  •   
  • crop=",0"将保持x轴的默认行为(水平   将图像居中)并从顶部边缘裁剪。

  •   
     

使用crop="smart"也可以“智能裁剪”图像。图片   通过删除切片以递增方式裁剪为请求的大小   从具有最小熵的边缘。

答案 2 :(得分:3)

这个问题已经很久了,但是,因为它似乎是Google搜索django智能裁剪时的第一个结果,所以我想加上我的小颗粒。

这个“crop = auto”功能被添加到了sorl,但后来又被删除了。因此,对于可能满足这种需求的其他人,您可以尝试:

https://github.com/francescortiz/image

允许您通过管理员设置图像的关注中心。

答案 3 :(得分:1)

虽然原始答案不再有效,但在最新版本的sorl中,您可以指定以空格分隔的x和y裁剪值。例如,crop =“center top”,将以X为中心,但保持在Y的顶部,这对于我的情况下的人物照片更好,但并不完美。

相关问题