私は、単純な256x256ピクセルのRGBA四角形を、pythonのpngモジュールを使って描画しようとしています。PyPNGで単純な塗りつぶしのボックスを描く
私はpng.Writer関数を使いたいと思います。私はwrite()メソッドを使ってそれを描画しなければならないと思います。私は運がなかった!私は現在のコードを信用していないので、私は一からの提案を喜んで受けています
私は可能な限りPILを使用しないことを好みます。
提案がありますか?
私は、単純な256x256ピクセルのRGBA四角形を、pythonのpngモジュールを使って描画しようとしています。PyPNGで単純な塗りつぶしのボックスを描く
私はpng.Writer関数を使いたいと思います。私はwrite()メソッドを使ってそれを描画しなければならないと思います。私は運がなかった!私は現在のコードを信用していないので、私は一からの提案を喜んで受けています
私は可能な限りPILを使用しないことを好みます。
提案がありますか?
私はフォーマットがあなたに影響を与えることができるものをと思い、png
は3つの形式...
>>> 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) ])
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.
ノートをHERES each row in boxed row flat pixel format.
白い四角形を描画簡単な例。
>>> 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)
Writer
も使い多分簡単に他の2つの形式を使用できることに注意してください。
| 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.
行列演算を扱うときに非常に迅速かつ容易に、画像を行列として表現することができますをnumpy
使用してみてください。
幸運。
カラーを印刷する場合は、そのカラーのRGB値を計算する必要があります。たとえば、赤は(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
これは非常に役に立ち、フォーマットの違いをよく理解しています。しかし、イメージをカラーで印刷することはできません。 .Writerメソッドのパレットを作成する必要がありますか?私はマニュアルを参照しましたが、少し曖昧です。 – Layla
@Layla更新しました... –
@Layla色で書き出すには何もする必要はありませんが、その色のRGB値を正しく設定していることを確認してください。 –