[Qgis-user] Creating PNG images using QGIS 3 pygis

wambacher at posteo.de wambacher at posteo.de
Tue Mar 5 05:11:01 PST 2019


Some days ago I upgraded from QGIS 2.18 to 3.6.0 - not knowing that many
Python scripts won't run without modifications.

I could convert all of them but not this one:

|fromPyQt4.QtCoreimport*fromPyQt4.QtGuiimport*fromPyQt4importQtGuiimportprocessing,sys,time,psycopg2
importpprint ...creatin layers skipped (got this
running)...################### Create Graph
#####################mapRenderer =iface.mapCanvas().mapRenderer()layers
=[missingLayer.id(),clipLayer.id()]mapRenderer.setLayerSet(layers)c
=QgsComposition(mapRenderer)c.setPlotStyle(QgsComposition.Print)c.setPaperSize(148,90)c.setPrintResolution(300)#
add mapx,y =0,0w,h =c.paperWidth(),c.paperHeight()composerMap
=QgsComposerMap(c,x,y,w,h)c.addItem(composerMap)#output to imagedpi
=c.printResolution()dpmm =dpi /25.4width =int(dpmm
*c.paperWidth())height =int(dpmm
*c.paperHeight())BackgroundColor=QColor(0,0,255)# create output image
and initialize itimage
=QImage(QSize(width,height),QImage.Format_ARGB32)image.setDotsPerMeterX(dpmm
*1000)image.setDotsPerMeterY(dpmm
*1000)image.fill(BackgroundColor)header
=QgsComposerLabel(c)header.setItemPosition(40,1)header.setBackgroundEnabled(0)newFont
=QFont("Comic Sans MS",18)header.setFont(newFont)header.setText("Missing
Countries Map")header.adjustSizeToText()c.addItem(header)header2
=QgsComposerLabel(c)header2.setItemPosition(50,11)header2.setBackgroundEnabled(0)newFont2
=QFont("Comic Sans
MS",10)header2.setFont(newFont2)header2.setText("TS="+ts)header2.adjustSizeToText()c.addItem(header2)footer
=QgsComposerLabel(c)footer.setItemPosition(124,87.5)footer.setBackgroundEnabled(0)newFont3
=QFont("Times
Roman",4)footer.setFont(newFont3)footer.setText(time.ctime())footer.adjustSizeToText()c.addItem(footer)legend
=QgsComposerLegend(c)legend.model().setLayerSet(mapRenderer.layerSet())legend.setItemPosition(5,65)legend.setFrameEnabled(1)newFont
=QFont("Comic Sans
MS",6)legend.setStyleFont(QgsComposerLegendStyle.Title,newFont)legend.setStyleFont(QgsComposerLegendStyle.Subgroup,newFont)legend.setStyleFont(QgsComposerLegendStyle.SymbolLabel,newFont)legend.setStyleMargin(QgsComposerLegendStyle.Title,2)legend.setTitle("Legend")legend.setSymbolHeight(2)legend.adjustBoxSize()c.addItem(legend)#
render the compositionimagePainter
=QPainter(image)imagePainter.setRenderHint(QPainter.Antialiasing)sourceArea
=QRectF(0,0,c.paperWidth(),c.paperHeight())targetArea
=QRectF(0,0,width,height)c.render(imagePainter,targetArea,sourceArea)imagePainter.end()imageFile
="missing_boundaries"+ts+".png"print"saving to "+imageFile
image.save(imageFile) |

Yes, I do know the QqsComposer has gone but I can't find any
documentation or example for QGIS3 plotting. Except the API description
of course.

I don't need the whole script converted - *only a hint how to create a
png the new way.*

regards

walter
**

||

-- 
My projects:

Admin Boundaries of the World <https://wambachers-osm.website/boundaries>
Missing Boundaries
<https://wambachers-osm.website/index.php/projekte/internationale-administrative-grenzen/missing-boundaries>
Emergency Map <https://wambachers-osm.website/emergency>
Postal Code Map (Germany only) <https://wambachers-osm.website/plz>
Fools (QA for zipcodes in Germany) <https://wambachers-osm.website/fools>
Postcode Boundaries of Germany <https://wambachers-osm.website/pcoundaries>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20190305/74dfe8ad/attachment.html>


More information about the Qgis-user mailing list