Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:18:14+00:00 2026-06-06T09:18:14+00:00

I am trying to draw a simple 256×256 pixel RGBA square using python’s png

  • 0

I am trying to draw a simple 256×256 pixel RGBA square using python’s png module.

I’d like to use the png.Writer function and I imagine I’d have to draw it out using the write() method. I have not had any luck however! I don’t have faith in my current code so I’m willing to take suggestions from scratch

I prefer not to use the PIL if possible.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:18:16+00:00Added an answer on June 6, 2026 at 9:18 am

    I think the format is what may be affecting you, it seems that png has three formats …

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

    The alpha being appended to the end of each RGB sequence.

     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.
    

    note the each row in boxed row flat pixel format.

    heres a quick example that draws a white square.

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

    note that Writer can also use the other 2 formats which maybe easier to use.

         |  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 to use numpy its much faster and easier when dealing with matrix operations, images can be represented as matrices.

    good luck.

    If you want to print colors then you would need to calculate the RGB values for that color, for example the color red is (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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write simple graphic editor using PyGObject and python 3. I need
I'm trying to draw a simple circle using OpenGL ES. The problem is that
I'm trying to just draw a simple image from the drawable-mdpi folder in android
Currently I'm trying to do what I thought would be a simple task: Draw
I'm trying to draw some simples lines with the iPhone/Touch SDK. I'd like to
I am simply trying to draw a rectangle inside of a panel using flex4.
I'm trying to draw a square in XNA and simply move it around with
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw a simple box in my iPhone game. Here is the
I'm trying to create a simple window in Win32 and draw a rectangle in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.