code release

This commit is contained in:
iperov 2021-07-23 17:34:49 +04:00
commit a902f11f74
354 changed files with 826570 additions and 1 deletions

43
xlib/qt/gui/QXImage.py Normal file
View file

@ -0,0 +1,43 @@
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from .QXPixmap import QXPixmap
class QXImage(QImage):
"""
extension of QImage
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cache = {}
def as_QXPixmap(self) -> QXPixmap:
pixmap = self._cache.get(QXPixmap, None )
if pixmap is None:
pixmap = self._cache[QXPixmap] = QXPixmap(QPixmap.fromImage(self))
return pixmap
def as_QIcon(self) -> QIcon:
icon = self._cache.get(QIcon, None )
if icon is None:
icon = self._cache[QIcon] = QIcon(self.as_QXPixmap())
return icon
def colored(self, color) -> 'QXImage':
"""
get colored version from cache or create.
"""
image = self._cache.get(color, None)
if image is None:
pixmap = self.as_QXPixmap()
qp = QPainter(pixmap)
qp.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
qp.fillRect( pixmap.rect(), QColor(color) )
qp.end()
image = self._cache[color] = QXImage( pixmap.toImage() )
return image

View file

@ -0,0 +1,24 @@
from typing import List
from .QXImage import QXImage
class QXImageSequence:
"""
contains a list of QXImage with defined FPS
"""
def __init__(self, frames : List[QXImage], fps : float):
super().__init__()
self._frames = frames
self._fps = fps
self._frame_count = len(frames)
def get_fps(self) -> float: return self._fps
def get_frame_count(self) -> int: return self._frame_count
def get_frame(self, i) -> QXImage: return self._frames[i]
def get_duration(self) -> int:
"""
return duration in ms
"""
return int( (self._frame_count / self._fps) * 1000 )

51
xlib/qt/gui/QXPixmap.py Normal file
View file

@ -0,0 +1,51 @@
from PyQt6.QtCore import *
from PyQt6.QtGui import *
class QXPixmap(QPixmap):
"""
extension of QPixmap
contains cached scaled versions
cached grayscaled
cached QIcon
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cache = {}
def scaled_cached(self, width: int, height: int, aspectRatioMode: Qt.AspectRatioMode = Qt.AspectRatioMode.KeepAspectRatio) -> 'QPixmap':
"""
get scaled version from cache or create.
"""
key = (width, height)
pixmap = self._cache.get(key, None)
if pixmap is None:
pixmap = self._cache[key] = QXPixmap( self.scaled(width, height, aspectRatioMode=aspectRatioMode, transformMode=Qt.TransformationMode.SmoothTransformation) )
return pixmap
def as_QIcon(self) -> QIcon:
icon = self._cache.get( QIcon, None )
if icon is None:
icon = self._cache[QIcon] = QIcon(self)
return icon
def grayscaled_cached(self) -> 'QXPixmap':
"""
get grayscaled version from cache or create.
"""
key = 'grayscaled'
pixmap = self._cache.get(key, None)
if pixmap is None:
pixmap = QXPixmap(self)
qp = QPainter(pixmap)
qp.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
qp.fillRect( pixmap.rect(), QColor(127,127,127,255) )
qp.end()
pixmap = self._cache[key] = pixmap
return pixmap

32
xlib/qt/gui/from_file.py Normal file
View file

@ -0,0 +1,32 @@
from PyQt6.QtGui import *
from .QXImage import QXImage
from .QXPixmap import QXPixmap
def QPixmap_from_file(filepath, color=None):
img = QPixmap(str(filepath))
if color is not None:
qp = QPainter(img)
qp.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
qp.fillRect( img.rect(), QColor(color) )
qp.end()
return img
def QXPixmap_from_file(filepath, color=None):
img = QXPixmap(str(filepath))
if color is not None:
qp = QPainter(img)
qp.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
qp.fillRect( img.rect(), QColor(color) )
qp.end()
return img
def QXImage_from_file(filepath, color=None):
return QXImage(QPixmap_from_file(filepath, color).toImage())
def QIcon_from_file(filepath, color='black'):
return QIcon(QPixmap_from_file(filepath,color=color))

25
xlib/qt/gui/from_np.py Normal file
View file

@ -0,0 +1,25 @@
import numpy as np
from PyQt6.QtGui import *
from xlib.image import ImageProcessor
def QPixmap_from_np(image : np.ndarray):
ip = ImageProcessor(image).to_uint8()
N,H,W,C = ip.get_dims()
if N > 1:
raise ValueError(f'N dim must be == 1')
if C == 1:
format = QImage.Format.Format_Grayscale8
elif C == 3:
format = QImage.Format.Format_BGR888
elif C == 4:
format = QImage.Format.Format_ARGB32
else:
raise ValueError(f'Unsupported channels {C}')
image = ip.get_image('HWC')
q_image = QImage(image.data, W, H, W*C, format)
q_pixmap = QPixmap.fromImage(q_image)
return q_pixmap