“返回”;“块”;使用Python和Pillow实现图像的可视化

“返回”;“块”;使用Python和Pillow实现图像的可视化,python,image,python-imaging-library,pillow,Python,Image,Python Imaging Library,Pillow,这是一个非常基本的问题,我确信我错过了枕头库/文档的某些部分 假设你有一个128x128的图像,你想保存它的“块”,即从原始图像左上角右起“x”像素,从原始图像左上角向下“y”像素(因此这个“块”的左上角位于(x,y)。如果你知道你想要的块是“a”像素宽和“b”像素高(所以你想要的块的四个角是已知的,它们是(x,y),(x+a,y),(x,y+b),(x+a,y+b))-你如何将原始图像的这个“块”保存为一个单独的图像文件 更简单地说,如何使用PIL保存给定像素坐标的图像片段?欢迎提供任何帮助/

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

假设你有一个128x128的图像,你想保存它的“块”,即从原始图像左上角右起“x”像素,从原始图像左上角向下“y”像素(因此这个“块”的左上角位于(x,y)。如果你知道你想要的块是“a”像素宽和“b”像素高(所以你想要的块的四个角是已知的,它们是(x,y),(x+a,y),(x,y+b),(x+a,y+b))-你如何将原始图像的这个“块”保存为一个单独的图像文件

更简单地说,如何使用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')
那又如何呢?我假设,这个子映像在内部与任何其他映像相同,并且可以放入可用的IO函数中。(但我从未使用过这个库;我喜欢scikit映像,但我认为它的依赖性要大得多)