C++中修改PyQt对象

This commit is contained in:
Irony 2020-03-02 21:05:24 +08:00
parent 0534fb8a4a
commit 5065d26249
8 changed files with 129 additions and 0 deletions

7
.gitignore vendored
View file

@ -3,6 +3,13 @@ __pycache__/
*.py[co] *.py[co]
*$py.class *$py.class
debug
release
.qmake.stash
Makefile
Makefile.Debug
Makefile.Release
Tmp Tmp
Tmp/* Tmp/*

Binary file not shown.

View file

@ -0,0 +1,30 @@
#-------------------------------------------------
#
# Project created by QtCreator 2020-03-02T16:57:48
#
#-------------------------------------------------
TARGET = Cold
TEMPLATE = lib
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
cold.cpp
HEADERS += \
cold.h
unix {
target.path = /usr/lib
INSTALLS += target
}

View file

@ -0,0 +1,2 @@
qmake
nmake

View file

@ -0,0 +1,19 @@
#include "cold.h"
void cold(QImage &src, int delta)
{
int rows = src.height();
int cols = src.width();
for (int i = 0; i < rows; i++)
{
QRgb *line = (QRgb *)src.scanLine(i);
for (int j = 0; j < cols; j++)
{
int r = qRed(line[j]);
int g = qGreen(line[j]);
int b = qBlue(line[j]) + delta;
b = qBound(0, b, 255);
src.setPixel(j, i, qRgb(r, g, b));
}
}
}

View file

@ -0,0 +1,4 @@
#include <QImage>
#include <QColor>
extern "C" __declspec(dllexport) void cold(QImage &src, int delta);

View file

@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2020年3月2日
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: 892768447@qq.com
@file:
@description: 冷色调
'''
import sys
from ctypes import CDLL
from time import time
# For PyQt5
try:
from PyQt5 import sip
except:
import sip
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QSlider, QVBoxLayout
# For PySide2
# import shiboken2
# from PySide2.QtCore import Qt
# from PySide2.QtGui import QImage, QPixmap
# from PySide2.QtWidgets import QApplication, QWidget, QLabel, QSlider, QVBoxLayout
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.imgLabel = QLabel(self)
self.coldSlider = QSlider(Qt.Horizontal, self)
self.coldSlider.valueChanged.connect(self.doChange)
self.coldSlider.setRange(0, 255)
layout.addWidget(self.imgLabel)
layout.addWidget(self.coldSlider)
# 加载图片
self.srcImg = QImage('src.jpg')
self.imgLabel.setPixmap(QPixmap.fromImage(self.srcImg).scaledToWidth(800, Qt.SmoothTransformation))
# DLL库
self.dll = CDLL('Cold.dll')
print(self.dll)
def doChange(self, value):
t = time()
img = self.srcImg.copy() # 复制一份
# For PyQt5
self.dll.cold(sip.unwrapinstance(img), value)
# For PySide2
# self.dll.cold(shiboken2.getCppPointer(img)[0], value)
self.imgLabel.setPixmap(QPixmap.fromImage(img).scaledToWidth(800, Qt.SmoothTransformation))
print('use time:', time() - t)
if __name__ == "__main__":
app = QApplication(sys.argv)
view = Window()
view.show()
sys.exit(app.exec_())

Binary file not shown.

After

Width:  |  Height:  |  Size: 612 KiB