Skip to content
Advertisement

How to crop corners of a 1000×1000 picture

I’m trying to crop a picture to 1000 by 1000 pixels. It works to crop the far left of the picture.

img = Image.open('image.jpg')
(width, height) = img.size
if width >= height:
    multiplier = 1000 / height
    height = int(height * multiplier + 1)
    width = int(width * multiplier + 1)
    img = img.resize((width, height))
    cropped_img = img.crop((0,0, 1000,1000))
    cropped_img.save("background_image.png")
if height >= width:
    multiplier = 1000 / width
    width = int(width * multiplier + 1)
    height = int(height * multiplier + 1)
    img = img.resize((height, width))
    cropped_img = img.crop((0,0, 1000,1000))
    cropped_img.save("background_image.png")

However, if I attempt to change the crop to the far right it returns an error:

SystemError: tile cannot extend outside image

img = Image.open('image.jpg')
(width, height) = img.size
if width >= height:
    multiplier = 1000 / height
    height = int(height * multiplier + 1)
    width = int(width * multiplier + 1)
    img = img.resize((width, height))
    cropped_img = img.crop((1000,1000, 0, 0))
    cropped_img.save("background_image.png")
if height >= width:
    multiplier = 1000 / width
    width = int(width * multiplier + 1)
    height = int(height * multiplier + 1)
    img = img.resize((height, width))
    cropped_img = img.crop((1000,1000, 0, 0))
    cropped_img.save("background_image.png")

Advertisement

Answer

As the comments and the documentation pointed out, parameters should be supplied in the form of (left, upper, right, lower).

In your case, this translates to (width - 1000, height - 1000, width, height)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement