这是一个非常基本的问题,我确信我错过了 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/