Python中用PIL/Pillow裁剪圖片

若是系統中沒有安裝Pillow包,安裝:sublime-text

lena

lena2

從右下角開始(200*200)ide

從中心開始(100*150)spa

half_the_width = img.size[0] / 2
half_the_height = img.size[1] / 2
img4 = img.crop(
    (
        half_the_width - 50,
        half_the_height - 75,
        half_the_width + 50,
        half_the_height + 75
    )
)
img4.save("lena4.jpg")

 

lena4

把圖片填充爲正方形code

longer_side = max(img4.size)
horizontal_padding = (longer_side - img4.size[0]) / 2
vertical_padding = (longer_side - img4.size[1]) / 2
img5 = img4.crop(
    (
        -horizontal_padding,
        -vertical_padding,
        img4.size[0] + horizontal_padding,
        img4.size[1] + vertical_padding
    )
)
img5.save("lena5.jpg")

lena5

相關文章
相關標籤/搜索