In [1]:
from PIL import Image
In [ ]:
 
In [19]:
img = Image.open("./squad-bezel-gunbound.png")
img.size
Out[19]:
(500, 500)
In [20]:
img
Out[20]:
In [21]:
out = autocrop_image(img)
bbox (190, 189, 311, 311)
size (121, 122)
In [17]:
out
Out[17]:
In [18]:
# https://gist.github.com/odyniec/3470977
def autocrop_image(image, border = 0):
    # Get the bounding box
    bbox = image.getbbox()
    print("bbox", bbox)

    # Crop the image to the contents of the bounding box
    image = image.crop(bbox)

    # Determine the width and height of the cropped image
    (width, height) = image.size
    print("size", image.size)

    # Add border
    width += border * 2
    height += border * 2
    
    # Create a new image object for the output image
    cropped_image = Image.new("RGBA", (width, height), (0,0,0,0))

    # Paste the cropped image onto the new image
    cropped_image.paste(image, (border, border))

    # Done!
    return cropped_image
In [ ]:
 
In [23]:
img = Image.open("output.png")
img
Out[23]:
In [28]:
crop(img)
output.png: 288x385 -> 239x342
Out[28]:
In [27]:
# https://github.com/crazyhitty/crop-transparent-image/blob/master/crop.py
def readable_resolution(size: tuple):
    return str(size[0]) + 'x' + str(size[1])

def is_pixel_alpha(pixel: tuple or int):
    pixel_value = pixel[3] if isinstance(pixel, tuple) else pixel
    return pixel_value == 0


def crop(image):
    #if not is_valid_image_file(image_path):
    #    raise ValueError(image_path, 'is not a valid png image.', 'Only a valid png file is accepted')

    #image = Image.open(image_path, 'r')
    width = image.size[0]
    height = image.size[1]
    pixels = image.load()

    top = 0
    bottom = 0
    left = 0
    right = 0

    for y in range(0, height):
        for x in range(0, width):
            if not is_pixel_alpha(pixels[x, y]):
                if left == 0 or x - 1 < left:
                    left = x - 1
                break

    for y in range(0, height):
        for x in range(0, width):
            if not is_pixel_alpha(pixels[x, y]):
                if top == 0 or y < top:
                    top = y
                break

    for y in reversed(range(0, height)):
        for x in reversed(range(0, width)):
            if not is_pixel_alpha(pixels[x, y]):
                if right == 0 or x + 1 > right:
                    right = x + 1
                break

    for y in reversed(range(0, height)):
        for x in reversed(range(0, width)):
            if not is_pixel_alpha(pixels[x, y]):
                if bottom == 0 or y + 1 > bottom:
                    bottom = y + 1
                break

    if left == -1:
        left = 0

    if top == -1:
        top = 0

    if right == 0:
        right = width

    if bottom == 0:
        bottom = height

    cropped_image = image.crop((left, top, right, bottom))
    print(image.filename + ': ' + readable_resolution(image.size) + ' -> ' + readable_resolution(cropped_image.size))
    #image.close()
    return cropped_image
In [ ]: