stamp_preview.md

2023/10/08 categories:Code| tags:Code|

記事に戻る

# -*- coding: utf-8 -*-
import fitz
from PyQt6 import QtCore, QtGui, QtWidgets
from stamp_to_page import stamp_to_page

class StampPreview(QtWidgets.QGroupBox):
    def __init__(self):
        super().__init__()
        self.setTitle('プレビュー')
        self.scene = QtWidgets.QGraphicsScene()
        self.view = QtWidgets.QGraphicsView()
        self.view.setScene(self.scene)
        self.setMaximumSize(235, 235)
        self.setMinimumSize(235, 235)
        self.setLayout( QtWidgets.QHBoxLayout() )
        self.layout().addWidget(self.view)

    def preview(
        self, r, line_width, color, 
        str_u, str_m, str_b, fontsize_u, fontsize_m, fontsize_b
    ):
        doc = fitz.open()
        page = doc.new_page(width=r*2, height=r*2)

        r2 = r - line_width / 2

        stamp_to_page(
            page, r, r, r2, line_width, color, 
            str_u, str_m, str_b, fontsize_u, fontsize_m, fontsize_b
        )
        
        pix = page.get_pixmap(matrix=fitz.Matrix(4, 4))
        byte_array = QtCore.QByteArray( pix.tobytes() )
        qpixmap = QtGui.QPixmap()
        qpixmap.loadFromData(byte_array)
        
        self.scene.clear()
        self.scene.addPixmap(qpixmap)
        self.scene.setSceneRect(0.0, 0.0, float(qpixmap.width()), float(qpixmap.height()))
        self.view.fitInView(0.0, 0.0, float(qpixmap.width()), float(qpixmap.height()))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = StampPreview()
    window.show()
    app.exec()

Share post

Related Posts

コメント