def embed_image(message: IO[str], image: pygame.Surface, num_bits_modify: int) -> None: """ Mutates the given image to embed the given message within it Arguments: message -- the file object containing the wanted message image -- the pygame Surface with pixel data num_bits_modify -- the number of bits to modify in each pixel value """ if (num_bits_modify > 8): raise Error('No more than 8 bits per pixel\'s channel') elif (num_bits_modify <= 0): raise Error('Must modify at least 1 bit per pixel channel') image.lock() image_buffer = image.get_view('1') image_bytes = image_buffer.raw image_bytes_length = len(image_bytes) current_byte_index = 0 stride_size = 8 * num_bits_modify bit_mask = construct_mask(num_bits_modify) inverted_mask = construct_inverted_mask(num_bits_modify) current_message_stream = message.read(stride_size) while len(current_message_stream) > 0: if len(current_message_stream) < stride_size: current_message_stream += b'\xFF' # Grab the value from the message bits = int.from_bytes(current_message_stream[::-1], byteorder='big') i = 0 while (i * num_bits_modify) < (len(current_message_stream) * 8) + ( (len(current_message_stream) * 8) % num_bits_modify): value_to_embed = (bits & (bit_mask << (num_bits_modify * i))) >> (num_bits_modify * i) current_image_byte = image_bytes[current_byte_index] current_image_byte = (current_image_byte & inverted_mask) | value_to_embed image_buffer.write(bytes([current_image_byte]), current_byte_index) i += 1 current_byte_index += 1 if current_byte_index >= len(image_bytes): raise Exception('Message too big for image') current_message_stream = message.read(stride_size) image.unlock()
def render(screen: pygame.Surface, objects: Tuple[Geometry], lights: Tuple[Light]): screen.lock() for j in range(-int(SCREEN[1] / 2), int(SCREEN[1] / 2)): for i in range(-int(SCREEN[0] / 2), int(SCREEN[0] / 2)): x = (2 * (i + 0.5)) / (SCREEN[0] - 1) * math.tan(FOV / 2.0) * int( SCREEN[0]) / SCREEN[1] y = -(2 * (j + 0.5)) / (SCREEN[1] - 1) * math.tan(FOV / 2.0) direction = Vector(x, y, -1).normalize() screen.set_at((i + int(SCREEN[0] / 2), j + int(SCREEN[1] / 2)), cast_ray(Vector(0, 0, 0), direction, tuple(objects), tuple(lights))) pygame.display.flip() screen.unlock()
def prepare_font_image(image: pg.Surface, scale_factor: int) -> pg.Surface: try: image.lock() for y in range(image.get_height()): for x in range(image.get_width()): r, g, b, a = image.get_at((x, y)) if (r, g, b, a) == (0, 0, 0, 255): image.set_at((x, y), (255, 255, 255, 255)) else: image.set_at((x, y), (0, 0, 0, 0)) finally: image.unlock() old_width = image.get_width() old_height = image.get_height() new_width = old_width * scale_factor new_height = old_height * scale_factor return pg.transform.scale(image, (new_width, new_height))
return noise.snoise2((x+cdx),(y+cdy),1,1) > 0.5 mdx = randint(-2048,2048) mdy = randint(-2048,2048) def spouses(x, y): global mdx global mdy m = noise.snoise2((x+mdx),(y+mdy),1,1) m = max(0, m) return int(4*m) + 1 shownoise = '-shownoise' in [a.lower() for a in argv] background = Surface(screen.get_size()) if shownoise: background.lock() for y in range(0, background.get_height()): for x in range(0, background.get_width()): background.set_at((x,y), grayvalue(noiseat(x,y))) background.unlock() else: background.fill((255,255,255)) screen.blit(background, (0,0)) sprites = Group() def personat(x,y): return noiseat(x*8,y*8) <= 0.95 for x in range(0, background.get_width(), 8):
def render(self, text, antialias = True, forecolor=(255, 255, 255, 255), backcolor=(255, 255, 255, 0)): size = self.size(text) img = NSImage.alloc().initWithSize_(size) img.lockFocus() NSString.drawAtPoint_withAttributes_(text, (0.0, 0.0), { NSFontAttributeName: self._font, NSUnderlineStyleAttributeName: self._isUnderline and 1.0 or 0, NSBackgroundColorAttributeName: _getColor(backcolor),# or None, NSForegroundColorAttributeName: _getColor(forecolor), }) rep = NSBitmapImageRep.alloc().initWithFocusedViewRect_(((0.0, 0.0), size)) img.unlockFocus() if rep.samplesPerPixel() == 4: s = Surface(size, SRCALPHA|SWSURFACE, 32, [-1<<24,0xff<<16,0xff<<8,0xff]) a = Numeric.reshape(Numeric.fromstring(rep.bitmapData(), typecode=Numeric.Int32), (size[1], size[0])) blit_array(s, Numeric.swapaxes(a, 0, 1)) #print "surface size is ", size filter_size = self.shadow_filter_size() border = self.border_size() result = [] result_size = (size[0] + border*2, size[1] + border*2) #print "result size is ", result_size for y in range(result_size[1]): result.append([(0, 0, 0, 0)] * result_size[0]) # Filter the character appropriately. s.lock() for y in range(size[1]): for x in range(size[0]): color = s.get_at((x, y)) s.set_at((x, y), (255, 255, 255, color[3])) if filter_size == 3: factors = [[1, 2, 1], [2, 4, 2], [1, 2, 1]] elif filter_size == 5: factors = [[1, 4, 6, 4, 1], [4, 16, 24, 16, 4], [6, 24, 36, 24, 6], [4, 16, 24, 16, 4], [1, 4, 6, 4, 1]] elif filter_size == 7: factors = [[1, 6, 15, 20, 15, 6, 1], [6, 36, 90, 120, 90, 36, 6], [15, 90, 225, 300, 225, 90, 15], [20, 120, 300, 400, 300, 120, 20], [15, 90, 225, 300, 225, 90, 15], [6, 36, 90, 120, 90, 36, 6], [1, 6, 15, 20, 15, 6, 1]] else: print "factors for filter size", filter_size, "not defined!" factors = 0 # Filter a shadow. for ry in range(result_size[1]): for rx in range(result_size[0]): inpos = (rx - int(border), ry - int(border)) count = 0 colorsum = 0 u = 0 v = 0 for a in filter_range(filter_size): v = 0 for b in filter_range(filter_size): x, y = (inpos[0] + a, inpos[1] + b) factor = factors[u][v] if x >= 0 and y >= 0 and x < size[0] and y < size[1]: colorsum += factor * s.get_at((x, y))[3] count += factor v += 1 u += 1 if count > 0: result_color = (int(colorsum) + count/2) / count else: result_color = 0 result[ry][rx] = (0, 0, 0, min(255, result_color*1.4)) # Blend the glyph itself to the result. for y in range(size[1]): for x in range(size[0]): color = s.get_at((x, y)) result[border + y][border + x] = \ alpha_blend(color, result[border + y][border + x]) result[border + y][border + x] = \ alpha_blend(color, result[border + y][border + x]) #result[border + y][border + x] = \ # alpha_blend(color, result[border + y][border + x]) s.unlock() glyph_rgba = Numeric.array(result, typecode=Numeric.UnsignedInt8) return glyph_rgba, result_size
class VisualTimer(object): """ Display a timer """ def __init__(self, finish, rect=None, color=(255, 255, 255)): self.time = 0 self.finish = float(finish) if rect == None: rect = Rect(0, 0, 100, 16) self.rect = Rect(rect) self.size = self.rect.width self.color = color self.image = Surface(self.rect.size) self.finished = 0 def set_alarm(self, time): self.finish = float(time) self.reset() def reset(self): self.time = 0 self.finished = 0 def update(self, time): if self.finished: return time += self.time if time <= self.finish: self.time = time else: self.finished = 1 def draw(self, surface): if not self.finished: self.render() surface.blit(self.image, self.rect.topleft) def render(self): i = ceil(self.size * (self.time / self.finish)) w, h = self.rect.size self.image.lock() self.image.fill((32, 32, 32)) self.image.fill(self.color, (0, 0, i, self.rect.height)) # Make the corners look pretty self.image.fill((0, 0, 0), (0, 0, 1, 1)) self.image.fill((0, 0, 0), (w - 1, 0, 1, 1)) self.image.fill((0, 0, 0), (w - 1, h - 1, 1, 1)) self.image.fill((0, 0, 0), (0, h - 1, 1, 1)) self.image.unlock()