如何以全分辨率打印无边界的图像

时间:2019-05-27 05:48:33

标签: python python-3.x pyqt5

在我的桌面上有一个带有一些图像的文件夹,还有一个通过USB电缆连接到我的笔记本电脑的佳能打印机。我有一些代码可以向用户显示图片,并让他选择其中一张,现在应自动打印这些图片(格式和所有其他设置均已修复)。使用mFoxRU的打印代码,我可以打印标签,但不能以完整分辨率或正确格式打印标签(我想打印5“ x7”,但打印机始终使用DinA5纸)。另外,我想无边框打印。我该如何改变?一些代码:

from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtGui import QIcon, QPixmap

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(1920, 1080)
        self.Frame = QtWidgets.QWidget(MainWindow)

        self.Bild1 = QtWidgets.QLabel(self.Frame)
        self.Bild1.setGeometry(QtCore.QRect(20, 45, 600, 400))
        pixmap1 = QPixmap('image1.JPG').scaled(600, 400)
        self.Bild1.setPixmap(pixmap1)

        self.Bild2 = QtWidgets.QLabel(self.Frame)
        self.Bild2.setGeometry(QtCore.QRect(660, 45, 600, 400))
        pixmap2 = QPixmap('image2.JPG').scaled(600, 400)
        self.Bild2.setPixmap(pixmap2)

        self.Bild3 = QtWidgets.QLabel(self.Frame)
        self.Bild3.setGeometry(QtCore.QRect(1300, 45, 600, 400))
        pixmap3 = QPixmap('image3.JPG').scaled(600, 400)
        self.Bild3.setPixmap(pixmap3)

        self.Bild4 = QtWidgets.QLabel(self.Frame)
        self.Bild4.setGeometry(QtCore.QRect(20, 565, 600, 400))
        pixmap4 = QPixmap('image4.JPG').scaled(600, 400)
        self.Bild4.setPixmap(pixmap4)

        self.Bild5 = QtWidgets.QLabel(self.Frame)
        self.Bild5.setGeometry(QtCore.QRect(660, 565, 600, 400))
        pixmap5 = QPixmap('image5.JPG').scaled(600, 400)
        self.Bild5.setPixmap(pixmap5)

        self.Bild6 = QtWidgets.QLabel(self.Frame)
        self.Bild6.setGeometry(QtCore.QRect(1300, 565, 600, 400))
        pixmap6 = QPixmap('image6.JPG').scaled(600, 400)
        self.Bild6.setPixmap(pixmap6)

        self.WeiterButton = QtWidgets.QPushButton(self.Frame)
        self.WeiterButton.setGeometry(QtCore.QRect(1825, 1008, 75, 24))
        MainWindow.setCentralWidget(self.Frame)
        self.WeiterButton.clicked.connect(self.print_widget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Fotobox"))
        self.WeiterButton.setText(_translate("MainWindow", "Print"))

    def print_widget(self):
        # Create printer
        printer = QtPrintSupport.QPrinter()
        # Create painter
        painter = QtGui.QPainter()
        # Start painter
        painter.begin(printer)
        # Grab a widget you want to print
        screen = self.Bild1.grab()
        # Draw grabbed pixmap
        painter.drawPixmap(0, 0, screen)
        # End painting
        painter.end()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

0 个答案:

没有答案