2012-06-25 16 views
5

Sto provando a disegnare un semplice quadrato RGBA 256x256 pixel usando il modulo png di Python.PyPNG per disegnare una semplice casella riempita

Mi piacerebbe usare la funzione png.Writer e immagino che dovrei disegnarlo usando il metodo write(). Comunque non ho avuto fortuna! Non ho fiducia nel mio codice attuale, quindi sono disposto a prendere suggerimenti da zero

Preferisco non utilizzare il PIL se possibile.

Qualche suggerimento?

risposta

8

penso che il formato è ciò che può essere ti interessano, sembra che png ha tre formati ...

>>> 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) ]) 

L'alfa viene aggiunto alla fine di ogni sequenza RGB.

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. 

nota la each row in boxed row flat pixel format.

Ecco un esempio veloce che disegna un quadrato bianco.

>>> 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) 

nota che Writer possono utilizzare anche gli altri 2 formati, che forse più facile da usare.

 | 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. 

Prova ad utilizzare numpy sue matrici molto più veloce e più facile quando si tratta di operazioni di matrice, le immagini possono essere rappresentati come.

buona fortuna.

Se si desidera stampare i colori, è necessario calcolare i valori RGB per quel colore, ad esempio il colore rosso è (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

Questo è stato estremamente utile, e ho capito meglio le differenze di formato. Tuttavia, non riesco a ottenere l'immagine per la stampa a colori. Ho bisogno di creare una palette per il metodo .Writer? Ho consultato il manuale, ma è un po 'vago. – Layla

+0

@Layla Ho apportato aggiornamenti ... –

+0

@Layla non devi fare nulla per scrivere a colori ma assicurati di aver impostato correttamente i valori RGB per quel colore, ho aggiunto un paio di esempi in più. –

Problemi correlati