(1)批量裁剪图片到指定像素

题------记录博客只是为了方便自己以后学习,如果什么地方有误,还请指正。

1、目的:

  • 在深度学习中,有时收集到的图片像素尺寸大小不一,需要将图片预处理到指定的像素尺寸。代码是将批量的图片裁剪到指定的像素尺寸。以500x500像素图片为例,裁剪结果为192x256。

2、代码:

import cv2
import imutils
import os

def crop_image(image,target_width,target_height):
    (h, w) = image.shape[:2]
    dH =0
    dW=0
    if w < h:
        image = imutils.resize(image, width=target_width,
        inter=cv2.INTER_AREA)
        dH = int((image.shape[0] - target_height) / 2.0)
    else:
        image = imutils.resize(image, height=target_height,
         inter=cv2.INTER_AREA)
        dW = int((image.shape[1] - target_width) / 2.0)
    (h, w) = image.shape[:2]
    image = image[dH:h - dH, dW:w - dW]
    return cv2.resize(image, (target_width, target_height),interpolation=cv2.INTER_AREA)

if __name__ =="__main__":
    image_paths = "C:/Users/Desktop/images/"   #图片文件夹路径,路径不能出现中文
    filelist = os.listdir(image_paths)
    for filename in filelist:
            image = cv2.imread(image_paths+'/'+filename)
            img = crop_image(image, 192, 256)    #可在此处修改宽,高
            cv2.imwrite(filename,img)

3、结果对比

  • 像素500x500像素500*500
  • 像素192x256像素192*256

参考博客:https://blog.csdn.net/y459541195/article/details/100687966.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值