2024-07-26 16:42:17 +08:00
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resize_image(input_path, output_path, new_width=200):
|
|
|
|
|
# 读取原始图片
|
|
|
|
|
img = cv2.imread(input_path, cv2.IMREAD_UNCHANGED)
|
|
|
|
|
# 计算新的高度,保持纵横比
|
|
|
|
|
height, width = img.shape[:2]
|
|
|
|
|
width_percent = (new_width / float(width))
|
|
|
|
|
new_height = int((float(height) * width_percent))
|
|
|
|
|
|
|
|
|
|
# 调整图片大小
|
2024-07-29 16:51:24 +08:00
|
|
|
# resized_img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LANCZOS4)
|
|
|
|
|
resized_img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
|
|
|
|
|
2024-07-26 16:42:17 +08:00
|
|
|
# 保存调整后的图片
|
|
|
|
|
cv2.imwrite(output_path, resized_img)
|
|
|
|
|
print(f"图片已保存为 {output_path}")
|
2024-07-29 16:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
resize_image("./input/image.png","temp2.png")
|
|
|
|
|
|