将PNG图像裁剪为最小尺寸 [英] Crop a PNG image to its minimum size

查看:264
本文介绍了将PNG图像裁剪为最小尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何切断PNG图像的空白边框区域并使用Python将其缩小到最小尺寸?

How to cut off the blank border area of a PNG image and shrink it to its minimum size using Python?

推荐答案

PIL getbbox 正在为我工​​作


im.getbbox() => 4元组或无

im.getbbox() => 4-tuple or None

计算图像中
非零区域的边界框。
边界框作为4元组
返回,定义左,上,右和
下像素坐标。如果图像
完全为空,则此方法
返回None。

Calculates the bounding box of the non-zero regions in the image. The bounding box is returned as a 4-tuple defining the left, upper, right, and lower pixel coordinate. If the image is completely empty, this method returns None.

我试过的代码示例,I已经使用bmp进行了测试,但它也适用于png。

Code Sample that I tried, I have tested with bmp, but it should work for png too.

>>> import Image
>>> im=Image.open("test.bmp")
>>> im.size
(364, 471)
>>> im.getbbox()
(64, 89, 278, 267)
>>> im2=im.crop(im.getbbox())
>>> im2.size
(214, 178)
>>> im2.save("test2.bmp")

这篇关于将PNG图像裁剪为最小尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆