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 8534579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:13:41+00:00 2026-06-11T10:13:41+00:00

I have some RGBA data in Python 3 and I want to display the

  • 0

I have some RGBA data in Python 3 and I want to display the image it represents in a GTK3 window without using any extra libraries.

First thing I tried was editing a Pixbuf‘s data as in an example (http://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html#put-pixel – sorry, I’m only allowed 2 links) in the (C) documentation, using Pixbuf.get_pixels(). This gives me an immutable bytes object, though, and so obviously won’t work. (A gdk-pixbuf bug report (668084, would link to it if SO would let me) covers the same function, but stuff must’ve changed a bit since.)

Next, I tried creating a Pixbuf from the data using Pixbuf.new_from_data(), but this is buggy too (see gdk-pixbuf bug 674691) (yes, that’s me in comment 3).

Then I looked at Cairo: ImageSurface.create_for_data should do this, but for some reason this isn’t available in the Python 3 binding yet. (I have tried this with Python 2, turning the surface into a Pixbuf afterwards and then wrapping that in a gtk.Image, and it works. But I’m using Python3 (and even then, it’s messy – Cairo is a vector graphics library, after all)).

I found a reference somewhere to using PIL to write the image out to a PNG file then reading that into a Pixbuf, but this is horrible, uses an extra library, and PIL isn’t available for Python 3 anyway.

So…any other methods?

Working code

Based on Havok’s answer, here’s a working example:

from array import array

from gi.repository import Gtk as gtk, GdkPixbuf

pixels = array('H')
for i in range(20):
    for j in range(20):
        px = (i < 10, j >= 10, (i < 10) ^ (j < 10))
        pixels.extend(65535 * c for c in px)

header = b'P6 20 20 65535 '
img_data = header + pixels

w = gtk.Window()
w.connect('delete-event', gtk.main_quit)
l = GdkPixbuf.PixbufLoader.new_with_type('pnm')
l.write(img_data)
w.add(gtk.Image.new_from_pixbuf(l.get_pixbuf()))
l.close()
w.show_all()
gtk.main()

Edit: actually, for correctness, I think this will need a pixels.byteswap() on little-endian systems before adding to the header (though of course it doesn’t matter with these colours).

  • 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-11T10:13:42+00:00Added an answer on June 11, 2026 at 10:13 am

    Wow, this will be a hard one, pretty hard.

    Neither PIL or Cairo’s ImageSurface.create_from_data() are available for Python 3. Your only option left is using GdkPixbuf directly, but as you said this is not a perfect solution, in particular because you’re expecting to use the alpha channel. My only thought is that you try to use this:

    http://developer.gnome.org/gdk-pixbuf/stable/GdkPixbufLoader.html#gdk-pixbuf-loader-new-with-type

    As I did at the end of here: Converting PIL GdkPixbuf

    loader = GdkPixbuf.PixbufLoader.new_with_type('pnm')
    loader.write(contents)
    pixbuf = loader.get_pixbuf()
    loader.close()
    

    And try to see what types are supported, could not find the PyGObject version of what the C documentation says about gdk_pixbuf_format_get_name() and gdk_pixbuf_get_formats(). With the pnm format you will not have the alpha channel.

    EDIT

    Great you found the PyGObject functions, posting here the output for documentation purposes:

    >>> from gi.repository import GdkPixbuf
    >>> formats = GdkPixbuf.Pixbuf.get_formats()
    >>> [f.get_name() for f in formats]
    ['GdkPixdata', 'ras', 'tiff', 'wmf', 'icns', 'ico', 'png', 'qtif', 
     'wbmp', 'gif', 'pnm', 'tga', 'ani', 'xbm', 'xpm', 'jpeg2000', 
     'pcx', 'jpeg', 'bmp', 'svg']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some arbitrary pixel data that I want to save as a PNG.
I have some data loaded as a np.ndarray and need to convert it to
Here's my dilemma: I have to RGBA RAW images: a master image (the first)
I have a simple image that stores the width, height, pixelformat, and some other
i am using canvas tag in this to disply some Rect i have used
I currently have some code that draws using pycairo and renders to an SDL
I have a high-resolution image that I want to use as a tiled map
I am using qTip2 on my website but have some problems with the css.
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it
I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .

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.