Django, sorl-缩略图裁剪图片头像

发布于 2024-08-06 20:25:11 字数 103 浏览 7 评论 0原文

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

谢谢

guys, i would like to know if sorl-thumbnail have any option to crop from the bottom to the top... i have a litter problem, in some picture sorl-thumbnail is croping the head of the people in pictures.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雨成花 2024-08-13 20:25:11

我刚刚发布了 sorl-thumbnail (3.2.5) 的新版本,受 btol45 答案的启发,可以从边缘进行裁剪智能裁剪。

引用文档:

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

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

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

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

还可以使用 crop="smart" 对图像进行“智能裁剪”。图像
通过删除切片逐步裁剪至请求的大小
从熵最小的边缘开始。

I've just released a new version of sorl-thumbnail (3.2.5) with cropping from edge and smart cropping inspired by btol45's answer.

Quoting the docs:

By default, the image is centered before being cropped. To crop from the
edges, pass a comma separated string containing the x and y
percentage offsets (negative values go from the right/bottom). Some
examples follow:

  • crop="0,0" will crop from the left and top edges.

  • crop="-10,-0" will crop from the right edge (with a 10% offset) and
    the bottom edge.

  • crop=",0" will keep the default behavior for the x axis (horizontally
    centering the image) and crop from the top edge.

The image can also be "smart cropped" by using crop="smart". The image
is incrementally cropped down to the requested size by removing slices
from edges with the least entropy.

情话难免假 2024-08-13 20:25:11

我不相信这已内置到 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',) 

I don't believe this is built into solr-thumbnails yet, but here's a plugin I cribbed from reddit that accomplishes what you're after. It's not perfect, but it tends to get the job done. It doesn't crop from bottom to top, but rather uses the entropy of the slices to determine what end to crop from. It's a slight improvement on the reddit version as it handles either portrait or landscape images.

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',) 
摇划花蜜的午后 2024-08-13 20:25:11

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

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

https://github.com/francescortiz/image< /a>

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

This question is old, but, since it appears as the first result in Google when searching for django smart crop, I want to add my little grain.

This "crop=auto" feature got added to sorl, but later, it got removed again. So, for others who might come with this need, you can try:

https://github.com/francescortiz/image

which allows you to set the center of attention of an image through the admin.

居里长安 2024-08-13 20:25:11

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

While the original answer no longer works, in recent versions of sorl, you can specify space-separated x and y crop values. For example, crop="center top", will center in X but keep the top in Y, which was better for pictures of people in my case, but not perfect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文