def GenerateSHPpreview(mapObject): # generates gif preview of shp files for every mapObject in list of objects currentDirectory = os.getcwd() for item in mapObject: path = os.getcwd() + os.sep + 'openraData/data/maps/' + str(item.id) + os.sep Dir = os.listdir(path + 'content/') if os.path.isdir(path+'content/png/'): shutil.rmtree(path+'content/png/') for fn in Dir: if fn.endswith('.shp'): os.mkdir(path + 'content/png/') os.chdir(path + 'content/png/') command = 'mono --debug %sOpenRA.Utility.exe %s --png %s %s' % (settings.OPENRA_PATH, item.game_mod, path+'content/'+fn, '../../../../palettes/0/RA1/temperat.pal') proc = Popen(command.split(), stdout=PIPE).communicate() pngsdir = os.listdir(path + 'content/png/') imglist = [] for pngfn in pngsdir: if pngfn.endswith('.png'): imglist.append(pngfn) imglist.sort() imgs = ImageList() for img in imglist: imgs.append(Image(path+'content/png/'+img)) imgs.animationDelayImages(50) imgs.writeImages(path+'content/'+fn+'.gif') os.chdir(currentDirectory) shutil.rmtree(path+'content/png/') return True
def save_files(png_list, save_name): print("Saving files to gif:%s" % save_name) imgs = ImageList() for file_name in png_list: im = PGImage(file_name) imgs.animationDelayImages(5) imgs.append(im) imgs.writeImages(save_name)
def GenerateSHPpreview(self): Dir = os.listdir(self.map_full_path_directory+'content/') for fn in Dir: if fn.endswith('.shp'): os.mkdir(self.map_full_path_directory+'content/png/') os.chdir(self.map_full_path_directory+'content/png/') command = 'mono --debug %sOpenRA.Utility.exe %s --png %s %s' % (settings.OPENRA_PATH, self.MapMod, self.map_full_path_directory+'content/'+fn, '../../../../palettes/0/RA1/temperat.pal') proc = Popen(command.split(), stdout=PIPE).communicate() self.flushLog(proc) pngsdir = os.listdir(self.map_full_path_directory+'content/png/') imglist = [] for pngfn in pngsdir: if pngfn.endswith('.png'): imglist.append(pngfn) imglist.sort() imgs = ImageList() for img in imglist: imgs.append(Image(self.map_full_path_directory+'content/png/'+img)) imgs.animationDelayImages(50) imgs.writeImages(self.map_full_path_directory+'content/'+fn+'.gif') os.chdir(self.currentDirectory) shutil.rmtree(self.map_full_path_directory+'content/png/')
def simulate(self, policy, iterations, output, figure_template, html, gif, anim_delay): raw_policy = deepcopy(policy) # Policy dictionary policy = { tuple([pol['state'][agent][1] for agent in self.agents]): pol['action'] for pol in policy } print '' print 'Goal State Set:' print self.name_goal print '' print 'Initial State:' agent_locs = OrderedDict([(agent, None) for agent in self.agents]) for agent in self.agents: agent_locs[agent] = np.random.choice(self.locs) print agent_locs.values() rwd = 0 print '' print 'Steps' history = {} for i in range(iterations): # Types of agent on each location agent_classes_on_loc = OrderedDict([(loc, []) for loc in self.locs]) for agent in self.agents: agent_classes_on_loc[agent_locs[agent]].append( self.agents_types[agent]) # State idx state_idx = self.s.index( tuple([self.locs[s] for s in agent_locs.values()])) # Computing rewards rwd = 0 for loc, types in self.name_goal.items(): if not (set(types) - set(agent_classes_on_loc[loc])): rwd += 1 # Printing history history[i] = { 'state_idx': state_idx, 'agent_locs': copy.deepcopy(agent_locs), 'rwd': rwd } # Updating agent location, given policy for agent in self.agents: # Locations adjacent to the agent location adj_locs = self.name_loc_roads[agent_locs[agent]] # Baseline probability of going to any adjacent location (error) pdf = np.zeros(len(self.locs), dtype=np.float32) for adj_loc in adj_locs: pdf[self.locs[adj_loc]] = self.error / (len(adj_locs) - 1) # Agent intended next state sn = self.locs[policy[tuple(agent_locs.values())][agent][-1]] # Success probability pdf[sn] = 1.0 - self.error # Transition agent_locs[agent] = np.random.choice(self.locs, p=pdf) # Printing history self.plot(raw_policy, output + figure_template, states=[hist['state_idx'] for hist in history.values()]) # Printing history for h, hist in history.items(): print h, hist # Creating animated GIF imgs = ImageList() for h, hist in history.items(): imgs.append(Image(output + figure_template % hist['state_idx'])) imgs.animationDelayImages(anim_delay) imgs.writeImages(output + gif) # Generating HTML report doc, tag, text, line = Doc().ttl() with tag('html'): with tag('body'): with tag('p', id='main'): with tag('h1'): text('Simulation Results') line('h2', 'Animation') with tag('div', id='frame'): doc.stag('img', src=gif) for h, hist in history.items(): with tag('p', id='%d' % h): line('h2', 'Iteration %d' % h) doc.stag('br') text('State ID: %d' % hist['state_idx']) doc.stag('br') text('Reward: %d' % hist['rwd']) doc.stag('br') for agent, loc in hist['agent_locs'].items(): text('Agent %s at location %s.' % (agent, loc)) doc.stag('br') with tag('div', id='frame'): doc.stag('img', src=figure_template % hist['state_idx']) # Storing HTML file result = indent(doc.getvalue()) with open(output + html, 'w') as f: f.write(result)
from pgmagick import Image, ImageList, Geometry, Color imgs = ImageList() for color in ('red', 'blue', 'green', 'black', 'yellow'): imgs.append(Image(Geometry(200, 200), Color(color))) imgs.animationDelayImages(100) imgs.scaleImages(Geometry(100, 100)) print len(imgs) imgs.writeImages('output.gif') imgs = ImageList() imgs.readImages('output.gif') for img in imgs: print img
#imgs.animationDelayImages(100) a = ImageType() imgs.readImages(srcGifFrame0) imgs.scaleImages("550x550") img = imgs.__getitem__(0) print img.columns(), img.rows() print len(imgs) #imgs.animationDelayImages(100) imgs.writeImages('./test.gif') """ from pgmagick import Image, ImageList, Geometry, Color imgs = ImageList() for color in ('red', 'blue', 'green', 'black', 'yellow'): imgs.append(Image(Geometry(200, 200), Color(color))) imgs.animationDelayImages(100) imgs.scaleImages(Geometry(100, 100)) print len(imgs) imgs.writeImages('output.gif') """ ""
from pgmagick import Image, ImageList, Geometry, Color imgs = ImageList() for color in ("red", "blue", "green", "black", "yellow"): imgs.append(Image(Geometry(200, 200), Color(color))) imgs.animationDelayImages(100) imgs.scaleImages(Geometry(100, 100)) print len(imgs) imgs.writeImages("output.gif") imgs = ImageList() imgs.readImages("output.gif") for img in imgs: print img
a = ImageType() imgs.readImages(srcGifFrame0) imgs.scaleImages( "550x550") img = imgs.__getitem__( 0 ) print img.columns(),img.rows() print len( imgs ) #imgs.animationDelayImages(100) imgs.writeImages( './test.gif' ) """ from pgmagick import Image, ImageList, Geometry, Color imgs = ImageList() for color in ('red', 'blue', 'green', 'black', 'yellow'): imgs.append(Image(Geometry(200, 200), Color(color))) imgs.animationDelayImages(100) imgs.scaleImages(Geometry(100, 100)) print len(imgs) imgs.writeImages('output.gif') """""