QGIS Python (PyQGIS) - Render map layer as an image

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ม.ค. 2025

ความคิดเห็น • 7

  • @krishnaglodha
    @krishnaglodha 5 ปีที่แล้ว +5

    Great tutorial!! Here is the final script developed in this video
    img = QImage(QSize(800,800), QImage.Format_ARGB32_Premultiplied)
    #set backgroundcolor
    color = QColor(255,255,255,255)
    img.fill(color.rgba())
    #create painter
    p= QPainter()
    p.begin(img)
    p.setRenderHint(QPainter.Antialiasing)
    #the mapsettings
    ms= QgsMapSettings()
    ms.setBackgroundColor(color)
    #set layers to render
    layer = QgsProject().instance().mapLayersByName('214')
    ms.setLayers([layer[0]])
    #set Extent
    rect = QgsRectangle(ms.fullExtent())
    ms.setExtent(rect)
    #size of output image
    ms.setOutputSize(img.size())
    #render map
    render = QgsMapRendererCustomPainterJob(ms, p)
    render.start()
    render.waitForFinished()
    p.end()
    #save the image
    img.save('C:/Users/Desktop/krishna/render.png')

  • @socalClutch
    @socalClutch 2 ปีที่แล้ว

    How could one take this code and set the extents using a 2nd layer such as a shapefile I call "mask"? Can I just refer to the layer's name or must I get the actual extents from the layer first? Tried many was to use ms.setExtent() but with no success....help!

  • @MrAsabust
    @MrAsabust 2 ปีที่แล้ว

    how to export by changing only the display scales?

  • @DerMattinger
    @DerMattinger 5 ปีที่แล้ว

    How can I export multiple layers (with different names?) Or is there a way to export whole groups?

    • @geospatialschool
      @geospatialschool  5 ปีที่แล้ว

      You can do what I've done here, just remove the current layer, add the new layer, and save it with a different name.

    • @nos540
      @nos540 4 ปีที่แล้ว

      @@geospatialschool A for cycle could be a possibility?

    • @adithaiddamlgoda1513
      @adithaiddamlgoda1513 3 ปีที่แล้ว

      @@nos540 Did you get the answer for this?