首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PyPNG绘制一个简单的填充框

使用PyPNG绘制一个简单的填充框
EN

Stack Overflow用户
提问于 2012-06-26 06:08:32
回答 2查看 3.2K关注 0票数 6

我正在尝试使用python的png模块绘制一个简单的256x256像素的RGBA正方形。

我想使用png.Writer函数,我想我必须使用write()方法来提取它。然而,我没有任何运气!我对我当前的代码没有信心,所以我愿意从头开始接受建议

如果可能的话,我不喜欢使用PIL。

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-26 06:46:51

我认为可能影响你的是格式,png似乎有三种格式……

代码语言:javascript
复制
>>> 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) ])

该alpha被附加到每个RGB序列的末尾。

代码语言:javascript
复制
 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.

请注意each row in boxed row flat pixel format.

下面是一个绘制白色正方形的快速示例。

代码语言:javascript
复制
>>> 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还可以使用其他两种格式,这两种格式可能更容易使用。

代码语言:javascript
复制
     |  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)。

代码语言:javascript
复制
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
票数 8
EN

Stack Overflow用户

发布于 2021-05-16 18:49:50

下面是一个创建全红色png文件的简单示例:

代码语言:javascript
复制
import png

width = 255
height = 255
img = []
for y in range(height):
    row = ()
    for x in range(width):
        row = row + (255, 0, 0, 255)
    img.append(row)
with open('red.png', 'wb') as f:
    w = png.Writer(width, height, greyscale=False, alpha='RGBA')
    w.write(f, img)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11198084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档