from PIL import Image
import os.path# 指明被遍历的文件夹
rootdir = r'F:\scientific research'for parent, dirnames, filenames in os.walk(rootdir): # 遍历每一张图片for filename in filenames:print('parent is :' + parent)print('filename is :' + filename)currentPath = os.path.join(parent, filename)print('the fulll name of the file is :' + currentPath)img = Image.open(currentPath)print(img.format, img.size, img.mode)# img.show()box1 = (856, 529, 1316, 989) # 设置左、上、右、下的像素image1 = img.crop(box1) # 图像裁剪image1.save(r"F:\scientific research\result10.28_1" + '\\' + filename) # 存储裁剪得到的图像
import cv2
import numpy as np
import matplotlib.pyplot as plt# 读取彩色图像
img = cv2.imread('DSC_3681.JPG')# 将彩色图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 计算灰度直方图
hist, bins = np.histogram(gray.ravel(), 256, [0, 256])# 输出灰度直方图信息
plt.hist(gray.ravel(), 256, [0, 256])
# 添加图例
plt.xlabel('Pixel value')
plt.ylabel('Pixel count')
plt.title('Grayscale Histogram')
plt.show()
上一篇:消息中间件----内存数据库 Redis7(第3章 Redis 命令)
下一篇:代码随想录算法训练营第三十三天 | leetcode 1005. K 次取反后最大化的数组和,134. 加油站,135. 分发糖果)