如何在PIL中缩放和居中裁剪图像

时间:2019-07-14 17:05:03

标签: python python-3.x python-imaging-library

我想使用PIL库缩放和裁剪图像。例如:我有3位用户的图片:

  • 500x500px
  • 1000x6123px
  • 1920x1080px

我应该只将图像保持在500x500px的分辨率,因此我想对这些图像进行中心裁剪并将其设置为500x500px。我该怎么办?

我尝试了堆栈溢出的一些解决方案:

# This works fine but it just crops a 500x500px square from the image
img.crop((width//2 - 500//2, height//2 - 500//2, width//2 + 500//2, height//2 + 500//2))

# Maybe i did something wrong but nearly everything is black (pic 1)
img.thumbnail([500, 500])
img.crop((width//2 - 500//2, height//2 - 500//2, width//2 + 500//2, height//2 + 500//2))

pic 1

0 个答案:

没有答案