{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2023-10-16T12:19:47.212910600Z", "start_time": "2023-10-16T12:19:47.129804500Z" } }, "outputs": [], "source": [ "import cv2" ] }, { "cell_type": "code", "execution_count": 3, "outputs": [], "source": [ "# 读取图像\n", "image = cv2.imread(\"QR.png\")\n", "\n", "# 将图像转换为灰度图像\n", "gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", "\n", "# 显示灰度图像\n", "cv2.imshow(\"Gray Image\", gray_image)\n", "cv2.waitKey(0)\n", "cv2.destroyAllWindows()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-10-16T12:22:46.953510800Z", "start_time": "2023-10-16T12:21:49.911626400Z" } }, "id": "9cdf4af283159d8c" }, { "cell_type": "code", "execution_count": 1, "outputs": [], "source": [ "# 使用PIL库(Pillow)灰度化\n", "\n", "from PIL import Image\n", "import os\n", "\n", "# 图片所在文件夹路径\n", "folder_path = 'makeQR/QR'\n", "# 保存灰度图像的目标文件夹路径\n", "save_folder_path = 'makeQR/grayQR'\n", "\n", "# 获取文件夹中所有图像文件的路径\n", "image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if filename.endswith(('.jpg', '.jpeg', '.png'))]\n", "\n", "# 批量灰度化处理并保存\n", "for image_path in image_paths:\n", " # 打开图像文件\n", " image = Image.open(image_path)\n", "\n", " # 将图像转换为灰度图像\n", " gray_image = image.convert('L')\n", "\n", " # 确定保存的文件名\n", " filename = os.path.basename(image_path)\n", " save_filename = 'gray_' + filename\n", " save_path = os.path.join(save_folder_path, save_filename)\n", "\n", " # 保存灰度图像\n", " gray_image.save(save_path)\n", "\n", " # # 显示灰度图像(可选)\n", " # gray_image.show()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-10-18T05:25:15.732943300Z", "start_time": "2023-10-18T05:25:15.450801700Z" } }, "id": "dab65fd87672a96e" }, { "cell_type": "code", "execution_count": 4, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D:\\Waste\\QRgray\\0.jpg\n", "D:\\Waste\\QRgray\\1.jpg\n", "D:\\Waste\\QRgray\\10.jpg\n", "D:\\Waste\\QRgray\\11.jpg\n", "D:\\Waste\\QRgray\\12.jpg\n", "D:\\Waste\\QRgray\\13.jpg\n", "D:\\Waste\\QRgray\\14.jpg\n", "D:\\Waste\\QRgray\\15.jpg\n", "D:\\Waste\\QRgray\\16.jpg\n", "D:\\Waste\\QRgray\\17.jpg\n", "D:\\Waste\\QRgray\\18.jpg\n", "D:\\Waste\\QRgray\\19.jpg\n", "D:\\Waste\\QRgray\\2.jpg\n", "D:\\Waste\\QRgray\\20.jpg\n", "D:\\Waste\\QRgray\\21.jpg\n", "D:\\Waste\\QRgray\\21.png\n", "D:\\Waste\\QRgray\\22.jpg\n", "D:\\Waste\\QRgray\\22.png\n", "D:\\Waste\\QRgray\\23.jpg\n", "D:\\Waste\\QRgray\\23.png\n", "D:\\Waste\\QRgray\\24.jpg\n", "D:\\Waste\\QRgray\\24.png\n", "D:\\Waste\\QRgray\\25.jpg\n", "D:\\Waste\\QRgray\\25.png\n", "D:\\Waste\\QRgray\\26.jpg\n", "D:\\Waste\\QRgray\\27.jpg\n", "D:\\Waste\\QRgray\\28.jpg\n", "D:\\Waste\\QRgray\\29.jpg\n", "D:\\Waste\\QRgray\\3.jpg\n", "D:\\Waste\\QRgray\\4.jpg\n", "D:\\Waste\\QRgray\\5.jpg\n", "D:\\Waste\\QRgray\\6.jpg\n", "D:\\Waste\\QRgray\\7.jpg\n", "D:\\Waste\\QRgray\\8.jpg\n", "D:\\Waste\\QRgray\\9.jpg\n", "D:\\Waste\\QRgray\\QR0.jpg\n", "D:\\Waste\\QRgray\\QR1.jpg\n", "D:\\Waste\\QRgray\\QR10.jpg\n", "D:\\Waste\\QRgray\\QR11.jpg\n", "D:\\Waste\\QRgray\\QR12.jpg\n", "D:\\Waste\\QRgray\\QR13.jpg\n", "D:\\Waste\\QRgray\\QR14.jpg\n", "D:\\Waste\\QRgray\\QR15.jpg\n", "D:\\Waste\\QRgray\\QR16.jpg\n", "D:\\Waste\\QRgray\\QR17.jpg\n", "D:\\Waste\\QRgray\\QR18.jpg\n", "D:\\Waste\\QRgray\\QR19.jpg\n", "D:\\Waste\\QRgray\\QR2.jpg\n", "D:\\Waste\\QRgray\\QR20.jpg\n", "D:\\Waste\\QRgray\\QR21.jpg\n", "D:\\Waste\\QRgray\\QR22.jpg\n", "D:\\Waste\\QRgray\\QR23.jpg\n", "D:\\Waste\\QRgray\\QR24.jpg\n", "D:\\Waste\\QRgray\\QR25.jpg\n", "D:\\Waste\\QRgray\\QR26.jpg\n", "D:\\Waste\\QRgray\\QR27.jpg\n", "D:\\Waste\\QRgray\\QR28.jpg\n", "D:\\Waste\\QRgray\\QR29.jpg\n", "D:\\Waste\\QRgray\\QR3.jpg\n", "D:\\Waste\\QRgray\\QR4.jpg\n", "D:\\Waste\\QRgray\\QR5.jpg\n", "D:\\Waste\\QRgray\\QR6.jpg\n", "D:\\Waste\\QRgray\\QR7.jpg\n", "D:\\Waste\\QRgray\\QR8.jpg\n", "D:\\Waste\\QRgray\\QR9.jpg\n", "D:\\Waste\\QRgray\\tft_0.png\n", "D:\\Waste\\QRgray\\tft_1.png\n", "D:\\Waste\\QRgray\\tft_10.png\n", "D:\\Waste\\QRgray\\tft_11.png\n", "D:\\Waste\\QRgray\\tft_12.png\n", "D:\\Waste\\QRgray\\tft_13.png\n", "D:\\Waste\\QRgray\\tft_14.png\n", "D:\\Waste\\QRgray\\tft_15.png\n", "D:\\Waste\\QRgray\\tft_16.png\n", "D:\\Waste\\QRgray\\tft_17.png\n", "D:\\Waste\\QRgray\\tft_18.png\n", "D:\\Waste\\QRgray\\tft_19.png\n", "D:\\Waste\\QRgray\\tft_2.png\n", "D:\\Waste\\QRgray\\tft_20.png\n", "D:\\Waste\\QRgray\\tft_3.png\n", "D:\\Waste\\QRgray\\tft_4.png\n", "D:\\Waste\\QRgray\\tft_5.png\n", "D:\\Waste\\QRgray\\tft_6.png\n", "D:\\Waste\\QRgray\\tft_7.png\n", "D:\\Waste\\QRgray\\tft_8.png\n", "D:\\Waste\\QRgray\\tft_9.png\n", "D:\\Waste\\QRgray\\weixin001.jpg\n" ] } ], "source": [ "# 使用opencv灰度化\n", "import cv2\n", "import os\n", "\n", "# 图片所在文件夹路径\n", "folder_path = r'D:\\Waste\\QRgray'\n", "# 保存灰度图像的目标文件夹路径\n", "save_folder_path = r'D:\\Waste\\1'\n", "\n", "# 获取文件夹中所有图像文件的路径\n", "image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if filename.endswith(('.jpg', '.jpeg', '.png'))]\n", "\n", "# 批量灰度化处理并保存\n", "for image_path in image_paths:\n", " # 读取图像\n", " print(image_path)\n", " image = cv2.imread(image_path)\n", "\n", " # 将图像转换为灰度图像\n", " gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", "\n", " # 确定保存的文件名\n", " filename = os.path.basename(image_path)\n", " save_filename = 'gray_' + filename\n", " save_path = os.path.join(save_folder_path, save_filename)\n", "\n", " # 保存灰度图像\n", " cv2.imwrite(save_path, gray_image)\n", "\n", " # # 显示灰度图像(可选)\n", " # cv2.imshow('Gray Image', gray_image)\n", " # cv2.waitKey(0)\n", " # cv2.destroyAllWindows()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-10-19T01:58:35.284872800Z", "start_time": "2023-10-19T01:58:33.196237500Z" } }, "id": "6f1bbc4682e42a8" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }