python--修改证件照的大小

1、一般证件照的尺寸有小一寸、一寸、小二寸、二寸、五寸、六寸、七寸(横向)以及身份证照片的大小,用python获取各种尺寸的证件照

2、根据查找的资料,每种尺寸对应的像素大小如下表:

图片

像素大小

小一寸

260x390

一寸

295x413

小二寸

390x567

二寸

413x636

5寸

840x1200

6寸

960x1440

7寸

1680x1200

身份证大头照

358x441

3、图片裁剪

一般而言,修改图片大小用opencv直接resize就ok了,但是问题是resize到特定大小会使照片产生形变,所以必须先对图片进行裁剪。图片要怎么裁剪?裁剪多大?

方法:

(1)计算标准图片的宽高比例ratio1,计算原图的宽高比例ratio2,ratio = w/h

(2)ratio2 >= ratio1,进行左右裁剪,使w变小达到标准ratio1的比例

(3)ratio2 < ratio1,进行下裁剪,使h变小达到标准ratio1的比例(不进行上裁剪就是怕人头被剪到)

(4)resize到标准的尺寸

4、代码

import cv2
class ImageSize:
	def __init__(self):
		# 小一寸
		self._lt_one_inch_w = 260
		self._lt_one_inch_h = 390
		self._lt_one_ratio= self._lt_one_inch_w / self._lt_one_inch_h

		# 一寸
		self._one_inch_w = 295
		self._one_inch_h = 413
		self._one_ratio= self._one_inch_w / self._one_inch_h

		# 小二寸
		self._lt_two_inch_w = 390
		self._lt_two_inch_h = 567
		self._lt_two_ratio= self._lt_two_inch_w / self._lt_two_inch_h

		# 二寸
		self._two_inch_w = 413
		self._two_inch_h = 636
		self._two_ratio= self._two_inch_w / self._two_inch_h

		# 五寸
		self._five_inch_w = 840
		self._five_inch_h = 1200
		self._five_ratio= self._five_inch_w / self._five_inch_h

		# 六寸
		self._six_inch_w = 960
		self._six_inch_h = 1440
		self._six_ratio= self._six_inch_w / self._six_inch_h

		# 七寸
		self._seven_inch_w = 1680
		self._seven_inch_h = 1200
		self._seven_ratio= self._seven_inch_w / self._seven_inch_h

		# ID card
		self._id_inch_w = 358
		self._id_inch_h = 441
		self._id_ratio= self._id_inch_w / self._id_inch_h

	def lt_one_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._lt_one_ratio:
			ratio_img = self.crop_l_r(img, self._lt_one_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._lt_one_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._lt_one_inch_w, self._lt_one_inch_h))
		return resize_img

	def one_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._one_ratio:
			ratio_img = self.crop_l_r(img, self._one_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._one_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._one_inch_w, self._one_inch_h))
		return resize_img

	def lt_two_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._lt_two_ratio:
			ratio_img = self.crop_l_r(img, self._lt_two_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._lt_two_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._lt_two_inch_w, self._lt_two_inch_h))
		return resize_img

	def two_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._two_ratio:
			ratio_img = self.crop_l_r(img, self._two_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._two_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._two_inch_w, self._two_inch_h))
		return resize_img

	def five_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._five_ratio:
			ratio_img = self.crop_l_r(img, self._five_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._five_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._five_inch_w, self._five_inch_h))
		return resize_img

	def six_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._six_ratio:
			ratio_img = self.crop_l_r(img, self._six_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._six_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._six_inch_w, self._six_inch_h))
		return resize_img

	def seven_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._seven_ratio:
			ratio_img = self.crop_l_r(img, self._seven_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._seven_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._seven_inch_w, self._seven_inch_h))
		return resize_img

	def id_inch(self, img):
		img = img
		h, w = img.shape[:2]
		img_ratio = w / h

		if img_ratio >= self._id_ratio:
			ratio_img = self.crop_l_r(img, self._id_ratio, h, w)
		else:
			ratio_img = self.crop_d(img, self._id_ratio, h, w)

		resize_img = cv2.resize(ratio_img, (self._id_inch_w, self._id_inch_h))
		return resize_img

	def crop_l_r(self, img, ratio, h, w):
		"""
		左右裁剪
		图片大小比例 > ratio
		"""
		crop_w = int(ratio * h)
		# 将w左右裁剪成这个crop_w的大小
		hide_w = (w - crop_w) // 2
		return_img = img[:, hide_w:w - hide_w]
		return return_img

	def crop_d(self, img, ratio, h, w):
		"""
		图片下裁剪
		图片的大小比例 < ratio
		"""
		crop_h = int(w / ratio)
		hide_h = h - crop_h
		return_img = img[:h-hide_h, :]
		return return_img

if __name__ == "__main__":
	image_path = "1.jpg"
	img = cv2.imread(image_path)
	imagesize = ImageSize()
	resize_img = imagesize.lt_one_inch(img)
	cv2.imwrite('resize.jpg', resize_img)

 

你可能感兴趣的:(python)