Image processing: use python to read the picture and crop it to get a picture of any size (with the center as the origin)

In the previous article, you can see that the image size is 1920*1080*3. Now if you cut it into a 1000*1000*3 image, you can of course read the grayscale image.

from skimage import io  
picture = io.imread("C:/Users/huyuan/Pictures/Camera Roll/1.jpg")# Picture path  
io.imshow(picture)

"""
Center crop an image of any size (with the center as the origin)
"""
slice_width, slice_height, _ = img_data.shape
if slice_width < 1000:
    raise Exception(
        "The width of  image must be at least {} pixels!".format(SLICE_WIDTH))
        # The width of the image should be greater than the width 512 you want to crop
elif slice_height < 1000:
    raise Exception(
        "The height of image must be at least {} pixels!".format(SLICE_HEIGHT))
        # The height of the image should be greater than the height 512 you want to crop
width_crop = (slice_width - 1000) // 2
height_crop = (slice_height - 1000) // 2
if width_crop > 0:
    img_data = img_data[width_crop:-width_crop, :, :]
if height_crop > 0:
    img_data = img_data[:, height_crop:-height_crop,:]
io.imshow(img_data)

Result: cropped image



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324864179&siteId=291194637