Edit: the problem was merely a typo in the coordinates (see comment on definition of p). Otherwise the code below should work for other situations, too, if adapted properly. The weird pixelated/grey/colorful images arise, when the rendered coordinates do not lie on the layer extend.

I have tried to adopt some code from the PyQGISDeveloperCookbook (p.37) to render a QGIS raster layer to a png image file (see bottom for layer's metadata). One thing that I have tried to change is to modify the rendered extent to a custom bounding box bbox centered in a given point p:

    from PyQt4.QtGui import QImage, QPainter
    from PyQt4.QtCore import QSize
    import os

    # raster layer in CRS UTM 33N (epsg 25833)
    rlayer = iface.activeLayer()
    # radius for image in meters
    radius = 50
    # center point 
    p = [33401477,5826362] # <- should be 334014.77 for x-coordinate
    # bounding box
    bbox = QgsRectangle(p[0]-radius, p[1]-radius, p[0]+radius, p[1]+radius)
    # image
    img = QImage(QSize(200,200), QImage.Format_ARGB32_Premultiplied)
    ## create painter
    painter = QPainter()
    render = QgsMapRenderer()
    ## set layer set
    lst = [rlayer.id()] 
    ## set extent
    ## set output size
    render.setOutputSize(img.size(), img.logicalDpiX())
    ## do the rendering
    ## save image
    img.save("D:\\Daten\\QGIStmp\\bbox1.png", "png")

The resulting image does not really look the way I expected (though interesting :-)):


I tried hard (generating more interesting images), but I couldn't figure out what the problem could be.

Querying Layer Metadata:

>>> rlayer.metadata()


    u'<p class="glossy">Treiber</p>\n<p>GDAL provider</p>\nECW<br>ERDAS Compressed Wavelets (SDK 5.0)<p class="glossy">Datensatzbeschreibung</p>\n<p>D:/Daten/QGIStmp/Nordost/dop20_400_5826.ecw</p>\n<tr><p>\nCOLORSPACE=RGB</p>\n<p>\nCOMPRESSION_RATE_TARGET=20</p>\n<p>\nVERSION=2</p>\n</tr><p class="glossy">Kanal 1</p>\n<p class="glossy">Kanal 2</p>\n<p class="glossy">Kanal 3</p>\n<p class="glossy">Dimensionen</p>\n<p>X: 10000 Y: 10000 Kan\xe4le: 3</p>\n<p>X : 5000,Y 5000</p><p>X : 2500,Y 2500</p><p>X : 1250,Y 1250</p><p>X : 625,Y 625</p><p>X : 312,Y 312</p><p>X : 156,Y 156</p><p class="glossy">Ursprung</p>\n<p>400000,5.828e+06</p>\n<p class="glossy">Pixelgr\xf6\xdfe</p>\n<p>0.2,-0.2</p>\n<p class="glossy">Leerwert</p>\n<p>*Leerwert nicht gesetzt*</p>\n</p>\n<p class="glossy">Datentyp</p>\n<p>Byte - Acht Bit vorzeichenlose Ganzzahl</p>\n<p class="glossy">Pyramiden\xfcbersichten</p>\n<p><p class="glossy">R\xe4umliches Bezugssystem des Layers</p>\n<p>+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs</p>\n<p class="glossy">Layerausdehnung (in ursp\xfcnglicher Projektion des Layers)</p>\n<p>400000.0000000000000000,5826000.0000000000000000 : 402000.0000000000000000,5828000.0000000000000000</p>\n<p class="glossy">\nKanal</p>\n<p>Kanal 1</p>\n<p>Kanal Nr</p>\n<p>\n1</p>\n<p>Keine Statistik</p>\n<p>\nNoch keine Statistik gesammelt</p>\n<p class="glossy">\nKanal</p>\n<p>Kanal 2</p>\n<p>Kanal Nr</p>\n<p>\n2</p>\n<p>Keine Statistik</p>\n<p>\nNoch keine Statistik gesammelt</p>\n<p class="glossy">\nKanal</p>\n<p>Kanal 3</p>\n<p>Kanal Nr</p>\n<p>\n3</p>\n<p>Keine Statistik</p>\n<p>\nNoch keine Statistik gesammelt</p>\n'

1 Answer 1


Your code is ok, but there is probably a typo in the p list. The first item value seems too big for the location you are working on (I generally work using CRS UTM 32N): probably the last digits of 33401477 are decimals.

If I use other values for p:

p = [318333,4993348]

on this sample raster (CRS UTM 32N, EPSG:32632):

enter image description here

I get a result without any problem (I don't know if it is the desired result).

enter image description here

  • 1
    That's it. Thanks for the sharp look! (I was beginning to dive into the QGIS-API, which keeps things rather brief in many places. So this really saved me a lot of time!)
    – Leolo
    Commented Mar 28, 2017 at 8:52
  • @Leolo I'm glad it worked also for you and, actually, you wrote a very good code for a beginner!
    – mgri
    Commented Mar 28, 2017 at 9:09
  • As I said, I mainly copied it from the "Cookbook"...
    – Leolo
    Commented Mar 28, 2017 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.