How to crop an image in OpenCV using Python

后端 未结 9 2204
面向向阳花
面向向阳花 2020-11-22 08:45

How can I crop images, like I\'ve done before in PIL, using OpenCV.

Working example on PIL

im = Image.open(\'0.png\').convert(\'L\')
im = im.crop((1         


        
9条回答
  •  执念已碎
    2020-11-22 09:00

    Below is the way to crop an image.

    image_path: The path to the image to edit

    coords: A tuple of x/y coordinates (x1, y1, x2, y2)[open the image in mspaint and check the "ruler" in view tab to see the coordinates]

    saved_location: Path to save the cropped image

    from PIL import Image
        def crop(image_path, coords, saved_location:
            image_obj = Image.open("Path of the image to be cropped")
                cropped_image = image_obj.crop(coords)
                cropped_image.save(saved_location)
                cropped_image.show()
    
    
    if __name__ == '__main__':
        image = "image.jpg"
        crop(image, (100, 210, 710,380 ), 'cropped.jpg')
    

提交回复
热议问题