保存 Mipmap 文件

Saving Mipmap files

因此,我几乎完成了创建 mipmap 的程序的构建。它将成功上传您选择的图像,并创建该图像的 mipmap,但现在最后一部分是我卡住的地方。

我现在希望用户能够保存他或她的 mipmap,但我不确定如何保存。我希望他们能够将它保存在他们想要的任何地方,我感觉它需要一个对话框,但我只知道如何在打开项目时实现对话框,而不是保存它们。

到目前为止,这是我的代码:

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.setAlignment(QtCore.Qt.AlignCenter)
        self.origImage.clear()
        global image
        image = QtGui.QFileDialog.getOpenFileName(self,
                                              "Select Image",
                                              "",
                                              "Image File (*.jpg *.png *.gif)")
        global pixmap
        pixmap = QPixmap(image)
        scaledPixmap = pixmap.scaled(self.origImage.size(), Qt.KeepAspectRatio)
        self.origImage.setPixmap(scaledPixmap)
        self.origImage.show()


    def mipmap(self):
        self.mipMap.setAlignment(QtCore.Qt.AlignCenter)
        #Create scaled versions of the source image.
        pixmap = QPixmap(image)
        global mipmaps
        mipmaps = []
        #Version 1 goes up to 1/16 of original size.
        mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 2))
        mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 4))
        mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 8))
        mipmaps.append(pixmap.scaledToWidth(pixmap.width() / 16))
        #Show the first mipmapped version of the image, at 75% label size.
        scaledMipMap = mipmaps[0].scaled(self.mipMap.size() * (3/4), Qt.KeepAspectRatio)
        self.mipMap.setPixmap(scaledMipMap)
        self.mipMap.show()

    def download():
        mipmaps.save('/path/to/file.png', 'PNG')

def main():
    app = QtGui.QApplication(sys.argv)
    form = MyApp()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

到open/save任何文件,有两个步骤:

  1. 获取路径作为字符串,例如"/home/Documents/myImage.png"。获取路径的一种方法是在对话框中询问用户。
    Qt 提供了执行此操作的方法,其中之一是 getSaveFileName()。来自 doc:

    This is a convenience static function that will return a file name selected by the user. The file does not have to exist.

    这个方法returns一个文件名:它不保存任何东西。

  2. 使用路径打开或保存文档
    要加载图像,您使用了 pixmap = QPixmap(imagePath)
    要保存它,您可以使用 pixmap.save(imagePath) (doc)