def genera_immagini_chunk(self): filename = r"../graphics/results/"+ self.pkl_name.split('/')[-1] if os.path.isfile(filename): print "[WORLD MAKER]: nothing to save..." return z_max = self.maximun dz_height = int((z_max)*(BLOCCOY-2*DY)) height = int(2*DY*(self.dimy-1)+BLOCCOY + dz_height) width = int(BLOCCOX*(self.dimx)) print "[WORLD MAKER]: generation of chunk images\t" background_final = Surface((width, height)) background_final.set_colorkey(TRANSPARENCY) background_final.fill(TRANSPARENCY) #sea_background = Surface((width, height)) #sea_background.set_colorkey(TRANSPARENCY) #sea_background.fill(TRANSPARENCY) for z in range(self.dimz): background = Surface((width, height)) #immagine con i tiles bassi foreground = Surface((width, height)) #immagine con i tiles alti background.fill(TRANSPARENCY) foreground.fill(TRANSPARENCY) background.set_colorkey(TRANSPARENCY) foreground.set_colorkey(TRANSPARENCY) for y in range(self.dimy): for x in range(self.dimx): t_type = self.matrix[z][y][x] tile = self.load_tile(t_type,z,1) tile_up = self.load_tile(t_type,z,0) if tile: xo = width/2 + (x-y-1)*DX yo = (x+y)*DY - z*DZ + dz_height tileRect = tile.get_rect() tileRect.topleft = (int(xo),int(yo)+BLOCCOY/2) background.blit(tile,tileRect) if tile_up: xo = width/2 + (x-y-1)*DX yo = (x+y)*DY - z*DZ + dz_height tileRect = tile_up.get_rect() tileRect.topleft = (int(xo),int(yo)) #if t_type == T_ACQUA: # sea_background.blit(tile_up,tileRect) #else: foreground.blit(tile_up,tileRect) background_final.blit(background,background.get_rect()) background_final.blit(foreground,background.get_rect()) data = Image.tostring(background, "RGBA") surf = Image.fromstring(data, (width, height), 'RGBA', False) Image.save(surf,r"../graphics/results/hill_"+str(z)+"_d.png") data = Image.tostring(foreground, "RGBA") surf = Image.fromstring(data, (width, height), 'RGBA', False) Image.save(surf,r"../graphics/results/hill_"+str(z)+"_u.png") #data = Image.tostring(sea_background, "RGBA") #surf = Image.fromstring(data, (width, height), 'RGBA', False) #Image.save(surf,r"../graphics/results/sea.png") Image.save(background_final,r"../graphics/results/all_hill.png") pickle.dump( self.matrix, open( r"../graphics/results/"+self.pkl_name.split('/')[-1], "wb" ) )
def get_palettes_image(self): addr_start = 0x3F00 buffer = [] for i in range(32): color = PALETTES[self._bus.read(addr_start + i)] buffer.extend(color) return Image.fromstring(bytes(buffer), (8, 4), 'RGB')
def merge_images(current_image: image, second_img_path: str): strFormat = "RGBA" raw_str = image.tostring(current_image, strFormat, False) im1 = Image.frombytes(strFormat, current_image.get_size(), raw_str) print(second_img_path) im2 = Image.open(second_img_path) new_image = Image.alpha_composite(im1, im2) return image.fromstring(new_image.tobytes(), new_image.size, new_image.mode)
def split_sprites(path, SCALE): res = [] im = Image.open(path) for i in range(im.size[0] // 80): tmp_im = im.crop((i * 80, 0, (i + 1) * 80, im.size[1])) tmp_el = image.fromstring(tmp_im.tobytes("raw", 'RGBA'), tmp_im.size, 'RGBA').convert_alpha() res.append( scale(tmp_el, [round(x * SCALE * 1.3) for x in tmp_el.get_size()])) return res
def __init__(self): with open('assets/blocks.json') as f: self.blocks = json.load(f) self.textures = {} for name in self.blocks.keys(): block_texture = self.blocks[name]['texture'] texture_x, texture_y, cid = block_texture['x'], block_texture[ 'y'], block_texture['catalog_id'] image = Image.open('imgs/blocks/tiles{}.png'.format(cid)) x, y = texture_x * 16, texture_y * 16 image = image.crop((x, y, x + 16, y + 16)) self.textures[name] = pim.fromstring(image.tobytes('raw', 'RGBA'), (16, 16), 'RGBA')
def __init__(self, gameMap, location, r, img, name=None): super().__init__(gameMap) # init the planet self.map = gameMap self.x, self.y = location self.r = r self.name = name self.teamNo = None self.units = PlanetUnits(self) self.map.pTexts.add(self.units) self.selected = False self.needsUpdate = True # create the image self.image = pgi.fromstring(img, (self.r * 2 + 1, self.r * 2 + 1), 'RGBA')
def __init__(self, im=None): # todo clean up, it a mess if isinstance(im, Surface): self.image = im elif isinstance(im, str): for start in builtins: if im.startswith(start + ":"): for file in os.listdir("scene/Media" + "/" + start): if file.startswith(im[len(start) + 1:]): im = "scene/Media/" + start + "/" + file self.image = image.load(im) elif isinstance(im, PIL.Image): self.image = image.fromstring(image.tostring(), image.size, image.mode) self.original = self.image self.filtering_mode = 0 # todo implement and add constants self.size = 0 # todo add geter
def splash(size, name, path="splash.png"): environ['SDL_VIDEO_WINDOW_POS'] = "center" display.set_caption(name) wininfo = display.Info() screensize = (wininfo.current_w, wininfo.current_h) desktop = ImageGrab.grab() screen = display.set_mode(size, NOFRAME, 32) background = image.load(path).convert_alpha() w, h = size w //= 2 h //= 2 desktop = desktop.crop((screensize[0] // 2 - w, screensize[1] // 2 - h, screensize[0] // 2 + w, screensize[1] // 2 + h)) string = desktop.tostring() desktop = image.fromstring(string, size, desktop.mode) desktop.blit(background, (0, 0)) screen.blit(desktop, (0, 0)) display.update()
def load_image(self, imagepath): # get original image as 8-bit since LUT operations are faster # than bitmap operations im = Image.open(imagepath) self.original_image = image.fromstring(im.tobytes(), im.size, "P") # create the palette palette = [] rgb = [] for val in im.getpalette(): rgb.append(val) if len(rgb) == 3: palette.append(rgb + [255]) rgb = [] ## print (palette[0]) self.original_image.set_palette(palette) self.original_image.set_colorkey([255, 255, 255]) self.original_rect = self.original_image.get_rect() self.base_image = transform.scale(self.original_image, self.size) self.image = self.base_image self.rect = self.original_rect
def get_background(self, name_tbl_index): start_addr = [0x2000, 0x2400, 0x2800, 0x2C00][name_tbl_index] buf_pixel_index = [[0] * 256 for _ in range(240)] sprite_index = (self._reg.ctrl >> 4 & 1) for row in range(30): for col in range(32): block_sprite_index = self._bus.read(start_addr) start_addr += 1 pattern = self._get_sprite(sprite_index, block_sprite_index) for r in range(8): for c in range(8): attr = self.get_attr(name_tbl_index, row * 8 + r, col * 8 + c) if pattern[r][c] == 0: addr = 0x3F00 else: addr = 0x3F00 + ((attr << 2) | pattern[r][c]) buf_pixel_index[row * 8 + r][col * 8 + c] = self._bus.read(addr) buffer = [] for row in range(240): for col in range(256): buffer.extend(PALETTES[buf_pixel_index[row][col]]) return Image.fromstring(bytes(buffer), (256, 240), 'RGB')
def pil_img_to_pygame_surf(img, color_format="RGBA"): if not HAS_PIL: raise Exception("PIL was not found on this machine.") size = img.size data = img.convert(color_format).tostring("raw", color_format) return fromstring(data, size, color_format)
def from_str(string, size=(8, 8)): string = decompress(b64decode(string)) return scale(fromstring(string, size, "RGBA"), (size[0] * 4, size[1] * 4))
def from_database(self, entity_manager) -> None: self.sprite = sprite.Sprite() self.sprite.image = image.fromstring(self.sql_image, (self.sql_width, self.sql_height), "RGBA") self.sprite.rect = self.sprite.image.get_rect()
def ImageToSprite(self, sprite): sprite = image.fromstring(sprite.tobytes(), sprite.size, sprite.mode) sprite = transform.scale(sprite, (Constants.TILESIZE, Constants.TILESIZE)) return sprite
def load_image(texture_x, texture_y, file): image = Image.open(ASSETS_DIR + 'mobs/' + file) x, y = texture_x * 16, texture_y * 16 image = image.crop((x, y, x + 16, y + 16)) return pim.fromstring(image.tobytes('raw', 'RGBA'), (16, 16), 'RGBA')
views = ["9ff,4ff,24","6ff,4ff,24","3ff,4ff,24", "ff,2ff,24"] for index, v in enumerate(views): # Pan the camera to the view params = urllib.urlencode({'AbsolutePanTilt': v}) f = urllib.urlopen("http://"+camera_ip+"/command/ptzf.cgi", params) f.read() # Collect image (color, and black and white versions) source = os.path.join('./', stamp + '.jpg') data = urllib.urlretrieve("http://"+camera_ip+"/oneshotimage.jpg", source) bwimg = cv.LoadImage(source, 0) img = cv.LoadImage(source) # Create pygame surfaces for bitblitting pygameimg = img.tostring() with_laughingman = image.fromstring(pygameimg, (WIDTH,HEIGHT), "RGB") # Detect faces and bitblit the laughingman faces = cv.HaarDetectObjects(bwimg, hc, cv.CreateMemStorage()) for (fx,fy,fw,fh),n in faces: (x,y,w,h) = (fx-10, fy-10, fw+20, fh+20) laughingman_scaled = scale(laughingman, (w,h)) with_laughingman.blit(laughingman_scaled, (x,y)) # Save the image image.save(with_laughingman, source) tweet = "Total people found in view ("+str(index+1)+"): "+str(len(faces)) print tweet
def load_image(self, texture_x, texture_y): image = Image.open(ASSETS_DIR + 'items/items.png') x, y = texture_x * 16, texture_y * 16 image = image.crop((x, y, x + 16, y + 16)) self.img = pim.fromstring(image.tobytes('raw', 'RGBA'), (16, 16), 'RGBA')
xf, yf = imgforet.get_size() xm, ym = imgmer.get_size() foret0 = image.tostring(imgforet, "RGB") mer0 = image.tostring(imgmer, "RGB") xif, yif = randint(0, xf-img_size), randint(0, yf-img_size) xim, yim = randint(0, xm-img_size), randint(0, ym-img_size) foret = b"" for y in range(img_size) : pos = (yif+y)*xf*3+xif*3 foret += foret0[pos : pos+img_size*3] mer = b"" for y in range(img_size) : pos = (yim+y)*xm*3+xim*3 mer += mer0[pos : pos+img_size*3] imgmer = image.fromstring(mer, [img_size]*2, "RGB") image.save(imgmer, "merrendu.png") print(len(foret), len(mer)/3/img_size) #Creation de l'image def pxl(x,y,c): return x*3+c + (img_size*3)*y rendu = "" for y in range(img_size) : for x in range(img_size) : if y < Y2[x] : rendu += mer[3*img_size*y+3*x : 3*img_size*y+3*x+3].decode(encoding='latin1') else : rendu += foret[3*img_size*y+3*x : 3*img_size*y+3*x+3].decode(encoding='latin1')
def from_pil_to_pygame(pil_image): mode = pil_image.mode size = pil_image.size data = pil_image.tostring() return image.fromstring(data, size, mode)
def load_image(self, texture_x, texture_y, cid): image = Image.open(ASSETS_DIR + 'blocks/tiles{}.png'.format(cid)) x, y = texture_x * 16, texture_y * 16 image = image.crop((x, y, x + 16, y + 16)) self.img = pim.fromstring(image.tobytes('raw', 'RGBA'), (16, 16), 'RGBA')
def _load_image(texture_x, texture_y): image = Image.open(ASSETS_DIR + 'player.png') x, y = texture_x * 12, texture_y * 15 image = image.crop((x, y, x + 12, y + 15)) return pim.fromstring(image.tobytes('raw', 'RGBA'), (12, 15), 'RGBA')
def save_targa(im, arg): """ Convert PIL image to pygame image and save. PIL cannot save tga and no other library works with PIL """ image.save(image.fromstring(im.tobytes(), im.size, im.mode), arg.split('.', 1)[0] + '.tga')
def _pop_image_from_stack(self): try: surface_string, self.upper_left, self.lower_right = self.images_stack.pop() popped_surface = image.fromstring(surface_string, (self.screen.get_width(), self.screen.get_height()), 'RGB') self.screen.blit(popped_surface, (0,0)) except IndexError: pass
def load_own_image(self, img, texture_x, texture_y): image = Image.open(img) x, y = texture_x * 16, texture_y * 16 image = image.crop((x, y, x + 16, y + 16)) self.img = pim.fromstring(image.tobytes('raw', 'RGBA'), (16, 16), 'RGBA')
def export_stack_to_images(self, filename): for num, img in enumerate(self.images_stack): name = filename + str(num) + '.bmp' surface = image.fromstring(img[0], (self.screen.get_width(), self.screen.get_height()), 'RGB') image.save(surface, name)