Pygame,从二维 numpy 数组创建灰度 [英] Pygame, create grayscale from 2d numpy array

查看:76
本文介绍了Pygame,从二维 numpy 数组创建灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4,pygame==1.9.2b8

Python 3.4, pygame==1.9.2b8

我想绘制灰度框架.现在,下面的代码生成蓝色,但我想在 (0,255) 范围内生成颜色,其中 0 - 黑色.255 - 白色.怎么可能?!

I want to draw grayscale frame. Now, code below produce blue color, but I want to make color in range (0,255), where 0 - black. 255 -white. How is it possible?!

import pygame 
import numpy as np
s = 300
screen = pygame.display.set_mode((s, s))
screenarray = np.zeros((s,s))
screenarray.fill(200)
pygame.surfarray.blit_array(screen, screenarray)
pygame.display.flip()
input()

  • 事实上,我有更复杂的screenarray,其中每个元素位于 (0,65535) 期间.所以我想把它转换成灰度.
    • in fact I have more complex screenarray, where each element lie during (0,65535). So I want to convert It to grayscale.
    • 非常感谢.

      推荐答案

      PyGame 使用 24 位颜色作为三个字节 (R,G,B) 所以白色是 (0,0,0),黑色是 (255,255,255)

      PyGame use 24-bit colors as three bytes (R,G,B) so white is (0,0,0) and black is (255,255,255)

      import pygame 
      import numpy as np
      
      SIZE = 256
      
      pygame.init()
      screen = pygame.display.set_mode((SIZE, SIZE))
      
      screenarray = np.zeros((SIZE, SIZE, 3))
      
      for x in range(SIZE):
          screenarray[x].fill(x)
      
      pygame.surfarray.blit_array(screen, screenarray)
      
      pygame.display.flip()
      
      input()
      
      pygame.quit()
      

      这篇关于Pygame,从二维 numpy 数组创建灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆