Skip to content

Instantly share code, notes, and snippets.

@vinsonzou
Created June 7, 2021 13:55
Show Gist options
  • Select an option

  • Save vinsonzou/4933a837568065436a8b7d3f172435ff to your computer and use it in GitHub Desktop.

Select an option

Save vinsonzou/4933a837568065436a8b7d3f172435ff to your computer and use it in GitHub Desktop.
照片尺寸转换
# pip install opencv-python
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 = 626
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 = "demo.jpg"
img = cv2.imread(image_path)
imagesize = ImageSize()
resize_img = imagesize.two_inch(img)
cv2.imwrite('resize.jpg', resize_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment