def __init__(self, text, screen, drawCursor=True): """ Create the dialog box and load cursors. text - a list of lines of text to go in the dialog. font - the font with which to write the text. screen - the surface to draw the dialog onto. soundManager - the sound manager. drawCursor - whether a continuation cursor should be drawn when the text has finished writing. """ #store variables we'll need again self.text = text.split(LINESEP) self.screen = screen self.drawCursor = drawCursor #determine the speed to write at, in characters per tick if settings.textSpeed == "SLOW": self.speed = 1 elif settings.textSpeed == "MEDIUM": self.speed = 2 elif settings.textSpeed == "FAST": self.speed = 4 #parse the dialog xml file fn = os.path.join(settings.path, "data", globs.DIALOG) root = data.getTreeRoot(fn, "Ditto main") transparency = data.getAttr(root, "transparency", data.D_INT3LIST) #create font fontPath = os.path.join(settings.path, "data", globs.FONT) self.font = font.Font(fontPath) #create the box size = ((self.screen.get_width()-(OBJECTBUFFER*2), (len(self.text)*(self.font.height+LINEBUFFER))-LINEBUFFER+(BORDER*2))) self.box = box.Box(size).convert(self.screen) #load the cursors cursorPath = os.path.join(settings.path, "data", data.getAttr(root, "cursor", data.D_STRING)) self.cursor = data.getImage(cursorPath).convert(self.screen) self.cursor.set_colorkey(transparency) self.cursorLocation = (self.screen.get_width()-OBJECTBUFFER-BORDER-self.cursor.get_width(), self.screen.get_height()-OBJECTBUFFER-BORDER-self.cursor.get_height()) cursorPath = os.path.join(settings.path, "data", data.getAttr(root, "sidecursor", data.D_STRING)) self.sideCursor = data.getImage(cursorPath).convert(self.screen) self.sideCursor.set_colorkey(transparency) #calculate location of dialog box self.location = OBJECTBUFFER, self.screen.get_height()-self.box.get_height()-OBJECTBUFFER #start progress at 0 and set drawing and busy self.progress = 0 self.writing = True self.busy = True
def __init__(self, screen, node): """Load the image and determine it's position.""" #store screen for later self.screen = screen #open the image fn = os.path.join(settings.path, "data", data.getAttr(node, "file", data.D_STRING)) self.image = data.getImage(fn, "Intro file.").convert(self.screen) transparency = data.getAttr(node, "transparency", data.D_INT3LIST) self.image.set_colorkey(transparency) #calculate where the centres are for the screen and the image screenCentre = self.screen.get_width() / 2, self.screen.get_height( ) / 2 imageCentre = self.image.get_width() / 2, self.image.get_height() / 2 #find out the location of the image location = data.getAttr(node, "location", data.D_INT2LIST) #calculate its position on the screen #location (0,0) should be dead centre, (-100, 100) bottom left self.position = ((screenCentre[0] - imageCentre[0]) + ((location[0] * screenCentre[0]) / 100), (screenCentre[1] - imageCentre[1]) + ((location[1] * screenCentre[1]) / 100))
def __init__(self, fn): """ Load the image and cut out the individual characters. fn - the font XML file. """ #parse the XML file root = data.getTreeRoot(fn) path = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING)) self.height = data.getAttr(root, "height", data.D_INT) transparency = data.getAttr(root, "transparency", data.D_INT3LIST) #load the image image = data.getImage(path, fn) image.set_colorkey(transparency) #cut out the tiles and store them in a dictionary self.characters = {} for c in data.getChildren(root, "character"): char = data.getAttr(c, "char", data.D_STRING) width = data.getAttr(c, "width", data.D_INT) location = data.getAttr(c, "location", data.D_INT2LIST) self.characters[char] = image.subsurface(location[0], location[1], width, self.height)
def openImage(self, fn): """ Open the tileset image and cut into tiles. fn - the path to the tileset image. """ #get the image, and make sure it's pixel dimensions are consistent #tilesets have 1 spacing between each tile, #so adding 1 should give a multiple of the tilesize+1 tilesetImage = data.getImage(fn) tilesetImage.set_colorkey(self.transparency) data.check(((tilesetImage.get_width()+1)%(self.tileSize[0]+1))==0, fn) data.check(((tilesetImage.get_height()+1)%(self.tileSize[1]+1))==0, fn) dimensions = ((tilesetImage.get_width()+1)/(self.tileSize[0]+1), (tilesetImage.get_height()+1)/(self.tileSize[1]+1)) #iterate over each tile, cutting it out and adding to our list #go across each row in turn to get index numbering correct self.tiles = [] for y in range(0, dimensions[1]): for x in range(0, dimensions[0]): tile = tilesetImage.subsurface((x*(self.tileSize[0]+1), y*(self.tileSize[1]+1), self.tileSize[0], self.tileSize[1])) self.tiles.append(tile) #calculate offset self.tileOffset = ((globs.TILESIZE[0]-self.tileSize[0])/2, globs.TILESIZE[1]-self.tileSize[1])
def openImage(self, fn): """ Open the tileset image and cut into tiles. fn - the path to the tileset image. """ #get the image, and make sure it's pixel dimensions are consistent #tilesets have 1 spacing between each tile, #so adding 1 should give a multiple of the tilesize+1 tilesetImage = data.getImage(fn) tilesetImage.set_colorkey(self.transparency) data.check( ((tilesetImage.get_width() + 1) % (self.tileSize[0] + 1)) == 0, fn) data.check( ((tilesetImage.get_height() + 1) % (self.tileSize[1] + 1)) == 0, fn) dimensions = ((tilesetImage.get_width() + 1) / (self.tileSize[0] + 1), (tilesetImage.get_height() + 1) / (self.tileSize[1] + 1)) #iterate over each tile, cutting it out and adding to our list #go across each row in turn to get index numbering correct self.tiles = [] for y in range(0, dimensions[1]): for x in range(0, dimensions[0]): tile = tilesetImage.subsurface( (x * (self.tileSize[0] + 1), y * (self.tileSize[1] + 1), self.tileSize[0], self.tileSize[1])) self.tiles.append(tile) #calculate offset self.tileOffset = ((globs.TILESIZE[0] - self.tileSize[0]) / 2, globs.TILESIZE[1] - self.tileSize[1])
def getBattler(self): graphicsNode = data.getChild(self.speciesNode, "graphics") battleNode = data.getChild(graphicsNode, "battle") battler = data.getImage(os.path.join(settings.path, "data", data.getAttr(battleNode, "front", data.D_STRING)), battleNode.ditto_fn) trans = data.getAttr(battleNode, "transparency", data.D_INT3LIST) battler.set_colorkey(trans) return battler
def setup(self): """Initialize pygame, find the main game file and parse it.""" #initialise pygame pygame.init() #find the main game file and parse it if settings.path is not None: fn = os.path.join(settings.path, "data", "game.xml") else: raise error.DittoInvalidResourceException("settings.py", "path") root = data.getTreeRoot(fn, "Ditto Main") for p in data.getChildren(root, "property"): if data.getAttr(p, "name", data.D_STRING) == "tilesize": globs.TILESIZE = data.getAttr(p, "value", data.D_INT2LIST) elif data.getAttr(p, "name", data.D_STRING) == "font": globs.FONT = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "dialog": globs.DIALOG = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "pokemon": globs.POKEMON = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "soundeffects": soundPath = os.path.join( settings.path, "data", data.getAttr(p, "value", data.D_STRING)) elif data.getAttr(p, "name", data.D_STRING) == "moves": globs.MOVES = os.path.join( settings.path, "data", data.getAttr(p, "value", data.D_STRING)) elif data.getAttr(p, "name", data.D_STRING) == "behaviours": globs.BEHAVIOURS = os.path.join( settings.path, "data", data.getAttr(p, "value", data.D_STRING)) #if there's an icon specified, use it if len(data.getChildren(root, "icon")) > 0: node = data.getChild(root, "icon") iconPath = os.path.join(settings.path, "data", data.getAttr(node, "file", data.D_STRING)) icon = data.getImage(iconPath, fn) pygame.display.set_icon(icon) #set up the main window windowSize = settings.screenSize[0] * globs.TILESIZE[ 0], settings.screenSize[1] * globs.TILESIZE[1] self.screen = pygame.display.set_mode(windowSize) pygame.display.set_caption("%s --- Ditto Engine" % data.getAttr(root, "title", data.D_STRING)) #create a clock object self.clock = pygame.time.Clock() #initialise sound sound.init(soundPath) #set up the initial scene, the intro self.activeScene = intro.Intro(self.screen)
def getBattler(self): graphicsNode = data.getChild(self.speciesNode, "graphics") battleNode = data.getChild(graphicsNode, "battle") battler = data.getImage( os.path.join(settings.path, "data", data.getAttr(battleNode, "front", data.D_STRING)), battleNode.ditto_fn) trans = data.getAttr(battleNode, "transparency", data.D_INT3LIST) battler.set_colorkey(trans) return battler
def __init__(self, screen, partyNode, font, location, poke): self.screen = screen self.font = font self.location = location self.poke = poke self.speciesNode = poke.speciesNode graphicsNode = data.getChild(self.speciesNode, "graphics") iconNode = data.getChild(graphicsNode, "icon") fn = os.path.join(settings.path, "data", data.getAttr(iconNode, "file", data.D_STRING)) image = data.getImage(fn, iconNode.ditto_fn) self.iconSize = data.getAttr(iconNode, "size", data.D_INT2LIST) self.icon = image.subsurface((0,0), self.iconSize) self.icon.set_colorkey(data.getAttr(iconNode, "transparency", data.D_INT3LIST))
def setup(self): """Initialize pygame, find the main game file and parse it.""" #initialise pygame pygame.init() #find the main game file and parse it if settings.path is not None: fn = os.path.join(settings.path, "data", "game.xml") else: raise error.DittoInvalidResourceException("settings.py", "path") root = data.getTreeRoot(fn, "Ditto Main") for p in data.getChildren(root, "property"): if data.getAttr(p, "name", data.D_STRING) == "tilesize": globs.TILESIZE = data.getAttr(p, "value", data.D_INT2LIST) elif data.getAttr(p, "name", data.D_STRING) == "font": globs.FONT = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "dialog": globs.DIALOG = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "pokemon": globs.POKEMON = data.getAttr(p, "value", data.D_STRING) elif data.getAttr(p, "name", data.D_STRING) == "soundeffects": soundPath = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING)) elif data.getAttr(p, "name", data.D_STRING) == "moves": globs.MOVES = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING)) elif data.getAttr(p, "name", data.D_STRING) == "behaviours": globs.BEHAVIOURS = os.path.join(settings.path, "data", data.getAttr(p, "value", data.D_STRING)) #if there's an icon specified, use it if len(data.getChildren(root, "icon")) > 0: node = data.getChild(root, "icon") iconPath = os.path.join(settings.path, "data", data.getAttr(node, "file", data.D_STRING)) icon = data.getImage(iconPath, fn) pygame.display.set_icon(icon) #set up the main window windowSize = settings.screenSize[0]*globs.TILESIZE[0], settings.screenSize[1]*globs.TILESIZE[1] self.screen = pygame.display.set_mode(windowSize) pygame.display.set_caption("%s --- Ditto Engine" % data.getAttr(root, "title", data.D_STRING)) #create a clock object self.clock = pygame.time.Clock() #initialise sound sound.init(soundPath) #set up the initial scene, the intro self.activeScene = intro.Intro(self.screen)
def __init__(self, screen, node): """Load the image and determine it's position.""" #store screen for later self.screen = screen #open the image fn = os.path.join(settings.path, "data", data.getAttr(node, "file", data.D_STRING)) self.image = data.getImage(fn, "Intro file.").convert(self.screen) transparency = data.getAttr(node, "transparency", data.D_INT3LIST) self.image.set_colorkey(transparency) #calculate where the centres are for the screen and the image screenCentre = self.screen.get_width()/2, self.screen.get_height()/2 imageCentre = self.image.get_width()/2, self.image.get_height()/2 #find out the location of the image location = data.getAttr(node, "location", data.D_INT2LIST) #calculate its position on the screen #location (0,0) should be dead centre, (-100, 100) bottom left self.position = ((screenCentre[0]-imageCentre[0])+((location[0]*screenCentre[0])/100), (screenCentre[1]-imageCentre[1])+((location[1]*screenCentre[1])/100))
def __init__(self, size, fn=None): """ Initialize a surface and draw the box onto it. size - the size of the box. fn - the path to a box xml config file. """ #initialize the pygame Surface pygame.Surface.__init__(self, size) #if no filename given, use the game default box if fn == None: fn = os.path.join(settings.path, "data", globs.DIALOG) #parse the box xml file root = data.getTreeRoot(fn) tilesetPath = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING)) tileset = data.getImage(tilesetPath) transparency = data.getAttr(root, "transparency", data.D_INT3LIST) tileset.set_colorkey(transparency) tileSize = data.getAttr(root, "tilesize", data.D_INT2LIST) data.check((tileSize[0]+1)*3==tileset.get_width()+1, tilesetPath) data.check((tileSize[1]+1)*3==tileset.get_height()+1, tilesetPath) #fill transparent self.fill((255,0,255)) self.set_colorkey((255,0,255)) #cut each of the nine tiles out from the tileset tileNW = tileset.subsurface((0,0), tileSize) tileN = tileset.subsurface((tileSize[0]+1,0), tileSize) tileNE = tileset.subsurface(((tileSize[0]+1)*2,0), tileSize) tileW = tileset.subsurface((0,tileSize[1]+1), tileSize) tileC = tileset.subsurface((tileSize[0]+1,tileSize[1]+1), tileSize) tileE = tileset.subsurface(((tileSize[0]+1)*2,tileSize[1]+1), tileSize) tileSW = tileset.subsurface((0,(tileSize[1]+1)*2), tileSize) tileS = tileset.subsurface((tileSize[0]+1,(tileSize[1]+1)*2), tileSize) tileSE = tileset.subsurface(((tileSize[0]+1)*2,(tileSize[1]+1)*2), tileSize) #calculate how much of the box is not covered by edge tiles - all this middle must be covered by the centre tile #work out how many tiles it will take to cover that, and where to start drawing from middleSize = size[0]-(2*tileSize[0]), size[1]-(2*tileSize[1]) dimensions = (middleSize[0]/tileSize[0])+1, (middleSize[1]/tileSize[1])+1 origin = (size[0]-(dimensions[0]*tileSize[0]))/2, (size[1]-(dimensions[1]*tileSize[1]))/2 #iterate over the required dimensions, drawing in the centre tiles #as we go down the first column only, draw in the left and right side tiles on the edge of the box #after we finish each column, draw the top and bottom tiles on the edge for x in range(0, dimensions[0]): for y in range(0, dimensions[1]): self.blit(tileC, (origin[0]+(x*tileSize[0]), origin[1]+(y*tileSize[1]))) if x == 0: self.blit(tileW, (0, origin[1]+(y*tileSize[1]))) self.blit(tileE, (size[0]-tileSize[0], origin[1]+(y*tileSize[1]))) self.blit(tileN, (origin[0]+(x*tileSize[0]), 0)) self.blit(tileS, (origin[0]+(x*tileSize[0]), size[1]-tileSize[1])) #draw the corner tiles in the corners self.blit(tileNW, (0, 0)) self.blit(tileNE, (size[0]-tileSize[0], 0)) self.blit(tileSW, (0, size[1]-tileSize[1])) self.blit(tileSE, (size[0]-tileSize[0], size[1]-tileSize[1]))
def handler(): print(request.form) img = getImage(**request.form) return jsonify({"address": url_for('static', filename="pictures/" + img)})
print(output_height) print(output_width) images = glob.glob(images_path + "*.jpg") + glob.glob(images_path + "*.png") + glob.glob(images_path + "*.jpeg") images.sort() colors = [(0, 0, 0), (255, 0, 255), (255, 215, 0), (0, 255, 255), (255, 125, 0)] cnt = 0 # 开始统计程序运行时间 start_time = datetime.datetime.now() for imgName in images: outName = output_path + str("%d.jpg" % cnt) X = data.getImage(imgName, input_width, input_height) pr = model.predict(np.array([X]))[0] pr = pr.reshape((output_height, output_width, n_class)).argmax(axis = 2) seg_img = np.zeros((output_height, output_width, 3)) for c in range(n_class): seg_img[:, :, 0] += ((pr[:, :] == c) * (colors[c][0])).astype('uint8') seg_img[:, :, 1] += ((pr[:, :] == c) * (colors[c][1])).astype('uint8') seg_img[:, :, 2] += ((pr[:, :] == c) * (colors[c][2])).astype('uint8') seg_img = cv2.resize(seg_img, (input_width, input_height)) cv2.imwrite(outName, seg_img) cnt += 1 end_time = datetime.datetime.now() print("Total %d images, Average Time: " % cnt, end='') print((end_time - start_time).seconds * 1.0 / cnt)
print(output_height) print(output_width) # look up test images images = glob.glob(images_path + "*.jpg") + glob.glob( images_path + "*.png") + glob.glob(images_path + "*.jpeg") images.sort() cnt = 0 for imgName in images: outName = output_path + str("%d.jpg" % cnt) origin_img = cv2.imread(imgName, 1) origin_h = origin_img.shape[0] origin_w = origin_img.shape[1] X = data.getImage(imgName, input_width, input_height, image_init, resize_op) pr = model.predict(np.array([X]))[0] pr = pr.reshape((output_height, output_width, n_class)).argmax(axis=2) seg_img = np.zeros((output_height, output_width, 3)) for c in range(n_class): seg_img[:, :, 0] += ((pr[:, :] == c) * (colors[c][0])).astype('uint8') seg_img[:, :, 1] += ((pr[:, :] == c) * (colors[c][1])).astype('uint8') seg_img[:, :, 2] += ((pr[:, :] == c) * (colors[c][2])).astype('uint8') seg_img = cv2.resize(seg_img, (input_width, input_height), interpolation=cv2.INTER_NEAREST) cv2.imwrite(outName, seg_img) cnt += 1 print("Test Success!")
model.load_weights(save_weights_path) output_height = model.outputHeight output_width = model.outputWidth print(output_height) print(output_width) # look up test images images = glob.glob(images_path + "*.jpg") + glob.glob( images_path + "*.png") + glob.glob(images_path + "*.jpeg") images.sort() cnt = 0 for imgName in images: outName = output_path + str("%d.jpg" % cnt) X = data.getImage(imgName, input_width, input_height) pr = model.predict(np.array([X]))[0] pr = pr.reshape((output_height, output_width, n_class)).argmax(axis=2) seg_img = np.zeros((output_height, output_width, 3)) for c in range(n_class): seg_img[:, :, 0] = ((pr[:, :] == c) * (colors[c][0])).astype('uint8') seg_img[:, :, 1] = ((pr[:, :] == c) * (colors[c][1])).astype('uint8') seg_img[:, :, 2] = ((pr[:, :] == c) * (colors[c][2])).astype('uint8') seg_img = cv2.resize(seg_img, (input_width, input_height)) cv2.imwrite(outName, seg_img) cnt += 1 print("Test Success!") # mIOU