3

I'm a newbie in QGIS and I have to write a small python script to automate shapefile loading, applying style and save image. I'm having trouble on this last item.

Here is the thing:

img.save("render.png","png")
True
img.save("C:\FOLDER\USER\render.jpg","JPG")
False

Where img is a QImage instance. And I can't find the image produced with the relative path.

For a better understanding, here is all the code part concerning image output, which is from QGIS workbook :

img = PyQt4.QtGui.QImage(PyQt4.QtCore.QSize(800, 600), QImage.Format_ARGB32_Premultiplied)

color = QColor(255, 25, 255) img.fill(color.rgb())

p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)

render = QgsMapRenderer()
layer = iface.activeLayer()
lst = [layer.id()]
render.setLayerSet(lst)

rect = QgsRectangle(render.fullExtent())
rect.scale(1.1) render.setExtent(rect)

render.setOutputSize(img.size(), img.logicalDpiX())

render.render(p)

p.end()

img.save("render.png","png")
1

If you're using backslashes, you need to use double backslash to escape the path. In my case:

img.save("render.png","png")
True
img.save("C:\Users\ME\Folder\\render.jpg","JPG")
True

Or you could use single forward slashes:

img.save("C:/Users/ME/Folder/render.jpg","JPG")
True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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