如何在QGIS导出(pyqgis)中禁用抗锯齿

时间:2018-07-03 19:00:33

标签: python-3.x qgis

我正在尝试通过python代码在QGIS中将打印布局另存为BMP,但想进行抗锯齿,似乎无法弄清楚该怎么做

def saveImage(self, layout, filename="defaultexport", extension=".bmp"):
    """Saves given layout as an image"""
    filefolder = get_save_location()
    filepath = os.path.join(filefolder, filename + extension)

    if not os.path.isdir(filefolder):
        os.makedirs(filefolder)


    exporter = QgsLayoutExporter(layout)
    context = QgsLayoutRenderContext(layout)
    context.setFlag(context.FlagAntialiasing, False)


    export_settings = exporter.ImageExportSettings()
    export_settings.generateWorldFile = False
    export_settings.dpi = 25
    export_settings.flags = context.FlagAntialiasing
    result = exporter.exportToImage(filepath, export_settings)

这就是我所拥有的。我不知道我在用QgsLayoutRenderContext做什么,但是这似乎是唯一的事情。手动保存并在“保存”对话框中打开AA设置可以正常工作,但我需要通过pyqgis来完成

1 个答案:

答案 0 :(得分:0)

修订此项目以了解更多Python和PyQt5知识,这很容易

    exporter = QgsLayoutExporter(layout)
    context = QgsLayoutRenderContext(layout)
    context.setFlag(context.FlagAntialiasing, False)

    export_settings = exporter.ImageExportSettings()
    export_settings.generateWorldFile = False
    export_settings.dpi = 25
    export_settings.flags = context.flags()
    result = exporter.exportToImage(self.filepath, export_settings)

需要使用context.flags()