图像处理:利用python读取图片并裁剪得到任意尺寸的图片(以中心为原点)

上一篇可以看到图片大小是1920*1080*3,现在假如剪切成1000*1000*3的图片,当然也可以读取灰度图。

from skimage import io  
picture = io.imread("C:/Users/huyuan/Pictures/Camera Roll/1.jpg")# 图片路径  
io.imshow(picture)

"""
中心裁剪任意尺寸的图片(以中心为原点)
"""
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))
        # 图片宽度应大于想要裁剪后的宽度512
elif slice_height < 1000:
    raise Exception(
        "The height of image must be at least {} pixels!".format(SLICE_HEIGHT))
        # 图片高度应大于想要裁剪后的高度512
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)

结果:剪切后的图片



猜你喜欢

转载自blog.csdn.net/forever0_0love/article/details/80085738