Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect colors when reconstructing an image from arrays with pygame/pyglet
    primarykey
    data
    text
    <p>I wrote up this program that would use numpy and Image(PIL) library to read an image as a bunch of matrices and use pyglet(and opengl) to reconstruct the image.</p> <p>The code using pyglet is as follows:</p> <pre><code>import Image import numpy import window import sys import pyglet import random a=numpy.asarray(Image.open(sys.argv[1])) h,w= a.shape[0],a.shape[1] s=a[0] print s.shape ####################################### def display(): x_a=0;y_a=h for page in a: for array in page: j=array[2] k=array[1] l=array[0] pyglet.gl.glColor3f(l,j,k) pyglet.gl.glVertex2i(x_a,y_a) x_a+=1 y_a-=1 x_a=0 ######################################33 def on_draw(self): global w,h self.clear pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) pyglet.gl.glBegin(pyglet.gl.GL_POINTS) display() pyglet.gl.glEnd() pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png') window.win.on_draw=on_draw ####################################### u=window.win(w,h) pyglet.app.run() </code></pre> <p>The same code modified to use the pygame library (and which is devoid of any opengl usage)</p> <pre><code>import pygame import numpy import Image import sys from pygame import gfxdraw color=(255,255,255) a=numpy.asarray(Image.open(sys.argv[1])) h,w=a.shape[0],a.shape[1] pygame.init() screen = pygame.display.set_mode((w,h)) def uu(): y_a=0 for page in a: x_a=0 for array in page: co=(array[0],array[1],array[2]) pygame.gfxdraw.pixel(screen,x_a,y_a,co) x_a+=1 y_a+=1 uu() done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() </code></pre> <p>The result from pyglet vs. pygame:</p> <p><img src="https://i.stack.imgur.com/kQb5c.png" alt="pyglet vs pygame"></p> <p>So my question is...why is there a problem? Is there a problem with the way I utilised opengl to draw the picture pixel by pixel or there something else that is for now beyond my comprehension?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload