Trouble using python PIL library to crop and save image

前端 未结 2 592
攒了一身酷
攒了一身酷 2020-12-23 16:49

Im attempting to crop a pretty high res image and save the result to make sure its completed. However I keep getting the following error regardless of how I use the save me

2条回答
  •  温柔的废话
    2020-12-23 17:16

    Try this:

    it's a simple code to crop an image, and it works like a charm ;)

    import Image
    
    def crop_image(input_image, output_image, start_x, start_y, width, height):
        """Pass input name image, output name image, x coordinate to start croping, y coordinate to start croping, width to crop, height to crop """
        input_img = Image.open(input_image)
        box = (start_x, start_y, start_x + width, start_y + height)
        output_img = input_img.crop(box)
        output_img.save(output_image +".png")
    
    def main():
        crop_image("Input.png","output", 0, 0, 1280, 399)
    
    if __name__ == '__main__': main()
    

    In this case the Input image is 1280 x 800 px and the croped is 1280 x 399px starting at the top left corner.

提交回复
热议问题