我试图通过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来完成
重新审视这个项目,了解更多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()