Using QGIS 3.0 API for layout?
You can simplify this script considerably in QGIS 3.0:
projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()
image_settings = exporter.ImageExportSettings()
image_settings.dpi = 300 # or whatever you want
for comp in projectLayoutManager.printLayouts():
if comp.atlas().enabled():
result, error = QgsLayoutExporter.exportToImage(comp.atlas(),
baseFilePath='c:/temp/my_atlas', extension='.png', settings=image_settings)
if not result == QgsLayoutExporter.Success:
print(error)
To control the generated filenames, you need to set an expression for the atlas filenames, e.g.:
comp.atlas().setFilenameExpression('"some_attribute" || '_export' )
Great, it's gone! thank ndawson.
My new code write atlas result in subdirectories, if can help anyone
import os
folder = "D:\CARTE_THEMATIQUE\OUTPUT"
projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()
for comp in projectLayoutManager.printLayouts():
comp.atlas().setFilenameExpression( "CODE_CENTRE ||'/'|| CODE_CONTRAT" )
image_settings = QgsLayoutExporter(comp.atlas().layout () ).ImageExportSettings()
image_settings.dpi = 100 # or whatever you want
if comp.atlas().enabled():
(filepath, filename) = os.path.split(folder +'\\' + comp.atlas().currentFilename () +'png')
if not os.path.exists(filepath):
os.makedirs(filepath)
result, error = QgsLayoutExporter.exportToImage(comp.atlas(), baseFilePath=filepath, extension='.png', settings=image_settings)
if not result == QgsLayoutExporter.Success:
print(error)