Testing image to character script? - ???

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.

We Are The Witches

True & Honest Fan
kiwifarms.net
Joined
Feb 23, 2019
███████████████████████
██████████████████████████
██████████████████████
█████████████████████
██████████████████
█████████████████
██████████████
██████████████
████████
████
███████
████
████
███










██
██
████
██████████
██████
████
█████
██████████
██████
████████
██████████
█████████
██████████████
████████████
████
██████████
█████
████████
██████████████
████████████████
████████████
██████████████
██████████████
████
██████████████
██████████████
██████████
██████████████
████████████
 
What I had was filled with bugs because I was trying to reinvent the wheel, but I also did for fun so it didn't matter. I decided to polish it a bit more, and even though the code is not super efficient (I'm sure people who are into programming could make a much better version since I'm just starting), it can do what it's supposed to do well enough.

The major obstacle for doing this from scratch would be image resizing & (proper and accurate) color compression, but since there is the Pillow library, all that can be handled to some of their functions.

This is the code (to be improved), all it needs is a somewhat recent version of Python and the Pillow library:
Python:
from PIL import Image
import pathlib

# choose image name.extension (same directory as this Python file)
filename = "image_test.png"

# choose boolean if for [BBCode] or for <span>
for_forum = False

# choose character to use
char_line = 0

# choose width of image (in characters), 32 for appropriate in forum (must provide an integer).
image_char_width = 80

# do you want to save a copy of the resized image, also choose a name
save_resize_image = True
save_resize_image_name = "resized_image"

####################################################################################################

# character list to choose from
char_list = [
  (0 , "█"), # appropriate for (pixel_width = 2; pixel_height = 1)
  (1 , "■"),
  (2 , "⧆"),
  (3 , "⛾"),
  (4 , "▩"),
  (5 , "⧉"),
  (6 , "⬚"),
  (7 , "◍"),
  (8 , "●"),
  (9 , "◙"),
  (10 , "◉"),
  (11 , "▦")
]

####################################################################################################

# open image and get extension
try:
  im = Image.open(filename)
except:
  print("Error.\nDid you put the correct extension?")
extension = pathlib.Path(filename).suffix

# chosen character assignment (selected at the beginning of script)
chosen_char = char_list[char_line][1]

# set filenames for the result
bbcode_filename = "bbcode_image"
html_filename = "span_image"

# sets boolean to choose if you want BBCode or <span> tags
if for_forum:
  start = "[COLOR=#"
  middle = "]"
  end = "[/COLOR]"

  added_chars = len(start) + len(middle) + len(end) + 6 + 3 # 6 for RGB, 3 for each special character (e.g: █)
else:
  start = "<span style=\"color:#"
  middle = "\">"
  end = "</span>"

  added_chars = len(start) + len(middle) + len(end) + 6 + 3

# sets color of forum background to appear transparent if done for forum
k_transparent = "383c42"
k_transparent_red = 56
k_transparent_green = 60
k_transparent_blue = 66

# checks if the image is RGB or RGBA
try:
  r, g, b, a = im.getpixel((0, 0))
  rgba = True
except:
  rgba = False

# checks the width & height of the image, and calculates its ratio
width, height = im.size
ratio = width / height

image_char_height = int(height / (width / image_char_width))

# resizes image to desired size based on input width
im_resized = im.resize((image_char_width, image_char_height))

# sets an empty matrix to hold the rows of pixels
pixels_matrix = []

# number of pixels in total as a result
total_chars = 0

####################################################################################################

if rgba:
  for output_im_h in range(image_char_height):
    row_pixels_matrix = []
    for output_im_w in range(image_char_width):
      r, g, b, a = im_resized.getpixel((output_im_w, output_im_h))
      if a == 0:
        rgb_hex = k_transparent
      else:
        r_hex = hex(r)[2:].zfill(2)
        g_hex = hex(g)[2:].zfill(2)
        b_hex = hex(b)[2:].zfill(2)
        rgb_hex = r_hex + g_hex + b_hex
      row_pixels_matrix.append(rgb_hex)
    pixels_matrix.append(row_pixels_matrix)
else:
  for output_im_h in range(image_char_height):
    row_pixels_matrix = []
    for output_im_w in range(image_char_width):
      r, g, b = im_resized.getpixel((output_im_w, output_im_h))
      r_hex = hex(r)[2:].zfill(2)
      g_hex = hex(g)[2:].zfill(2)
      b_hex = hex(b)[2:].zfill(2)
      rgb_hex = r_hex + g_hex + b_hex
      row_pixels_matrix.append(rgb_hex)
    pixels_matrix.append(row_pixels_matrix)

if for_forum:
  with open(f"{bbcode_filename}.txt", "w", encoding="utf-8") as txt_file:
    for pixel_row in pixels_matrix:
      for pixel in pixel_row:
        txt_file.write(f"{start}{pixel}{middle}{chosen_char}{end}")
        total_chars += added_chars
      txt_file.write(f"\n")
      total_chars += 2
  print(f"Total characters: {total_chars}\n")

else:
  with open(f"{html_filename}.html", "w", encoding="utf-8") as html_file:
    html_file.write(f"<!DOCTYPE html><html><head><style>body{{background:#000;}}</style></head><body>\n")
    for pixel_row in pixels_matrix:
      for pixel in pixel_row:
        html_file.write(f"{start}{pixel}{middle}{chosen_char}{end}")
      html_file.write(f"<br>\n")
    html_file.write(f"\n</body></html>\n")

# print(pixels_matrix)

if save_resize_image:
  im_resized.save(f"{save_resize_image_name}{extension}")

print("Done.")

You'd have to change the settings above to your liking. Like you'd provide the name.extension of the image, if you want it for Kiwifarms or just as HTML; the character that you want to do it with (now with "█"); the width you want it to have (you need to have in mind that each character/pixel takes a bunch of other characters, so set it low, because if you put a very high number and try to open the HTML file, it will have to load millions and millions of characters, so don't go overboard, for Kiwifarms, the character limit on posts is 64000, so you cannot do big images because it takes a lot of BBCode to do it like this), and if you want a copy of the resized image to be saved.

If you want it for Kiwifarms, you just set for_forum to True and then when executed, it will create a txt file that you can copy-paste to KF (you need to set a low enough width so that Kiwifarms' character limitation can allow you to post it).

* Example:










██





██






██






























█████
█████████
 
Back