def mosaic(fid, numberOfTiles): image = Imager(fid) x = image.xmax y = image.ymax tileScale = int(x/numberOfTiles) new_x = tileScale*numberOfTiles new_y = int(y/tileScale)*tileScale scaled = image.resize(new_x, new_y) def get_tile(startx, starty): tile = [] for x in range(startx, startx+tileScale): for y in range(starty, starty+tileScale): tile.append(scaled.get_pixel(x, y)) return tile def build_tile(startxb, startyb): tile = tiles[random.randint(0, len(tiles)-1)] for x2 in range(startxb, startxb+tileScale): for y2 in range(startyb, startyb+tileScale): scaled.set_pixel(x2, y2, tile[(x2-startxb)*(y2-startyb)]) tiles = [] for x2 in range(0, new_x, tileScale): for y2 in range(0, new_y, tileScale): tiles.append(get_tile(x2, y2)) for w in range(0, new_x, tileScale): for z in range(0, new_y, tileScale): build_tile(w, z) scaled.display()
def selective_color(fid, color, tolerance): image = Imager(fid) def equal(pix1, pix2): l1 = pix1[0] / pix1[1] if pix1[1] != 0 else 0 l2 = pix1[0] / pix1[2] if pix1[2] != 0 else 0 s1 = pix2[0] / pix2[1] if pix2[1] != 0 else 0 s2 = pix2[0] / pix2[2] if pix2[2] != 0 else 0 equal1 = l1 < s1 + tolerance and l1 > s1 - tolerance equal2 = l2 < s2 + tolerance and l2 > s2 - tolerance return not (equal1 and equal2) def bw(pixel): lum = int((pixel[1] + pixel[2] + pixel[0]) / 3) return lum, lum, lum for y in range(image.ymax): for x in range(image.xmax): pixel = image.get_pixel(x, y) if equal(pixel, color): image.set_pixel(x, y, bw(pixel)) image.display()