python - 使用 Python 和 Pillow 返回图像的 "chunk"

标签 python image python-imaging-library

这是一个非常基本的问题,我确信我错过了 Pillow 库/文档的某些部分......

假设您有一个 128x128 的图像,并且您想要保存其中的“ block ”,即从原始图像左上角开始的“x”个像素,从左上角开始向下的“y”个像素原始图像的一角(因此该“ block ”的左上角位于(x,y)。如果您知道所需的 block 是“a”像素宽,“b”像素高(因此四个角你想要的 block 是已知的,它们是 (x,y),(x+a,y),(x,y+b),(x+a,y+b)) - 你会如何保存这个您作为单独的图像文件给出的原始图像的“ block ”?

更简洁地说,如何使用 PIL 保存给定像素坐标的图像片段?任何帮助/指示表示赞赏。

最佳答案

想出:

"""
The function "crop" takes in large_img, small_img, x, y, w, h and returns the image lying within these restraints:
large_img: the filename of the large image
small_img: the desired filename of the smaller "sub-image"
x: x coordinate of the upper left corner of the bounding box
y: y coordinate of the upper left corner of the bounding box
w: width of the bounding box
h: height of the bounding box
"""
def crop(large_img, small_img, x, y, w, h):
    img = Image.open(large_img)
    box = (x, y, x+w, y+h)
    area = img.crop(box)
    area.save(small_img, 'jpeg')

关于python - 使用 Python 和 Pillow 返回图像的 "chunk",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44273936/

相关文章:

javascript - 从 MongoDB 在 Angular.js 中显示图像

javascript - jquery 更改特定 div 内的图像边距

python - 使用 Pillow 将 EPS 图像转换为 JPG

python - 在 Mac OS X 上安装 PIL 以与 Django 一起使用

python - Pandas stack/groupby 制作一个新的数据框

c++ - boost 随机样本,如 python random.sample

python - 具有正确标题顺序的数据透视表语法

image - Swift2 从 Firebase 检索图像

python - 在 tkinter 中裁剪图像

python - 过滤/排除字典中的值列表(Python 3)