Python中的Mipmapping?

2024-10-01 02:21:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用QtCreator和Python构建一个mipmapping程序。到目前为止,我的应用程序如下所示:

Design

在这里你可以上传一个图像到程序,它将显示你的原始图像在左标签框。按“创建mipmap”时,它应该会在右侧标签框中显示mipmap。在

上传照片时,看起来是这样的: Program

下一个部分是我被卡住的地方。在Python中有mipmapping算法可以使用吗?我看到这个question on StackOverflow有点帮助,但是我不能安装numpy,因为它不能识别我的python3.4,尽管它已经安装了。在

以下是我目前所掌握的:

from __future__ import division
from PyQt4 import QtCore, QtGui, QtOpenGL
from PyQt4.QtGui import * #Used to import QPixmap. DO NOT REMOVE.
from PyQt4.QtCore import * #Used to import Qt.KeepAspectRation. DO NOT REMOVE.
import sys, os
import mmCreator



class MyApp(QtGui.QMainWindow, mmCreator.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)
        self.setupUi(self)
        self.btnSelect.clicked.connect(self.select_image)
        self.btnConvert.clicked.connect(self.mipmap)
        #self.btnDownload.clicked.connect(self.download)


    def select_image(self):
        self.origImage.clear()
        image = QtGui.QFileDialog.getOpenFileName(self,
                                              "Select Image",
                                              "",
                                              "Image File (*.jpg *.png *.gif)")
        pixmap = QPixmap(image)
        scaledPixmap = pixmap.scaled(self.origImage.size(), Qt.KeepAspectRatio)
        self.origImage.setPixmap(scaledPixmap)
        self.origImage.show()


    #def mipmap():

那么,除了numpy之外,有没有其他方法可以在我的程序中实现mipmap呢?在

感谢任何帮助。在


Tags: from图像imageimportself程序defconnect
1条回答
网友
1楼 · 发布于 2024-10-01 02:21:10

您可以使用QPixmap.scaled创建半宽或半高的图像。在

# Create pixmap from source file
pixmap = QPixmap('/path/to/file')

# Create scaled versions of source file.
mipmaps = []
mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 2))
mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 4))

# Merge scaled images into a single image
w = sum([m.width() for m in mipmaps])
h = max([m.height() for m in mipmaps])
merged_pixmap = QPixmap(w, h)
painter = QPainter(merged_pixmap)

x = 0
for mipmap in mipmaps:
    painter.drawPixmap(x, 0, mipmap)
    x += mipmap.width()

# Save file.
merged_pixmap.save('/path/to/file.png', 'PNG')

相关问题 更多 >