Reputation: 539
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.
The code using pyglet is as follows:
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()
The same code modified to use the pygame library (and which is devoid of any opengl usage)
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()
The result from pyglet vs. pygame:
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?
Upvotes: 1
Views: 756
Reputation: 5161
Pygame.Color
expects integers in the range 0-255, while pyglet.gl.glColor3f
expects floats in the range 0.0-1.0. A conversion like this should fix your problem:
j=array[0] / 255.0
k=array[1] / 255.0
l=array[2] / 255.0
pyglet.gl.glColor3f(j,k,l)
Upvotes: 1