def create_ascii_matrix(image, max_width, method="luminosity"): print("Image {} loaded successfully...".format(IMAGE)) print("Image size: {}x{}".format(image.width, image.height)) print("Image format: {} ; Image mode: {}".format(image.format, image.mode)) image = downscale_image(image, max_width) print("Image size after downscaling: {}x{}".format(image.width, image.height)) pixel_matrix = numpy.asarray(image) brightness_matrix = convert_to_brightness_matrix(pixel_matrix, method) # Find max and min values -> [Min: 0, Max: 255] max_b, min_b = -1000, 1000 for row in brightness_matrix: for pixel in row: if pixel > max_b: max_b = pixel if pixel < min_b: min_b = pixel print("Brightness Min: {} ; Max: {}".format(min_b, max_b)) # Convert brightness to ascii chars and append to new matrix variance = max_b - min_b print(f"variance: {variance}") m = math.ceil( variance / len(CONVERSION_STRING) ) # calculates the brightness range a single char has to cover print(f"Brightness range per char: {m}") ascii_matrix = [] col_s, row_s = len(brightness_matrix[0]), len(brightness_matrix) pixel_c = col_s * row_s pre = f'Converting Brightness-Matrix to ASCII-Matrix using the conversion string\n\n"{CONVERSION_STRING}"\n' pb = ProgressBar(pixel_c, 'pixels', pre=pre, post='Finished conversion!') pb.print_progress() p_count = 0 for y in range(0, len(brightness_matrix)): ascii_matrix.append([]) for x in range(0, len(brightness_matrix[y])): p_count += 1 char_index = round(brightness_matrix[y][x] / m) if char_index > len(CONVERSION_STRING) - 1: if DEBUG: pb.print_progress( p_count, f"Char index out of bounds: {brightness_matrix[y][x] / m} => ~{char_index};" f" The value was corrected to {len(CONVERSION_STRING) - 1}" ) char_index = len(CONVERSION_STRING) - 1 else: pb.print_progress(p_count) ascii_matrix[y].append(CONVERSION_STRING[char_index]) return ascii_matrix
def ex1(): """ **Example 1** :: pb1 = ProgressBar(100, '%', pre='Downloading file', post='Download finished', length=25) pb1.print_progress() # Prints the initial empty progress bar for mb in range(1, 101): pb1.print_progress(mb) sleep(0.15) **Output:** :: Downloading file [========================>] - Finished 100 % of 100 % Download finished """ pb1 = ProgressBar(100, '%', pre='Downloading file', post='Download finished', length=25) pb1.print_progress() # Prints the initial empty progress bar for mb in range(1, 101): pb1.print_progress(mb) sleep(0.15)
def ex5(): with open('example.txt', 'r') as f: pb5 = ProgressBar(len(f.readlines()), 'lines', pre="Reading lines from file {}".format(f.name), post='Finished reading file!') f.seek(0) # Return to start of line after obtaining line count pb5.print_progress() # Prints the initial empty progress bar for lineno, line in enumerate(f, start=1): pb5.print_progress(lineno, pre=line.replace('\n', '')) sleep(1)
def ex2(): """ **Example 2** :: pb2 = ProgressBar(500, 'MB', pre='Downloading file', post='Download finished', head='#') pb2.print_progress() # Prints the initial empty progress bar`` for mb in range(1, 501): pb2.print_progress(mb) sleep(0.02) **Output:** :: Downloading file [=================================================#] - Finished 500 MB of 500 MB Download finished """ pb2 = ProgressBar(500, 'MB', pre='Downloading file', post='Download finished', head='#') pb2.print_progress() # Prints the initial empty progress bar for mb in range(1, 501): pb2.print_progress(mb) sleep(0.02)
def convert_to_brightness_matrix(rgb_matrix, method): col_s, row_s = len(rgb_matrix[0]), len( rgb_matrix) # TODO check if order is right pixel_c = col_s * row_s bm = [] pre = f'Converting RGB-Matrix to Brightness-Matrix [RGB ==({method})==> Brightness]...\nPixel matrix size: {col_s} cols x {row_s} rows.' pb = ProgressBar(pixel_c, 'pixels', pre=pre, post='Finished conversion!') pb.print_progress() p_count = 0 # Use the Luminosity formula to convert RGB values to a single brightness value for y in range(0, row_s): bm.append([]) for x in range(0, col_s): if method == "lum": # Luminosity bm[y].append( int((0.21 * rgb_matrix[y, x, 0]) + (0.72 * rgb_matrix[y, x, 1]) + (0.07 * rgb_matrix[y, x, 2]))) elif method == "avg": # Average bm[y].append( int((int(rgb_matrix[y, x, 0]) + int(rgb_matrix[y, x, 1]) + int(rgb_matrix[y, x, 2])) / 3)) elif method == "light": # Lightness bm[y].append( int( int((max(rgb_matrix[y, x, 0], rgb_matrix[ y, x, 1], rgb_matrix[y, x, 2])) + int( min(rgb_matrix[y, x, 0], rgb_matrix[y, x, 1], rgb_matrix[y, x, 2])) / 2))) else: print("The method {} does't exist.".format(method)) return -1 p_count += 1 pb.print_progress(p_count) return bm
def ex3(): """ **Example 3** :: pb3 = ProgressBar(1000.12, 'MB', pre='Downloading file', post='Download finished', length=100) pb3.print_progress() # Prints the initial empty progress bar for mb in range(1, 1001): if mb != 1000 and mb % 2 == 0: mb = mb + 0.5 elif mb != 1000: mb = mb + 0.25 else: mb = mb + 0.12 pb3.print_progress(mb) sleep(0.025) **Output:** :: Downloading file [===================================================================================================>] - Finished 1000.12 MB of 1000.12 MB Download finished """ pb3 = ProgressBar(1000.12, 'MB', pre='Downloading file', post='Download finished', length=100) pb3.print_progress() # Prints the initial empty progress bar for mb in range(1, 1001): if mb != 1000 and mb % 2 == 0: mb = mb + 0.5 elif mb != 1000: mb = mb + 0.25 else: mb = mb + 0.12 pb3.print_progress(mb) sleep(0.025)
'color-stop(0.15, #f2f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), ' 'color-stop(0.9, #ff2), color-stop(1, #f22) );' 'background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f)' ', color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), ' 'color-stop(0.9, #ff2), color-stop(1, #f22) );' 'color: transparent;' '-webkit-background-clip: text;' 'background-clip: text;' 'width: max-content;}\r\n' '</style>\r\n</head>\r\n' '<pre style="font-family: monospace;line-height: 3px; font-size: 3px;" class="rainbow">\r\n' ) pb = ProgressBar( ascii_count, 'characters', pre=f'Inserting {ascii_count} ASCII characters into {file_name}...', post='Finished generating HTML file!') pb.print_progress() c = 1 for r in ascii_matrix: line = "" for p in r: line += p * 3 pb.print_progress(c) c += 1 f.write(line + "\r\n") f.write("</pre>\r\n</html>") f.close() # SAVE AS NEW ASCII IMAGE
def ex4(): pb4 = ProgressBar(5, 'files', pre='Deleting files', post='Finished!', length=25, empty='*', fill='#') pb4.print_progress() # Prints the initial empty progress bar for file in range(1, 6): pb4.print_progress(file, pre="Deleting file file{}.txt".format(file)) sleep(1)