2012-06-25 9 views
5

Ich versuche, ein einfaches 256x256 Pixel RGBA-Quadrat mit Python Png-Modul zu zeichnen.PyPNG zum Zeichnen einer einfachen gefüllten Box

Ich würde gerne die png.Writer-Funktion verwenden und ich stelle mir vor, ich müsste es mit der write() -Methode zeichnen. Ich hatte jedoch kein Glück! Ich habe kein Vertrauen in meinen aktuellen Code, also bin ich bereit, Vorschläge von Grund auf neu zu nehmen

Ich bevorzuge es, die PIL wenn möglich zu verwenden.

Irgendwelche Vorschläge?

Antwort

8

Ich denke, das Format ist, was Sie können beeinflussen, wie es scheint, dass png drei Formate hat ...

>>> help(png) 
    Boxed row flat pixel:: 

    list([R,G,B, R,G,B, R,G,B], 
     [R,G,B, R,G,B, R,G,B]) 

    Flat row flat pixel::  

     [R,G,B, R,G,B, R,G,B, 
     R,G,B, R,G,B, R,G,B] 

    Boxed row boxed pixel:: 

     list([ (R,G,B), (R,G,B), (R,G,B) ], 
      [ (R,G,B), (R,G,B), (R,G,B) ]) 

Die alpha-Stellung zum Ende jedes RGB-Sequenz angehängt wird.

write(self, outfile, rows) 
|  Write a PNG image to the output file. `rows` should be 
|  an iterable that yields each row in boxed row flat pixel format. 
|  The rows should be the rows of the original image, so there 
|  should be ``self.height`` rows of ``self.width * self.planes`` values. 
|  If `interlace` is specified (when creating the instance), then 
|  an interlaced PNG file will be written. Supply the rows in the 
|  normal image order; the interlacing is carried out internally. 

Anmerkung des each row in boxed row flat pixel format.

here ein kurzes Beispiel, das ein weißes Quadrat gezeichnet.

>>> rows = [[255 for element in xrange(4) for number_of_pixles in xrange(256)] for number_of_rows in xrange(256)] 
>>> import numpy # Using numpy is much faster 
>>> rows = numpy.zeros((256, 256 * 4), dtype = 'int') 
>>> rows[:] = 255 
>>> png_writer = png.Writer(width = 256, height = 256, alpha = 'RGBA') 
>>> png_writer.write(open('white_panel.png', 'wb'), rows) 

beachten Sie, dass Writer auch die anderen zwei Formate verwenden können, die vielleicht einfacher zu bedienen.

 | write_array(self, outfile, pixels) 
    |  Write an array in flat row flat pixel format as a PNG file on 
    |  the output file. See also :meth:`write` method. 
    | 
    | write_packed(self, outfile, rows) 
    |  Write PNG file to `outfile`. The pixel data comes from `rows` 
    |  which should be in boxed row packed format. Each row should be 
    |  a sequence of packed bytes. 

Try numpy seine viel schneller und einfacher zu verwenden, wenn mit Matrix-Operationen zu tun, können die Bilder als Matrizen dargestellt werden.

viel Glück.

Wenn Sie Farben drucken möchten, müssten Sie die RGB-Werte für diese Farbe berechnen, z. B. die Farbe Rot (255, 0, 0, 255).

import png 
import numpy 
rows = numpy.zeros((256, 256, 4), dtype = 'int') # eassier format to deal with each individual pixel 
rows[:, :] = [255, 0, 0, 255] # Setting the color red for each pixel 
rows[10:40, 10:40] = [0, 255, 255, 255] # filled squared starting at (10,10) to (40,40) 
locs = numpy.indices(rows.shape[0:2]) 
rows[(locs[0] - 80)**2 + (locs[1] - 80)**2 <= 20**2] = [255, 255, 0, 255] # yellow filled circle, with center at (80, 80) and radius 20 
png_writer = png.Writer(width = 256, height = 256, alpha = 'RGBA') # create writer 
png_writer.write(open('colors_panel.png', 'wb'), rows.reshape(rows.shape[0], rows.shape[1]*rows.shape[2])) # we have to reshape or flatten the most inner arrays so write can properly understand the format 
+0

Dies war sehr hilfreich, und ich verstehe besser die Formatunterschiede. Ich kann jedoch nicht scheinen, dass das Bild in Farbe gedruckt wird. Benötige ich eine Palette für die .Writer-Methode? Ich habe das Handbuch konsultiert, aber es ist ein bisschen vage. – Layla

+0

@Layla Ich habe Updates gemacht ... –

+0

@Layla Sie müssen nichts tun, um in Farbe zu schreiben, aber stellen Sie sicher, dass Sie die RGB-Werte für diese Farbe richtig einstellen, habe ich ein paar zusätzliche Beispiele hinzugefügt. –

Verwandte Themen