2013-03-09 64 views
1

我寫下了這個程序,它將使用numpy和Image(PIL)庫來讀取圖像作爲一組矩陣,並使用pyglet(和opengl)重構圖像。用pygame/pyglet從數組重建圖像時顏色不正確

使用pyglet的代碼如下:

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

修改爲使用pygame的庫中的相同的代碼(和它是沒有任何的OpenGL用法)

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

從pyglet VS結果。pygame的:

pyglet vs pygame

所以我的問題1上是...爲什麼有問題?我用opengl如何逐像素地繪製圖片還是出現了一些現在超出我理解範圍的問題,是否存在問題?

+0

您不解釋什麼不起作用。你是否期待它將圖像作爲pyglet中的普通紋理使用? – ninMonkey 2013-03-09 16:34:26

+0

好吧,一個非常糟糕的圖像形式出現,而不是一個完美的..我會張貼原始圖片和上述代碼輸出..但因爲我是一個沒有排名或聲譽的新手,我不能這樣做。 – 2013-03-09 17:06:47

+0

您可以在imgur上發佈圖片,並將其鏈接。你在使用pygame和PIL和pyglet嗎?我沒有看到一個導入,但你被標記爲pygame,所以我很困惑。 - 我確實在尋找你,但我找不到具體的帖子。有一種方法可以將pygame曲面加載爲opengl(pyglet)紋理。 – ninMonkey 2013-03-09 18:11:54

回答

1

Pygame.Color期望整數範圍在0-255之間,而pyglet.gl.glColor3f期望浮動範圍在0.0-1.0範圍內。像這樣的轉換應該可以解決您的問題:

j=array[0]/255.0 
k=array[1]/255.0 
l=array[2]/255.0 
pyglet.gl.glColor3f(j,k,l) 
+0

啊,我明白了。謝謝你的答案,並感謝你的編輯。 – 2013-03-22 20:07:29