mirror of
https://github.com/N1ngYu/SaltBot.git
synced 2025-09-28 08:42:40 +08:00
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import cv2
|
|
from pyzbar.pyzbar import decode
|
|
import logging
|
|
import sys
|
|
|
|
sys.setrecursionlimit(3000) # 增加最大递归深度
|
|
|
|
|
|
|
|
# 配置日
|
|
def convert_to_grayscale(image_path: str) -> str:
|
|
"""
|
|
将输入图片转换为灰度图并保存
|
|
:param image_path: 输入图片的路径
|
|
:return: 灰度图的保存路径
|
|
"""
|
|
try:
|
|
# 读取图片
|
|
image = cv2.imread(image_path)
|
|
if image is None:
|
|
print(f"无法读取图片: {image_path}")
|
|
return None
|
|
|
|
# 转换为灰度图
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# 保存灰度图
|
|
gray_image_path = image_path.replace(".jpg", "_gray.jpg").replace(".png", "_gray.png")
|
|
cv2.imwrite(gray_image_path, gray_image)
|
|
print(f"灰度图已保存到 {gray_image_path}")
|
|
return gray_image_path
|
|
except Exception as e:
|
|
print(f"转换为灰度图时发生错误: {e}")
|
|
return None
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 输入图片路径
|
|
image_path = input("请输入图片的路径: ")
|
|
|
|
# 转换为灰度图
|
|
gray_image_path = convert_to_grayscale(image_path)
|
|
if gray_image_path:
|
|
print(f"灰度图已保存到 {gray_image_path}")
|
|
else:
|
|
print("无法转换为灰度图") |