def load_image(filename, transparent=False): image = pygame.image.load(filename) image = image.convert() if transparent: color = image.get_at((0, 0)) image.set_colorkey(color) return image
def __init__(self,image,x,y): pygame.sprite.Sprite.__init__(self) # set the rotation cycle if it hasn't been already if self.rot_cycle is None: self.rot_cycle = [3]*10 + [-3]*10 self.rot_cycle_pos = randint(0,len(self.rot_cycle)-1) self.rotation = randint(-30,30) # set the scale cycle if it hasn't been already if self.scale_cycle is None: steps = 5. self.scale_cycle = [i/steps for i in range(int(steps))] + [1.05, 1.1, 1.15, 1.1, 1.05, 1] self.scale_pos = 0 # need to store original image so quality stays the same and rect doesn't get huge self.orig_image = image # make a copy of the original image that the sprite uses to draw on self.image = image.convert() self.scale = uniform(0.1,0.75) self.image = pygame.transform.rotozoom(self.image,self.rotation,self.scale*self.scale_pos) self.rect = self.image.get_rect() self.x = x self.y = y self.orig_center = (x,y)
def render(self): screen_rect = self.get_screen_rect() gctx = PebbleGraphicsContext(self, None, None) self.harness.render(gctx) image = gctx.get_image() surface = pygame.image.fromstring( image.convert("RGBA").tostring(), image.size, "RGBA") if self.mult != 1: surface = pygame.transform.scale(surface, screen_rect.size) self.display.blit(surface.convert_alpha(self.display), screen_rect)
def load_png(path): """Load an image and return the image object. Args: path (str): The image file path to load. Returns: Surface: The loaded image. """ try: image = pygame.image.load(path) if image.get_alpha() is None: image.convert() else: image.convert_alpha() except pygame.error as message: print('Cannot load image: ', path) raise SystemExit(message) return image
def load_img(name): """Load an image as surface and convert it. Return an image and rect object""" path = os.path.join(IMG_DIR, name) image = pygame.image.load(path) if image.get_alpha is None: image = image.convert() else: image = image.convert_alpha() image_rect = image.get_rect() return image, image_rect
def load_png(name): """ Load image and return image object""" fullname = os.path.join('images', name) try: image = pygame.image.load(fullname) if image.get_alpha() is None: image = image.convert() else: image = image.convert_alpha() except pygame.error, message: print 'Cannot load image:', fullname raise SystemExit, message
def read_training(format_size=(16, 16), dir_name="lane_images", label_file="labels.json", extrapolate=True, grayscale=False): """ read_training will go into dir_name and look for a file called labels.json. It will then open all the images named in labels.json and convert them to images of size format_size. If extrapolate is True, then each image will be duplicated to its mirror version. - The mirrors of images labelled left or right will be labelled with their opposite direction - The mirrors of images labelled forward or backward will be labelled with the same direction. """ pathname = path.join(dir_name, label_file) with open(pathname, 'r') as lab: labels = json.load(lab) image_list = [] label_list = [] for image_name, label in labels.items(): # First open the image in the right format image_file_name = path.join(dir_name, image_name) image = Image.open(image_file_name) # Resize it and adapt it to our network image = image.resize(format_size) if grayscale: image = image.convert("L") image_list.append(list(image.getdata())) label_list.append(label) if extrapolate: mirror_image = image.transpose(method=Image.FLIP_LEFT_RIGHT) image_list.append(list(mirror_image.getdata())) if label == 'B' or label == 'F': label_list.append(label) elif label == 'L': label_list.append('R') elif label == 'R': label_list.append('L') else: raise Exception("Label unknown") return np.array(image_list), np.array(label_list)
def __init__(self, image, position, startalpha, endalpha, playtime, delay=0): #self.image = pygame.Surface( (image.get_width(),image.get_height()),pygame.locals.SRCALPHA) #self.image.blit(image,(0,0)) self.image = image.convert() self.startalpha = startalpha self.endalpha = endalpha self.playtime = playtime if startalpha < endalpha: self.delta = (endalpha - startalpha) / float(playtime) else: self.delta = (startalpha - endalpha) / float(playtime) self.delay = delay self.position = position
txt = font.render(message, True, (0, 0, 0)) return txt.get_width() # ------------------------------------------------------- # # Loads an image and converts it to the right bit depth # # ------------------------------------------------------- # def loadImage(name): name = os.path.join('images', name) try: image = pygame.image.load(name) except pygame.error, message: print "Failed loading image '%s', reason: '%s'" % (name, message) raise SystemExit, message image = image.convert() return image screen = None sound = None soundsEnabled = 1 testLevelname = "" def setScreenMode(flags, bpp): global screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), flags, bpp) def enableSounds(enable):
def __init__(self, image=None): if isinstance(image, Surface): image = Image.fromarray(surfarray.pixels3d(transform.flip(transform.rotate(image, 90), False, True)), mode="RGB") self.image = image.convert(mode="RGBA") if image else None
def load(self): self._image = pygame.image.load(self._name) if self._image.get_alpha() is None: self._image = image.convert() else: self._image = image.convert_alpha()
font = pygame.font.Font(FONT_FILENAME, fontsize) txt = font.render(message, True, (0,0,0)) return txt.get_width() # ------------------------------------------------------- # # Loads an image and converts it to the right bit depth # # ------------------------------------------------------- # def loadImage(name): name = os.path.join('images', name) try: image = pygame.image.load(name) except pygame.error, message: print "Failed loading image '%s', reason: '%s'" % (name, message) raise SystemExit, message image = image.convert() return image screen = None sound = None soundsEnabled = 1 testLevelname = "" def setScreenMode(flags,bpp): global screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), flags, bpp) def enableSounds(enable): global soundsEnabled soundsEnabled = enable