MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

Python处理图片的小技巧!

2025-01-13 14:20 huorong 精选文章 3 ℃ 0 评论

使用Python可以方便的开发一些图像处理的小工具 ,比如图片常做的对比度提高、边缘检测、立体感增强 ,话不多说直接肝代码!!

对比度提高:

使用Python中的OpenCV库可以实现对比度的调整

import cv2

# 加载图像
image = cv2.imread('example.jpg')

# 增加对比度
alpha = 1.5
adjusted_image = cv2.convertScaleAbs(image, alpha=alpha, beta=0)

# 显示结果
cv2.imshow('Adjusted Image', adjusted_image)
cv2.waitKey(0)

边缘检测

使用Python中的OpenCV库可以实现常见的边缘检测算法

import cv2

# 加载图像
image = cv2.imread('example.jpg')

# 边缘检测
canny = cv2.Canny(image, 100, 200)

# 显示结果
cv2.imshow('Canny Edge Detection', canny)
cv2.waitKey(0)

立体感增强:

使用Python中的Pillow库可以实现图像的立体感增强

from PIL import Image, ImageFilter

# 加载图像
image = Image.open('example.jpg')

# 应用滤镜
blur_radius = 10
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=blur_radius))

# 将原图像和模糊图像进行融合
alpha = 0.5
merged_image = Image.blend(image, blurred_image, alpha)

# 显示结果
merged_image.show()

Envelope Plot

画包络图(Envelope Plot)可以用于显示数据点的范围,常用于信号处理和数据分析中。在Python中,可以使用Matplotlib库绘制包络图

import numpy as np
import matplotlib.pyplot as plt

# 生成数据
t = np.linspace(0, 2*np.pi, 100)
x = np.sin(t) + np.random.normal(0, 0.1, size=100)
y = np.cos(t) + np.random.normal(0, 0.1, size=100)

# 绘制数据点
plt.plot(x, y, '.', color='blue', markersize=5, label='Data')

# 计算包络线
from scipy.signal import savgol_filter
window_length = 21
polyorder = 2
x_envelope = savgol_filter(x, window_length, polyorder, deriv=0)
y_envelope = savgol_filter(y, window_length, polyorder, deriv=0)

# 绘制包络线
plt.plot(x_envelope, y_envelope, color='red', linewidth=2, label='Envelope')

# 设置图例和标题
plt.legend()
plt.title('Envelope Plot')

# 显示图像
plt.show()

Tags:deriv

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言