from PyQt5.QtWidgets import QDialog, QFileDialog, QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.Qt import Qt
from sketch.uis.sketchui import Ui_sketch
from sketch.core.sketchstyle import SketchStyle
from PIL import Image
import cv2
import sys, os
import numpy as np
class SketchFrame(QDialog):
def __init__(self):
super(SketchFrame, self).__init__()
# 完成UI的加载
self.ui = Ui_sketch()
# 使用setupUi绑定对话框(父窗体)
self.ui.setupUi(self)
# 创建素描风格对象
self.sketchstyle = SketchStyle()
# 定义图片路径
self.filepath = ""
# 覆盖QDialog原来两个我们不需要的默认功能
def closeEvent(self, e):
# 窗体关闭前的释放工作,条件不满足可以阻止窗体关闭
sys.exit(0)
def keyPressEvent(self, e):
# 阻止按照ESC键关闭窗体
pass
# 显示图像
def images_show(self, h, w, c, data):
image = QImage(data, w, h, w * c ,QImage.Format_RGB888)
pix = QPixmap.fromImage(image)
# 获取QLabel的大小
width = self.ui.label_img.width()
height = self.ui.label_img.height()
# 等比例放缩图片
scaledPixmap = pix.scaled(width,height,Qt.KeepAspectRatio)
self.ui.label_img.setPixmap(scaledPixmap)
# 槽函数,加载图像
def load_image(self):
image_format = ['jpg', 'png', 'bmp']
self.filepath, imgtype = QFileDialog.getOpenFileName(self, "加载图片", "./", "(*.jpg *.png *.bmp)")
if not len(self.filepath):
QMessageBox.information(self, "提示", "请加载需要处理的图片!", QMessageBox.Yes)
else:
for i in range(len(image_format)):
ret = self.filepath.find(image_format[i])
break
if not ret:
QMessageBox.information(self, "提示", "请加载正确的图片!", QMessageBox.Yes)
else:
self.ui.textEdit_addr.setText(self.filepath)
data = self.sketchstyle.readFile(self.filepath)
img_data = data[3]
img_data = cv2.cvtColor(img_data, cv2.COLOR_RGB2BGR)
h, w, c = img_data.shape
self.img = img_data
self.images_show(h, w, c, img_data.tobytes())
# 槽函数,图片另存为...
def save_image(self):
if(self.filepath == ''):
QMessageBox.information(self, "提示", "请先加载图片!", QMessageBox.Yes)
else:
save_path = QFileDialog.getSaveFileName(self, "文件保存", "./", "JPG Image (*.jpg);;PNG Image (*.png);;BMP Image (*.bmp);;All Files (*)")
if save_path[0] == '':
QMessageBox.information(self, "提示", "请加载需要处理的图片!", QMessageBox.Yes)
else:
im = Image.fromarray(self.img)
im.save(save_path[0])
# 槽函数,使用素描滤镜
def sketch(self):
if(self.filepath == ''):
QMessageBox.information(self, "提示", "请先加载图片!", QMessageBox.Yes)
else:
# 调用sketchstyle素描风格对象的sketch素描特效函数,返回素描处理的图片
self.img = self.sketchstyle.sketch(self.sketchstyle.img_src)
h, w, c = self.img.shape
self.images_show(h, w, c, self.img.tobytes())
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
案例@图像素描效果.zip (16个子文件)
03_图像素描效果
run_core.bat 81B
sketch
core
__pycache__
sketchstyle.cpython-37.pyc 2KB
sketchstyle.py 2KB
main.py 252B
__pycache__
main.cpython-37.pyc 330B
uis
sketchframe.py 4KB
__pycache__
sketchframe.cpython-37.pyc 3KB
sketchapp.cpython-37.pyc 692B
sketchui.cpython-37.pyc 2KB
tools.bat 32B
sketchui.py 3KB
sketchapp.py 302B
sketch.ui 4KB
run_app.bat 50B
demo
result.jpg 50KB
lena.png 354KB
共 16 条
- 1
撸码的xiao摩羯
- 粉丝: 168
- 资源: 92
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0