python - 将 PNG 图像裁剪为其最小尺寸

标签 python image png

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

注意:边框大小不是固定值,可能因图片而异。

最佳答案

PILgetbbox为我工作

im.getbbox() => 4-tuple or 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.

我试过的代码示例,我已经用 bmp 测试过,但它也应该适用于 png。

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")

关于python - 将 PNG 图像裁剪为其最小尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1905421/

相关文章:

python - Pandas 条件格式不显示背景颜色。 (没有错误)

python - 使用azure作为Django的存储后端(使用django-storages)

java - 将 Image 对象转换为 FormFile 对象

java - 将图像从android上传到java servlet并保存

language-agnostic - 在 PNG 中编码 96 DPI 的首选值

python - Pytest-html:根据标记自定义报告标题

python - 在 Django 模板中创建年份下拉列表

python - 给定一个二维 numpy 实数数组,如何生成描述每个数字强度的图像?

python - 使用 PIL 将 RGBA PNG 转换为 RGB

jquery - 如何解决/破解 IE8 中半透明 PNG 褪色问题?