def make_texture(filename, mipmaps=False): image = pygame.image.load(filename) pixels = pygame.image.tostring(image, "RGBA", True) texture=glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR if mipmaps else GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) if mipmaps: gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA8, image.get_width(), image.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, pixels) else: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, image.get_width(), image.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels) return texture
def main(self): displayservice = Display3D.getDisplayService() self.link((self,"display_signal"), displayservice) self.send(self.disprequest, "display_signal"); # load texture if self.tex is not None: # load image image = pygame.image.load(self.tex) # create power of 2 dimensioned surface pow2size = (int(2**(ceil(log(image.get_width(), 2)))), int(2**(ceil(log(image.get_height(), 2))))) if pow2size != image.get_size(): textureSurface = pygame.Surface(pow2size, pygame.SRCALPHA, 32) # determine texture coordinates self.tex_w = float(image.get_width())/pow2size[0] self.tex_h = float(image.get_height())/pow2size[1] # copy image data to pow2surface textureSurface.blit(image, (0,0)) else: textureSurface = image self.tex_w = 1.0 self.tex_h = 1.0 # set plane size self.size = Vector(float(image.get_width())/100.0, float(image.get_height())/100.0, 0) # prepare vertices for intersection test x = float(self.size.x/2) y = float(self.size.y/2) self.vertices = [ Vector(-x, y, 0.0), Vector(x, y, 0.0), Vector(x, -y, 0.0), Vector(-x, -y, 0.0) ] # read pixel data textureData = pygame.image.tostring(textureSurface, "RGBX", 1) # gen tex name self.texID = glGenTextures(1) # create texture glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texID) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData ); glDisable(GL_TEXTURE_2D) while 1: # for _ in self.waitBox("callback"): yield 1 # self.display = self.recv("callback") # There is no need for a callback yet yield 1 self.handleEvents() self.handleMovementCommands() self.applyTransforms() self.draw()
def convertToImageString(self, image): #print "PygameDevice.convertToImageString:", image ix = image.get_width() iy = image.get_height() data = pygame.image.tostring(image, "RGBA", 1) #print data return (data, ix, iy)
def texture_sub_image(texid, place, image, dimensions=2): '''Specify a sub-part of a texture with an image. place - tuple of coords - the point within the texture of the image's lower-left (see OpenGL) coords''' format, type, pixels = format_type_array_from_image(image) size = [image.get_width(), image.get_height(), 0][:dimensions] texture_sub_data(texid, place, size, format, type, pixels.ctypes.data)
def crop_center(image, size): rect = Rect((0, 0), size) res_surf = Surface(size, SRCALPHA, 32) blit_pos = [0, 0] if image.get_width() > rect.width: blit_pos[0] = (-image.get_width()+rect.width) // 2 else: rect.x = (image.get_width()-rect.width) // 2 if image.get_height() > rect.height: blit_pos[1] = (-image.get_height()+rect.height) // 2 else: rect.y = (image.get_height()-rect.height) // 2 print(rect) print(blit_pos) res_surf.blit(crop(image, rect), blit_pos) return res_surf
def vscroll(scrollval, image): offs = image.get_height() newimage = image.copy() newimage.fill((0, 0, 0, 0)) newimage.blit(image, (0, scrollval)) if (str(scrollval))[0] == "-": newimage.blit(image, (0, (scrollval + offs))) else: newimage.blit(image, (0, (scrollval - offs))) return newimage
def texture_from_image(image, internalformat=None, dimensions=2): '''Create and return a new texture id and initialize its data with the given image. image - a pygame Surface. internalformat - the texture's internal format; if None (the default) will be set to an appropriate one.''' format, type, pixels = format_type_array_from_image(image) if internalformat is None: internalformat = format size = [image.get_width(), image.get_height(), 0][:dimensions] return texture_from_data(internalformat, size, format, type, pixels.ctypes.data)
def init_sprite (self, screen, x, y, image, a=0, static=0): """ Arguments: screen -- the screen on which the sprite should appear. x -- the x-coordinate of the centre of the image. y -- the y-coordinate of the centre of the image. image -- the image object, as returned from the load_image function. a -- the angle through which the image should be rotated. """ Object.__init__ (self, screen, x, y, image, a, static=static, x_offset=-image.get_width()/2, y_offset=-image.get_height()/2)
def make_texture( filename=None, image=None, interpolate=True, alpha=False, integer=False, maxlod=None): if image == None: image = pygame.image.load(filename) pixels = pygame.image.tostring(image, "RGBA" if alpha else "RGB", True) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST if interpolate else GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) if maxlod is not None: glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, maxlod) if alpha: if integer: targetformat = GL_RGBA8UI sourceformat = GL_RGBA_INTEGER else: targetformat = GL_RGBA8 sourceformat = GL_RGBA else: if integer: targetformat = GL_RGB8UI sourceformat = GL_RGB_INTEGER else: targetformat = GL_RGB8 sourceformat = GL_RGB glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE) glTexImage2D( GL_TEXTURE_2D, 0, targetformat, image.get_width(), image.get_height(), 0, sourceformat, GL_UNSIGNED_BYTE, pixels) print glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT) return texture
def make_texture(filename): image = pygame.image.load(filename) pixels = pygame.image.tostring(image, "RGB", True) texture=glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8, image.get_width(), image.get_height(), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels) return texture
def changeToGrey(filename): import Image from pygame import image import PIL # Recuperation resolution image = image.load(filename) resolution = (image.get_width(), image.get_height()) # Modification pixels img = Image.open(filename) pix = img.load() for i in range(resolution[0]): for j in range(resolution[1]): gris = int(round(0.299*pix[i,j][0]+0.587*pix[i,j][1]+0.114*pix[i,j][2])) pix[i,j] = (gris,gris,gris) img.save(filename)
def render(self): oldrect = self.rect cursorsize = 1 start = 0 while self.font.size(self.text[start:self.cursor+5])[0] > self.width: start = start + 1 if self.text: image = self.font.render(self.text[start:], 1, self.color, self.bgcolor) self.image = pygame.Surface((image.get_width() + cursorsize, image.get_height())) self.image.blit(image, (0, 0)) # Render the cursor cursor_pos = self.font.size(self.text[start:self.cursor])[0] cursor_height = self.image.get_height() self.image.fill((0,255,0), (cursor_pos, 0, cursorsize, cursor_height)) else: self.image = pygame.Surface((0, 0)) self.rect = Rect(self.pos, self.image.get_size()) return oldrect.union(self.rect)
def make_texture(filename=None, image=None, interpolate=True, alpha=False): if image == None: image = pygame.image.load(filename) pixels = pygame.image.tostring(image, "RGBA" if alpha else "RGB", True) texture=glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR if interpolate else GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR if interpolate else GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8 if alpha else GL_RGB8, image.get_width(), image.get_height(), 0, GL_RGBA if alpha else GL_RGB, GL_UNSIGNED_BYTE, pixels) return texture
from pygame import surfarray, image, display import pygame pygame.init() image = image.load('img.png') resolution = (image.get_width(), image.get_height()) screen = display.set_mode(resolution) screen.blit(image,(0,0)) surfarray.use_arraytype("numpy") screenpix = surfarray.pixels3d(image) data = [] data_str = '' for y in range(resolution[1]): for x in range(resolution[0]): red = screenpix[x][y][0] green = screenpix[x][y][1] blue = screenpix[x][y][2] data.append([red / 255.0 , green / 255.0 ,blue / 255.0]) data_str += str([red / 255.0 , green / 255.0 ,blue / 255.0]) + ',' + '\n' with open('rgb.js', 'w') as f: f.write('var labels = [' + data_str + '];') #[[1,1,1],[0,0,0],...]
def drawImage(self, image, pos): #print "drawImage:", self.graphicsDevice.screen, image, pos rect = (pos[0], pos[1], image.get_width(), image.get_height()) self.graphicsDevice.drawImage(self.graphicsDevice.screen, image, rect)
def main(self): displayservice = Display3D.getDisplayService() self.link((self, "display_signal"), displayservice) self.send(self.disprequest, "display_signal") # load texture if self.tex is not None: # load image image = pygame.image.load(self.tex) # create power of 2 dimensioned surface pow2size = (int(2**(ceil(log(image.get_width(), 2)))), int(2**(ceil(log(image.get_height(), 2))))) if pow2size != image.get_size(): textureSurface = pygame.Surface(pow2size, pygame.SRCALPHA, 32) # determine texture coordinates self.tex_w = float(image.get_width()) / pow2size[0] self.tex_h = float(image.get_height()) / pow2size[1] # copy image data to pow2surface textureSurface.blit(image, (0, 0)) else: textureSurface = image self.tex_w = 1.0 self.tex_h = 1.0 # set plane size self.size = Vector( float(image.get_width()) / 100.0, float(image.get_height()) / 100.0, 0) # prepare vertices for intersection test x = float(self.size.x / 2) y = float(self.size.y / 2) self.vertices = [ Vector(-x, y, 0.0), Vector(x, y, 0.0), Vector(x, -y, 0.0), Vector(-x, -y, 0.0) ] # read pixel data textureData = pygame.image.tostring(textureSurface, "RGBX", 1) # gen tex name self.texID = glGenTextures(1) # create texture glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texID) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) glDisable(GL_TEXTURE_2D) while 1: # for _ in self.waitBox("callback"): yield 1 # self.display = self.recv("callback") # There is no need for a callback yet yield 1 self.handleEvents() self.handleMovementCommands() self.applyTransforms() self.draw()
def transparentHack(bg, image, location, opacity): temp = pygame.Surface((image.get_width(), image.get_height())) temp.blit(bg, (-location[0], -location[1])) temp.blit(image, (0, 0)) temp.set_alpha(opacity) bg.blit(temp, location)