-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcommon.py
More file actions
96 lines (83 loc) · 2.76 KB
/
Copy pathcommon.py
File metadata and controls
96 lines (83 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# common.py
#
# Author: Yann KOETH
# Created: Wed Jul 16 19:11:21 2014 (+0200)
# Last-Updated: Thu Jul 24 10:33:48 2014 (+0200)
# By: Yann KOETH
# Update #: 148
#
import cv2
import os
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QDesktopWidget, QLabel, QGraphicsBlurEffect, QGraphicsPixmapItem
from tree import Tree
class CustomException(Exception):
pass
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
def setPickerColor(color, colorPicker):
"""Set the color picker color.
"""
css = 'QWidget { background-color: %s; border-width: 0; \
border-radius: 2px; border-color: #555; border-style: outset; }'
colorPicker.setStyleSheet(css % color.name())
def checkerboard(size):
"""Create a checkboard.
"""
h, w = size.height(), size.width()
c0 = (191, 191, 191, 255)
c1 = (255, 255, 255, 255)
blocksize = 8
coords = np.ogrid[0:h,0:w]
idx = (coords[0] // blocksize + coords[1] // blocksize) % 2
vals = np.array([c0, c1], dtype=np.uint8)
return np2Qt(vals[idx])
def np2Qt(image):
"""Convert numpy array to QPixmap.
"""
height, width, bytesPerComponent = image.shape
bytesPerLine = 4 * width
if bytesPerComponent == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
qimg = QImage(image.data, width, height,
bytesPerLine, QImage.Format_ARGB32)
return QPixmap.fromImage(qimg)
def fitImageToScreen(pixmap):
"""Fit pixmap to screen.
"""
resolution = QDesktopWidget().screenGeometry()
h, w = resolution.width(), resolution.height()
w = min(pixmap.width(), w)
h = min(pixmap.height(), h)
return pixmap.scaled(QtCore.QSize(w, h), QtCore.Qt.KeepAspectRatio)
def blurPixmap(pixmap, radius):
effect = QGraphicsBlurEffect()
effect.setBlurRadius(radius)
buffer = QPixmap(pixmap)
item = QGraphicsPixmapItem(buffer)
item.setGraphicsEffect(effect)
output = QPixmap(pixmap.width(), pixmap.height())
painter = QtGui.QPainter(output)
scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView(scene)
scene.addItem(item)
scene.render(painter)
return output
def scaleRect(rect, scale):
"""Scale 'rect' with a factor of 'scale'.
"""
x, y, w, h = rect
return (x * scale, y * scale, w * scale, h * scale)
def getObjectsTree(qTreeView, table, indexes, extract):
"""Create an object tree representation from QTreeView.
"""
tree = Tree()
model = qTreeView.model()
extracted = tree.fromQStandardItemModel(model, table, indexes, extract)
return tree, extracted