Django, sorl-thumbnail crop picture head

前端 未结 4 1030
萌比男神i
萌比男神i 2020-12-30 18:40

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 hea

相关标签:
4条回答
  • 2020-12-30 19:12

    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.

    0 讨论(0)
  • 2020-12-30 19:14

    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',) 
    
    0 讨论(0)
  • 2020-12-30 19:24

    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.

    0 讨论(0)
  • 2020-12-30 19:31

    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.

    0 讨论(0)
提交回复
热议问题