import cv2import osimport numpy as npfrom loguru import loggerCAPTCHA_WIDTH = 520CAPTCHA_HEIGHT = 320for root, dirs, files in os.walk('input'):for file in files:image_path = os.path.join(root, file)logger.debug(f'image path {image_path}')image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)image_height, image_width, image_dim = image.shapeif image_dim == 3:b_channel, g_channel, r_channel = cv2.split(image)alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255image = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))if image_width / image_height > CAPTCHA_WIDTH / CAPTCHA_HEIGHT:image = cv2.resize(image, (int(CAPTCHA_HEIGHT * image_width / image_height), CAPTCHA_HEIGHT))image_height, image_width, _ = image.shapeimage = image[:, int(image_width / 2 - CAPTCHA_WIDTH / 2): int(image_width / 2 + CAPTCHA_WIDTH / 2)]else:image = cv2.resize(image, (CAPTCHA_WIDTH, int(CAPTCHA_WIDTH * image_height / image_width)))image_height, image_width, _ = image.shapeimage = image[int(image_height / 2 - CAPTCHA_HEIGHT / 2): int(image_height / 2 + CAPTCHA_HEIGHT / 2), :]cv2.imwrite(f'output/{file}', image)logger.debug(f'finish output/{file}')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。