3

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

谢谢

4

4 回答 4

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',) 
于 2009-10-04T19:25:21.397 回答
5

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

引用文档:

默认情况下,图像在裁剪之前居中。要从边缘裁剪,请传递一个逗号分隔的字符串,其中包含偏移量xy 百分比偏移量(负值从右侧/底部开始)。一些例子如下:

  • crop="0,0"将从左侧和顶部边缘裁剪。

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

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

图像也可以通过使用“智能裁剪” crop="smart"。通过从熵最小的边缘移除切片,图像被逐步裁剪到所需的大小。

于 2009-10-04T22:39:01.663 回答
3

这个问题很老,但是,由于它在搜索 django smart crop 时作为第一个结果出现在 Google 中,所以我想添加我的小颗粒。

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

https://github.com/francescortiz/image

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

于 2012-05-06T10:05:07.060 回答
1

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

于 2012-12-13T19:17:50.253 回答