如何裁剪 1000x1000 图片的边角
How to crop corners of a 1000x1000 picture
我正在尝试将图片裁剪为 1000 x 1000 像素。它适用于裁剪图片的最左侧。
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")
但是,如果我尝试将裁剪更改为最右侧,它 returns 会出错:
系统错误:图块无法扩展到图像外
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")
正如评论和 documentation 指出的那样,参数应以 (left, upper, right, lower)
.
的形式提供
在您的情况下,这转换为 (width - 1000, height - 1000, width, height)
我正在尝试将图片裁剪为 1000 x 1000 像素。它适用于裁剪图片的最左侧。
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")
但是,如果我尝试将裁剪更改为最右侧,它 returns 会出错:
系统错误:图块无法扩展到图像外
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")
正如评论和 documentation 指出的那样,参数应以 (left, upper, right, lower)
.
在您的情况下,这转换为 (width - 1000, height - 1000, width, height)