From 1bea995dc1acc0d90afef6ff599059d2d43fea13 Mon Sep 17 00:00:00 2001 From: Ching Date: Thu, 10 Feb 2022 15:30:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(scripts):=20=E5=A2=9E=E5=8A=A0=E5=B0=86?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=BD=AC=E4=B8=BA=E7=81=B0=E5=BA=A6=E5=9B=BE?= =?UTF-8?q?=E7=9A=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加将图片转为灰度图的脚本 Signed-off-by: Ching --- scripts/grayscale.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 scripts/grayscale.py diff --git a/scripts/grayscale.py b/scripts/grayscale.py new file mode 100644 index 0000000..80a0170 --- /dev/null +++ b/scripts/grayscale.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +import sys +import numpy as np +import cv2 # opencv-python + +# 引入Python的可视化工具包 matplotlib +from matplotlib import pyplot as plt + + +def print_img_info(img): + print("================打印一下图像的属性================") + print("图像对象的类型 {}".format(type(img))) + print(img.shape) + print("图像宽度: {} pixels".format(img.shape[1])) + print("图像高度: {} pixels".format(img.shape[0])) + # GRAYScale 没有第三个维度哦, 所以这样会报错 + # print("通道: {}".format(img.shape[2])) + print("图像分辨率: {}".format(img.size)) + print("数据类型: {}".format(img.dtype)) + + +if __name__ == '__main__': + # 导入一张图像 模式为彩色图片 + img_path = sys.argv[1] + img = cv2.imread(img_path, cv2.IMREAD_COLOR) + + # 将色彩空间转变为灰度图并展示 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + + # 打印图片信息 + # print_img_info(gray) + + # 打印图片的局部 + # print("打印图片局部") + # print(gray[100:105, 100:105]) + + # plt.imshow(gray) + # 需要添加colormap 颜色映射函数为gray + plt.imshow(gray, cmap="gray") + + # 隐藏坐标系 + plt.axis('off') + # 展示图片 + + # plt.show() + # 你也可以保存图片, 填入图片路径就好 + new_name = '%s-gray.%s' % (img_path.split('.')[0], img_path.split('.')[-1]) + plt.savefig(new_name) + print(new_name)