def create_red_image(self, img): new_img = QImage(img) for x in range(0, img.width()): for y in range(0, img.height()): pix = img.pixel(x, y) r = pix & 0xFF g = (pix >> 8) & 0xFF b = (pix >> 16) & 0xFF alpha_mask = pix & 0xFF000000 new_img.setPixel(x, y, (alpha_mask | ((r + g + b) / 3))) return new_img
def create_red_image(self, img): new_img = QImage(img) for x in range(0, img.width()): for y in range(0, img.height()): pix = img.pixel(x, y) r = pix & 0xFF g = (pix >> 8) & 0xFF b = (pix >> 16) & 0xFF alpha_mask = pix & 0xFF000000 new_img.setPixel(x, y, (alpha_mask | ((r + g + b) / 3))) return new_img
class SetPixelFloat(UsesQApplication): '''Test case for calling setPixel with float as argument''' def setUp(self): #Acquire resources super(SetPixelFloat, self).setUp() self.color = qRgb(255, 0, 0) self.image = QImage(200, 200, QImage.Format_RGB32) def tearDown(self): #Release resources del self.color del self.image super(SetPixelFloat, self).tearDown() def testFloat(self): #QImage.setPixel(float, float, color) - Implicit conversion self.image.setPixel(3.14, 4.2, self.color) self.assertEqual(self.image.pixel(3.14, 4.2), self.color)
class ImagenQImage(BaseImagen): def __init__(self): super(ImagenQImage, self).__init__() self.mode = "No implementado" @property def size(self): return (self.img.width(), self.img.height()) def fromfile(self, filename): self.img = QImage(filename) def from_instance(self, qimage): self.img = qimage def empty(self, size, mode=QImage.Format_RGB888): self.img = QImage(size[0], size[1], mode) self.img.fill(qRgb(0, 0, 0)) #Rellenamos la imagen con negro def getpixel(self, xy): color = self.img.pixel(xy[0], xy[1]) return (qRed(color), qGreen(color), qBlue(color)) def putpixel(self, xy, value): self.img.setPixel(xy[0], xy[1], qRgb(value[0], value[1], value[2])) def get_img(self): return self.img def save(self, filename): self.img.save(filename, format="BMP", quality=100) def from_opencv(self, img_opencv): dst = cv2.cvtColor(img_opencv, cv2.COLOR_BGR2RGB) qim = QImage(dst.data, dst.shape[1], dst.shape[0], dst.strides[0], QImage.Format_RGB888) self.img = qim.copy()
def setSettings(self): """Set color, verbosity and logging options.""" self.logDock.setBgColor(self.config.get('Appearance', 'log_bg_color')) image = QImage(10, 10, QImage.Format_RGB32) image.fill(QColor(self.config.get('Appearance', 'circ_bg_color'))) image.setPixel(0, 0, QColor(0, 0, 0).rgb()) self.view.scene().setBackgroundBrush(QBrush(QPixmap.fromImage(image))) Plug.setInputVerbose = self.config.getboolean( 'LogVerbosity', 'input_chang') Plug.setOutputVerbose = self.config.getboolean( 'LogVerbosity', 'output_chang') Plug.connectVerbose = self.config.getboolean( 'LogVerbosity', 'conn_discon_io') Plug.addPlugVerbose = self.config.getboolean( 'LogVerbosity', 'adding_io') Circuit.addCircuitVerbose = self.config.getboolean( 'LogVerbosity', 'adding_circ') Circuit.removePlugVerbose = self.config.getboolean( 'LogVerbosity', 'removing_io') Circuit.removeCircuitVerbose = self.config.getboolean( 'LogVerbosity', 'removing_circ') Circuit.detailedRemoveVerbose = self.config.getboolean( 'LogVerbosity', 'detailed_rm') ClockThread.spd = self.config.getfloat( 'Clock', 'speed') if self.config.getboolean('LogHandlers', 'gui'): log.addHandler(self.logDock.handler) else: log.removeHandler(self.logDock.handler) if self.config.getboolean('LogHandlers', 'stdout'): log.addHandler(stdoutHandler) else: log.removeHandler(stdoutHandler) if self.config.getboolean('LogHandlers', 'file'): log.addHandler(fileHandler) else: log.removeHandler(fileHandler)
class ImagenQImage(BaseImagen): def __init__(self): super(ImagenQImage, self).__init__() self.mode = "No implementado" @property def size(self): return (self.img.width(), self.img.height()) def fromfile(self, filename): self.img = QImage(filename) def from_instance(self, qimage): self.img = qimage def empty(self, size, mode=QImage.Format_RGB888): self.img = QImage(size[0], size[1], mode) self.img.fill(qRgb(0,0,0))#Rellenamos la imagen con negro def getpixel(self, xy): color = self.img.pixel(xy[0], xy[1]) return (qRed(color), qGreen(color), qBlue(color)) def putpixel(self, xy, value): self.img.setPixel(xy[0], xy[1], qRgb(value[0], value[1], value[2])) def get_img(self): return self.img def save(self, filename): self.img.save(filename, format="BMP", quality=100) def from_opencv(self, img_opencv): dst = cv2.cvtColor(img_opencv, cv2.COLOR_BGR2RGB) qim = QImage(dst.data, dst.shape[1], dst.shape[0], dst.strides[0], QImage.Format_RGB888) self.img = qim.copy()
class Render(): '''Transform the numpy data into a renderable image suitable for screen''' def __init__( self, world ): self.world = world for k in self.world: exec( 'self.' + k + ' = self.world[k]' ) self.width = len( self.elevation ) self.height = self.width self.image = QImage( self.width, self.height, QImage.Format_RGB32 ) def hex2rgb( self, hexcolor ): r = ( hexcolor >> 16 ) & 0xFF; g = ( hexcolor >> 8 ) & 0xFF; b = hexcolor & 0xFF; return [r, g, b] def rgb2hex( self, rgb ): assert( len( rgb ) == 3 ) return '#%02x%02x%02x' % rgb def convert( self, mapType ): background = [] if mapType == "heightmap": heightmap = self.elevation * 255 # convert to greyscale for x in xrange( self.width ): for y in xrange( self.height ): gValue = heightmap[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "sealevel": for x in xrange( self.width ): for y in xrange( self.height ): elevation = self.elevation[x, y] gValue = elevation * 255 if elevation <= constants.WGEN_SEA_LEVEL: # sealevel self.image.setPixel( x, y, QtGui.QColor( 0, 0, gValue ).rgb() ) else: self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "elevation": for x in xrange( self.width ): for y in xrange( self.height ): elevation = self.elevation[x, y] if elevation <= constants.WGEN_SEA_LEVEL: # sealevel self.image.setPixel( x, y, QtGui.QColor( 0, 0, 128 ).rgb() ) elif elevation < constants.BIOME_ELEVATION_HILLS: # grasslands self.image.setPixel( x, y, QtGui.QColor( 128, 255, 0 ).rgb() ) elif elevation < constants.BIOME_ELEVATION_MOUNTAIN_LOW: # mountains self.image.setPixel( x, y, QtGui.QColor( 90, 128, 90 ).rgb() ) else: self.image.setPixel( x, y, QtGui.QColor( 255, 255, 255 ).rgb() ) elif mapType == "heatmap": for x in xrange( self.width ): for y in xrange( self.height ): gValue = self.temperature[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue * 255, gValue * 128, ( 1 - gValue ) * 255 ).rgb() ) elif mapType == "rawheatmap": temperature = self.temperature * 255 # convert to greyscale for x in xrange( self.width ): for y in xrange( self.height ): gValue = temperature[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == 'windmap': for x in xrange( self.width ): for y in xrange( self.height ): gValue = self.wind[x, y] self.image.setPixel( x, y, QtGui.QColor( 0, gValue * 255, 0 ).rgb() ) elif mapType == 'rainmap': for x in xrange( self.width ): for y in xrange( self.height ): gValue = self.rainfall[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue * 100, gValue * 100, gValue * 255 ).rgb() ) elif mapType == 'windandrainmap': for x in xrange( self.width ): for y in xrange( self.height ): rain = int( 255 * min( self.wind[x, y], 1.0 ) ) wind = int( 255 * min( self.rainfall[x, y], 1.0 ) ) self.image.setPixel( x, y, QtGui.QColor( 0, wind, rain ).rgb() ) elif mapType == 'drainagemap': drainage = self.drainage * 255 # convert to greyscale for x in xrange( self.width ): for y in xrange( self.height ): gValue = drainage[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == 'rivermap': for x in xrange( self.width ): for y in xrange( self.height ): gValue = self.elevation[x, y] * 255 if self.elevation[x, y] <= constants.WGEN_SEA_LEVEL: # sealevel self.image.setPixel( x, y, QtGui.QColor( 0, 0, gValue ).rgb() ) else: rgb = QtGui.QColor( gValue, gValue, gValue ).rgb() if self.rivers[x, y] > 0.0: rgb = constants.COLOR_COBALT if self.lakes[x, y] > 0.0: rgb = constants.COLOR_AZURE self.image.setPixel( x, y, rgb ) elif mapType == 'biomemap': for x in xrange( self.width ): for y in xrange( self.height ): self.image.setPixel( x, y, self.biomeColour[x, y] ) elif mapType == "erosionmap": erosion = self.erosion * 255 # convert to greyscale for x in xrange( self.width ): for y in xrange( self.height ): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "erosionappliedmap": erosion = ( self.elevation - self.erosion ) * 255 # convert to greyscale for x in xrange( self.width ): for y in xrange( self.height ): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) else: # something bad happened... print "did not get a valid map type, check your bindings programmer man!" print len( background ), background, mapType from numpy import zeros background = zeros( ( self.width, self.height ), dtype = "int32" ) return self.image
class Render(object): ''' Cette classe génère une image lorsqu'une instance de la classe image_provider demande une mise à jour. ''' def __init__(self, gui_handler, width, height): ''' Constructeur ''' self.__gui_handler = gui_handler self.__width = width self.__height = height self.__pixmap = QPixmap(self.__width, self.__height) self.__image = QImage(self.__width, self.__height, QImage.Format.Format_RGB32) self.positions = {} # Pour savoir si c'est le premier tour ou pas. self.__first = True # Contient les différents types d'entités et terrain avec leurs # couleurs associées. self.__terrain = [] def pixmap(self): """ Renvoie le pixmap mis à jour. """ self.__pixmap = QPixmap.fromImage(self.__image) return self.__pixmap def update(self): """" Mise a jour de l'image. """ if self.__first: self.__first = False self.__map_data = self.__gui_handler.get_map_data() self.__next_data = self.__gui_handler.get_entities() labels = [] # Découverte du terrain for terrain in SimUtils.get_terrains(): self.__terrain.append(terrain.color) labels.append(StatItem(terrain.name, "", terrain.color)) # Tri lexicographique des labels. labels.sort(key=lambda stat: stat._name) # Ajout des labels de terrain for label in labels: self.__gui_handler.add_stat(label) # Remplissage de la carte avec les terrains. for i in range(0, self.__width): for j in range(0, self.__height): # Affichage du point. color = QColor(self.__terrain[self.__map_data.get_terrain_type(i,j)]) self.__image.setPixel(i,j,color.rgb()) # Permet de faire le tri entre les entités déjà rencontrées et les # autres. entity_types = {} # Liste des futurs labels labels = [] # Découverte des entités - affectation des couleurs for entity in self.__next_data: # Ajout des labels de couleur pour les entités if not entity_types.has_key(entity.__name__): entity_types[entity.__name__] = True for label, color in entity._labels.iteritems(): labels.append(StatItem(label, "", color)) # Affichage de l'entité. self.__image.setPixel(entity._x, entity._y, QColor(entity._color).rgb()) self.positions[id(entity)] = [entity._x, entity._y] # Tri lexicographique des labels. labels.sort(key=lambda stat: stat._name) for label in labels: self.__gui_handler.add_stat(label) else: # Mise à jour du rendu for entity in self.__next_data: # Cas d'une entité désactivée (morte) remove_entity = not entity._is_active() if id(entity) not in self.positions: # Ajout de l'entité en cours de simulation self.__image.setPixel(entity._x, entity._y, QColor(entity._color).rgb()) self.positions[id(entity)] = [entity._x,entity._y] # Le simulateur demande de repeindre l'entité old_points = self.positions[id(entity)] if not remove_entity: self.positions[id(entity)] = [entity._x, entity._y] # On remet la couleur du terrain. color = QColor(self.__terrain[self.__map_data.get_terrain_type(old_points[0], old_points[1])]) self.__image.setPixel(old_points[0], old_points[1], color.rgb()) if not remove_entity: # Ajout des paramètres de setPixel dans une liste pour être ploté après. self.__image.setPixel(entity._x, entity._y, QColor(entity._color).rgb())
class Render(): '''Transform the numpy data into a renderable image suitable for screen''' def __init__(self, world): self.world = world for k in self.world: exec('self.' + k + ' = self.world[k]') self.width, self.height = self.elevation.shape self.image = QImage(self.width, self.height, QImage.Format_RGB32) self.image.fill(QtGui.QColor(0, 0, 0)) def hex2rgb(self, hexcolor): r = (hexcolor >> 16) & 0xFF g = (hexcolor >> 8) & 0xFF b = hexcolor & 0xFF return [r, g, b] def rgb2hex(self, rgb): assert (len(rgb) == 3) return '#%02x%02x%02x' % rgb def convert(self, mapType, seaLevel=None): if seaLevel: seaLevel /= 100.0 # reduce to 0.0 to 1.0 range background = [] if mapType == "heightmap": heightmap = self.elevation * 255 # convert to greyscale for x in range(self.width): for y in range(self.height): gValue = heightmap[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) elif mapType == "sealevel": for x in range(self.width): for y in range(self.height): elevation = self.elevation[x, y] gValue = elevation * 255 if elevation <= seaLevel: self.image.setPixel(x, y, QtGui.QColor(0, 0, gValue).rgb()) else: self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) elif mapType == "elevation": for x in range(self.width): for y in range(self.height): elevation = self.elevation[x, y] if elevation <= seaLevel: if elevation < seaLevel / 4.0: self.image.setPixel(x, y, COLOR_DEEPSEA) elif elevation < seaLevel / 2.0: self.image.setPixel(x, y, COLOR_SEA) else: self.image.setPixel(x, y, COLOR_BLUE) else: if elevation < 0.65: self.image.setPixel(x, y, COLOR_GRASSLAND) elif elevation < 0.95: self.image.setPixel(x, y, COLOR_HILLS) else: self.image.setPixel(x, y, COLOR_WHITE) elif mapType == "heatmap": for x in range(self.width): for y in range(self.height): gValue = self.temperature[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue * 255, gValue * 128, (1 - gValue) * 255).rgb()) elif mapType == "rawheatmap": temperature = self.temperature * 255 # convert to greyscale for x in range(self.width): for y in range(self.height): gValue = temperature[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) elif mapType == 'windmap': for x in range(self.width): for y in range(self.height): gValue = self.wind[x, y] self.image.setPixel(x, y, QtGui.QColor(0, gValue * 255, 0).rgb()) elif mapType == 'rainmap': for x in range(self.width): for y in range(self.height): gValue = self.rainfall[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue * 100, gValue * 100, gValue * 255).rgb()) elif mapType == 'windandrainmap': for x in range(self.width): for y in range(self.height): rain = int(255 * min(self.wind[x, y], 1.0)) wind = int(255 * min(self.rainfall[x, y], 1.0)) self.image.setPixel(x, y, QtGui.QColor(0, wind, rain).rgb()) elif mapType == 'drainagemap': drainage = self.drainage * 255 # convert to greyscale for x in range(self.width): for y in range(self.height): gValue = drainage[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) elif mapType == 'rivermap': for x in range(self.width): for y in range(self.height): gValue = self.elevation[x, y] * 255 if self.elevation[x, y] <= seaLevel: self.image.setPixel(x, y, QtGui.QColor(0, 0, gValue).rgb()) else: rgb = QtGui.QColor(gValue, gValue, gValue).rgb() if self.rivers[x, y] > 0.0: rgb = COLOR_COBALT if self.lakes[x, y] > 0.0: rgb = COLOR_AZURE self.image.setPixel(x, y, rgb) elif mapType == 'biomemap': for x in range(self.width): for y in range(self.height): self.image.setPixel(x, y, self.biomeColour[x, y]) elif mapType == "erosionmap": erosion = self.erosion * 255 # convert to greyscale for x in range(self.width): for y in range(self.height): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) elif mapType == "erosionappliedmap": erosion = (self.elevation - self.erosion) * 255 # convert to greyscale for x in range(self.width): for y in range(self.height): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor(gValue, gValue, gValue).rgb()) else: # something bad happened... print( "did not get a valid map type, check your bindings programmer man!" ) print(len(background), background, mapType) from numpy import zeros background = zeros((self.width, self.height), dtype="int32") return self.image
from PySide.QtCore import QCoreApplication from PySide.QtGui import QImage if __name__ == "__main__": app = QCoreApplication([]) img = QImage("lenna.png") w, h = img.size().width(), img.size().width() for p in ((x, y) for x in range(w) for y in range(h)): colour = img.pixel(p[0], p[1]) # AARRGGBB r = (colour >> 16) & 0xFF g = (colour >> 8) & 0xFF b = colour & 0xFF avg = round((r + g + b) / 3) # Naïve method (no colour weighting) new_colour = 0xff000000 + (avg << 16) + (avg << 8) + avg img.setPixel(p[0], p[1], new_colour) img.save("output.png")
class Render(): '''Transform the numpy data into a renderable image suitable for screen''' def __init__( self, world ): self.world = world for k in self.world: exec( 'self.' + k + ' = self.world[k]' ) self.width, self.height = self.elevation.shape self.image = QImage( self.width, self.height, QImage.Format_RGB32 ) self.image.fill(QtGui.QColor(0,0,0)) def hex2rgb( self, hexcolor ): r = ( hexcolor >> 16 ) & 0xFF; g = ( hexcolor >> 8 ) & 0xFF; b = hexcolor & 0xFF; return [r, g, b] def rgb2hex( self, rgb ): assert( len( rgb ) == 3 ) return '#%02x%02x%02x' % rgb def convert( self, mapType, seaLevel = None ): if seaLevel: seaLevel /= 100.0 # reduce to 0.0 to 1.0 range background = [] if mapType == "heightmap": heightmap = self.elevation * 255 # convert to greyscale for x in range( self.width ): for y in range( self.height ): gValue = heightmap[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "sealevel": for x in range( self.width ): for y in range( self.height ): elevation = self.elevation[x, y] gValue = elevation * 255 if elevation <= seaLevel: self.image.setPixel( x, y, QtGui.QColor( 0, 0, gValue ).rgb() ) else: self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "elevation": for x in range( self.width ): for y in range( self.height ): elevation = self.elevation[x, y] if elevation <= seaLevel: if elevation < seaLevel/4.0: self.image.setPixel( x, y, COLOR_DEEPSEA ) elif elevation < seaLevel/2.0: self.image.setPixel( x, y, COLOR_SEA ) else: self.image.setPixel( x, y, COLOR_BLUE ) else: if elevation < 0.65: self.image.setPixel( x, y, COLOR_GRASSLAND ) elif elevation < 0.95: self.image.setPixel( x, y, COLOR_HILLS ) else: self.image.setPixel( x, y, COLOR_WHITE ) elif mapType == "heatmap": for x in range( self.width ): for y in range( self.height ): gValue = self.temperature[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue * 255, gValue * 128, ( 1 - gValue ) * 255 ).rgb() ) elif mapType == "rawheatmap": temperature = self.temperature * 255 # convert to greyscale for x in range( self.width ): for y in range( self.height ): gValue = temperature[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == 'windmap': for x in range( self.width ): for y in range( self.height ): gValue = self.wind[x, y] self.image.setPixel( x, y, QtGui.QColor( 0, gValue * 255, 0 ).rgb() ) elif mapType == 'rainmap': for x in range( self.width ): for y in range( self.height ): gValue = self.rainfall[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue * 100, gValue * 100, gValue * 255 ).rgb() ) elif mapType == 'windandrainmap': for x in range( self.width ): for y in range( self.height ): rain = int( 255 * min( self.wind[x, y], 1.0 ) ) wind = int( 255 * min( self.rainfall[x, y], 1.0 ) ) self.image.setPixel( x, y, QtGui.QColor( 0, wind, rain ).rgb() ) elif mapType == 'drainagemap': drainage = self.drainage * 255 # convert to greyscale for x in range( self.width ): for y in range( self.height ): gValue = drainage[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == 'rivermap': for x in range( self.width ): for y in range( self.height ): gValue = self.elevation[x, y] * 255 if self.elevation[x, y] <= seaLevel: self.image.setPixel( x, y, QtGui.QColor( 0, 0, gValue ).rgb() ) else: rgb = QtGui.QColor( gValue, gValue, gValue ).rgb() if self.rivers[x, y] > 0.0: rgb = COLOR_COBALT if self.lakes[x, y] > 0.0: rgb = COLOR_AZURE self.image.setPixel( x, y, rgb ) elif mapType == 'biomemap': for x in range( self.width ): for y in range( self.height ): self.image.setPixel( x, y, self.biomeColour[x, y] ) elif mapType == "erosionmap": erosion = self.erosion * 255 # convert to greyscale for x in range( self.width ): for y in range( self.height ): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) elif mapType == "erosionappliedmap": erosion = ( self.elevation - self.erosion ) * 255 # convert to greyscale for x in range( self.width ): for y in range( self.height ): gValue = erosion[x, y] self.image.setPixel( x, y, QtGui.QColor( gValue, gValue, gValue ).rgb() ) else: # something bad happened... print("did not get a valid map type, check your bindings programmer man!") print(len( background ), background, mapType) from numpy import zeros background = zeros( ( self.width, self.height ), dtype = "int32" ) return self.image
from PySide.QtCore import QCoreApplication from PySide.QtGui import QImage if __name__ == "__main__": app = QCoreApplication([]) img = QImage("lenna.png") w, h = img.size().width(), img.size().width() for p in ((x, y) for x in range(w) for y in range(h)): colour = img.pixel(p[0], p[1]) # AARRGGBB r = (colour >> 16) & 0xFF g = (colour >> 8) & 0xFF b = colour & 0xFF avg = round((r + g + b) / 3) # Naïve method (no colour weighting) new_colour = 0xff000000 + (avg << 16) + (avg << 8) + avg img.setPixel(p[0], p[1], new_colour) img.save("output.png")