[QGIS-Developer] how to improve the resolution of a png image

SILVIA GAMERO gamerinsky at gmail.com
Thu Jul 5 09:50:10 PDT 2018


Hello everyone,

I need your help.

I have generated an image from a wms, i did it with this code, is a transparent
background image and generates the pgw at the same time. The
background of the image is transparent, because I need to place it on
another layer, the problem is that when making the assembly it looks
choppy and can not be seen clearly

I have also tried this even with the composer of impressions and the
same thing happens.

The loaded layer is a wms.

I'd appreciate your help,

regards,



def run(self):

    dpi = 400

    map_settings = iface.mapCanvas().mapSettings()
    c = QgsComposition(map_settings)

    dpmm = dpi / 25.4
    width = int(dpmm * c.paperWidth())
    height = int(dpmm * c.paperHeight())

    print dpi
    print width, height
    # set page background to transparent
    transparent_fill = QgsFillSymbolV2.createSimple({'outline_style':
'no', 'style': 'no'})
    c.setPageStyleSymbol(transparent_fill)

    # add a map to the composition
    x, y = 0, 0
    w, h = c.paperWidth(), c.paperHeight()

    composerMap = QgsComposerMap(c, x, y, w, h)
    composerMap.setBackgroundEnabled(False)
    c.addItem(composerMap)

    # con este codigo obtengo las ccoordenadas del composer
    composemapextent = composerMap.extent()
    xmin = composemapextent.xMinimum()
    xmax = composemapextent.xMaximum()
    ymin = composemapextent.yMinimum()
    ymax = composemapextent.yMaximum()

    # create output image and initialize it
    image = QImage(QSize(width, height), QImage.Format_ARGB32)
    image.setDotsPerMeterX(dpmm * 1000)
    image.setDotsPerMeterY(dpmm * 1000)
    image.fill(Qt.transparent)
    print "generando imagen"

    imagePainter = QPainter(image)

    c.setPlotStyle(QgsComposition.Print)
    c.renderPage(imagePainter, 0)
    imagePainter.end()

    # self.iface.mapCanvas().saveAsImage('C:\\temp\\output.png', None, 'png')

    image.save('C:\\temp\\output.png', 'png')

    # Line 1: Image spacing in the X direction
    print "generando pgw....."

    # xmin-0, ymin-1, xmax-2, ymax-3
    # resl_x = abs(extent[3] - extent[1]) / data.shape[0]
    resl_x = abs(xmax - xmin) / width
    # Line 2 and 3 are zero! We don't want to rotate the image

    # Line 4: Image spacing in the Y direction (negative)

    resl_y = abs(ymax - ymin) / height * (-1)

    # Line 5: Upper-left corner coordinate in X
    lonO = xmin

    # Line 6: Upper-right corner coordinate in Y
    latN = ymax

    # Format the text, one parameter per line
    navigation = '{}\n{}\n{}\n{}\n{}\n{}'.format(resl_x, 0, 0, resl_y,
lonO, latN)
    print "archivo de navegacion"

    # Write the file with the same file name as the image, and the ".pgw" extent
    open('c:\\temp\\output' + '.pgw', 'w').write(navigation)

    iface.addRasterLayer('c:/temp/output.png', 'output')


More information about the QGIS-Developer mailing list