使用Python實(shí)現(xiàn)將圖片轉(zhuǎn)線條圖
1、背景介紹
有時(shí)候我們需要將一張圖轉(zhuǎn)為線條圖片,這樣看起來(lái)更加的有意思,我們可以利用python中的一些圖片處理的庫(kù)對(duì)圖片進(jìn)行處理,先將圖片使用灰度模式讀取圖片,然后將圖片轉(zhuǎn)換成線條圖片
2、庫(kù)的安裝
| 庫(kù) | 用途 | 安裝 |
|---|---|---|
| PyQt5 | 界面設(shè)計(jì) | pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
| opencv-python | 視頻處理 | pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
| matplotlib | 圖片處理 | pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
| os | 獲取路徑 | 內(nèi)置庫(kù)無(wú)需安裝 |
3、完整代碼
注意事項(xiàng),以下請(qǐng)用相對(duì)路徑
image = cv2.imread('aaa.png', cv2.IMREAD_GRAYSCALE)
import cv2
import matplotlib.pyplot as plt
# 讀取圖片,使用灰度模式讀取圖片
image = cv2.imread('aaa.png', cv2.IMREAD_GRAYSCALE)
# 使用Canny邊緣檢測(cè)算法進(jìn)行邊緣提取,threshold1和threshold2是邊緣檢測(cè)的閾值
edges = cv2.Canny(image, threshold1=100, threshold2=200)
# 創(chuàng)建一個(gè)圖形窗口,設(shè)置顯示圖像的大小
plt.figure(figsize=(10, 5))
# 顯示原始灰度圖像
plt.subplot(1, 2, 1) # 將顯示區(qū)域分為1行2列,當(dāng)前顯示第一列
plt.title('Original Image') # 給當(dāng)前圖像設(shè)置標(biāo)題
plt.imshow(image, cmap='gray') # 顯示圖片,使用灰度色圖
# 顯示經(jīng)過(guò)Canny算法處理后的邊緣圖像
plt.subplot(1, 2, 2) # 當(dāng)前顯示第二列
plt.title('Edges') # 給邊緣圖像設(shè)置標(biāo)題
plt.imshow(edges, cmap='gray') # 顯示邊緣檢測(cè)結(jié)果,使用灰度色圖
# 顯示所有圖像
plt.show()
# 將邊緣檢測(cè)的結(jié)果保存為圖片
cv2.imwrite('edges_output.jpg', edges)5、完整代碼(GUI版本)
注意事項(xiàng),以下請(qǐng)用相對(duì)路徑
import sys
import cv2
import os
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt
class ImageProcessor(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('批量圖像邊緣檢測(cè)')
self.setGeometry(900, 500, 400, 200)
layout = QVBoxLayout()
self.label = QLabel('選擇圖片進(jìn)行批量邊緣檢測(cè)', self)
self.label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label)
self.btn_select = QPushButton('選擇圖片', self)
self.btn_select.clicked.connect(self.select_images)
layout.addWidget(self.btn_select)
self.btn_process = QPushButton('處理并保存', self)
self.btn_process.clicked.connect(self.process_images)
layout.addWidget(self.btn_process)
self.setLayout(layout)
def select_images(self):
# 選擇多個(gè)圖片文件
options = QFileDialog.Options()
self.filenames, _ = QFileDialog.getOpenFileNames(self, "選擇圖片文件", "",
"Images (*.png *.jpg *.bmp);;All Files (*)", options=options)
if self.filenames:
# 獲取當(dāng)前工作目錄或自定義目錄作為基準(zhǔn)目錄
base_dir = os.getcwd() # 或者指定一個(gè)路徑,例如 r"C:\Users\小莊的Y9000P\Desktop"
# 將絕對(duì)路徑轉(zhuǎn)換為相對(duì)路徑
self.filenames = [os.path.relpath(filename, base_dir) for filename in self.filenames]
self.label.setText(f'已選擇 {len(self.filenames)} 張圖片')
def process_images(self):
if not self.filenames:
QMessageBox.warning(self, '警告', '請(qǐng)先選擇圖片文件')
return
for filename in self.filenames:
try:
print(f"正在處理文件: {filename}") # 添加調(diào)試信息
# 讀取圖片,使用灰度模式讀取圖片
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
if image is None:
print(f"無(wú)法讀取文件: {filename}") # 添加調(diào)試信息
raise Exception(f"無(wú)法讀取文件: {filename}")
# 使用Canny邊緣檢測(cè)算法進(jìn)行邊緣提取
edges = cv2.Canny(image, threshold1=100, threshold2=200)
# 保存邊緣檢測(cè)結(jié)果
output_filename = filename.rsplit('.', 1)[0] + '_edges.jpg'
cv2.imwrite(output_filename, edges)
except Exception as e:
QMessageBox.warning(self, '錯(cuò)誤', f'處理文件 {filename} 時(shí)出錯(cuò): {str(e)}')
continue
QMessageBox.information(self, '完成', '所有圖片處理完成并已保存')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ImageProcessor()
ex.show()
sys.exit(app.exec_())效果圖


以上就是使用Python實(shí)現(xiàn)將圖片轉(zhuǎn)線條圖的詳細(xì)內(nèi)容,更多關(guān)于Python圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組
這篇文章主要介紹了Python如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Python如何使用struct.unpack處理二進(jìn)制文件
這篇文章主要介紹了Python如何使用struct.unpack處理二進(jìn)制文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
詳解Python的Django框架中Manager方法的使用
這篇文章主要介紹了Python的Django框架中Manager方法的使用,包括修改初始Manager QuerySets和增加額外的Manager方法等操作,需要的朋友可以參考下2015-07-07
Python實(shí)現(xiàn)從百度API獲取天氣的方法
這篇文章主要介紹了Python實(shí)現(xiàn)從百度API獲取天氣的方法,實(shí)例分析了Python操作百度API的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
tensorflow指定CPU與GPU運(yùn)算的方法實(shí)現(xiàn)
這篇文章主要介紹了tensorflow指定CPU與GPU運(yùn)算的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python實(shí)現(xiàn)文件信息進(jìn)行合并實(shí)例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)文件信息進(jìn)行合并實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

