I want to develop "Print lay out functionality", but in output system is giving me whole image, I want to print only selected polygon. Also it is not displaying label names on print image.
Please suggest some way.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import time
l = iface.activeLayer()
for a in l.getFeatures():
attrs = {16: 1}
l.dataProvider().changeAttributeValues({a.id(): attrs})
img = QImage(QSize(1280, 1024), QImage.Format_ARGB32_Premultiplied)
# set image's background color
color = QColor(255, 255, 255)
img.fill(color.rgb())
# create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
render = QgsMapRenderer()
palyr = QgsPalLayerSettings()
palyr.readFromLayer(l)
lst = [l.id()]
render.setLayerSet(lst)
render.setLabelingEngine(QgsPalLabeling())
# set extent
rect = QgsRectangle(render.fullExtent())
rect.scale(1.1)
render.setExtent(rect)
render.setScale(1)
render.setOutputSize(img.size(), img.logicalDpiX())
render.render(p)
p.end()
# save image
img.save("C:/example/Ashwin/%s.jpg" % a['CHALTA_NO'],"jpg")
attrs = {16: 0}
l.dataProvider().changeAttributeValues({a.id(): attrs})
time.sleep(1)
rect = QgsRectangle(render.fullExtent())
, this uses the entire area. You could use the extent of selection