图片等比例缩放固定大小,并补全

代码说明:1、将图像按较长的一边进行等比例缩放,2、固定大小输出并补全。

	for imagen in [imagen for imagen in os.listdir(image_path) if os.path.isfile(os.path.join(image_path, imagen))]:
		imagenes = os.path.join(image_path, imagen)
		print(imagenes)
		# img = cv2.resize(cv2.imread(imagenes, cv2.IMREAD_COLOR), (224, 224))
		# print(type(img))

		#zq 20230207 start1 resize等比例缩放 
		img =cv2.imread(imagenes, cv2.IMREAD_COLOR)
		w, h = img.shape[1], img.shape[0] #宽、高
		#print('初始值',w,h)
		size_h,size_w=224,224

		if h>w:
			scale = h/size_h
			w = int(w/scale)
			h = size_h    #图片的宽、高 以高封顶
		else:
			scale = w/size_w
			h = int(h/scale)
			w = size_w    #图片的宽、高 以高封顶
		#等比例缩放 https://www.iotword.com/6350.html
		img = cv2.resize(img, (w,h))
		# cv2.waitKey(0)
		# cv2.imshow('img',img) #查看图片
		#填充 https://blog.csdn.net/li17761193051/article/details/125321881
		#print('resize后',w,h)
		left = int((size_w-w)/2)
		right = left
		if w+left+right != size_w:
			left = size_w - w - right
			top = int((size_w-h)/2)
			bottom = top
		if h + top + bottom != size_h:
			top = size_h - h - bottom
		#print(top,bottom,left,right)
		img = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[255,255,255])
		# cv2.imshow('img',img) #查看图片
		# cv2.waitKey(0)
		#zq 20230207 end1

等比例缩放摘自: https://www.iotword.com/6350.html
填充摘自:填充 https://blog.csdn.net/li17761193051/article/details/125321881

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值