made filter better
This commit is contained in:
parent
d41ca10b19
commit
6735153a20
119
sobel.py
119
sobel.py
@ -1,11 +1,13 @@
|
||||
# import scipy.ndimage as nd
|
||||
# import imageio.v3 as iio
|
||||
import numpy as np
|
||||
# import numpy as np
|
||||
import math
|
||||
import os
|
||||
|
||||
from PIL import Image, ImageFilter
|
||||
import pygame as pg
|
||||
|
||||
IMAGE_PATH = 'sample-images/sunflower.jpg'
|
||||
IMAGE_PATH = 'sample-images/engine.PNG'
|
||||
|
||||
BLUR_STRENGTH_1 = 1
|
||||
BLUR_STRENGTH_2 = 2.5
|
||||
@ -18,27 +20,34 @@ TEXT_WIDTH = 6 # on font size 12 monocraft medium
|
||||
TEXT_HEIGHT = 11
|
||||
|
||||
SCALE_FACTOR = 2
|
||||
RESIZE_FACTOR = 2
|
||||
|
||||
SCALED_WIDTH = math.floor(TEXT_WIDTH / SCALE_FACTOR)
|
||||
SCALED_HEIGHT = math.floor(TEXT_HEIGHT / SCALE_FACTOR)
|
||||
|
||||
DEBUG = True
|
||||
|
||||
CHARS = list(reversed([
|
||||
' ',
|
||||
if not os.path.exists('./debug/') and DEBUG:
|
||||
os.makedirs('./debug')
|
||||
|
||||
# '█@?OPoci. '
|
||||
CHARS = [
|
||||
u'█',
|
||||
'@',
|
||||
'?',
|
||||
'O',
|
||||
'P',
|
||||
'o',
|
||||
'c',
|
||||
'i',
|
||||
'.',
|
||||
'_',
|
||||
'+',
|
||||
'=',
|
||||
'A',
|
||||
'K',
|
||||
'B',
|
||||
'&',
|
||||
'#'
|
||||
]))
|
||||
' '
|
||||
]
|
||||
|
||||
def main():
|
||||
|
||||
image = Image.open(IMAGE_PATH)
|
||||
image = image.resize((image.width * RESIZE_FACTOR, image.height * RESIZE_FACTOR))
|
||||
|
||||
L_image = image.convert('L')
|
||||
|
||||
@ -99,7 +108,7 @@ def main():
|
||||
if gradient_v:
|
||||
char = match_gradient(gradient_v)
|
||||
else:
|
||||
char = CHARS[round(L_image.getpixel((real_x, real_y))/(255/10)) - 1]
|
||||
char = CHARS[round(L_image.getpixel((real_x, real_y))/(255/len(CHARS))) - 1]
|
||||
|
||||
if char in histogram:
|
||||
histogram[char] += 1
|
||||
@ -125,49 +134,75 @@ def main():
|
||||
|
||||
y_offset += SCALED_HEIGHT
|
||||
|
||||
|
||||
|
||||
# for y in range(0, text_grind_height):
|
||||
# for x in range(0, text_grid_width):
|
||||
# gradient_v = gradient[y * (w * TEXT_WIDTH) + x]
|
||||
|
||||
# char = ' '
|
||||
|
||||
# if gradient_v:
|
||||
# char = match_gradient(gradient_v)
|
||||
# else:
|
||||
# char = CHARS[round(L_image.getpixel((x, y))/(255/10)) - 1]
|
||||
|
||||
# rendered_char = font.render(char, 1, WHITE)
|
||||
# window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT))
|
||||
|
||||
pg.display.update()
|
||||
pg.image.save(window, "render.png")
|
||||
pg.quit()
|
||||
|
||||
if DEBUG:
|
||||
sb.save('debug/sobel-dog.png')
|
||||
dog.save('debug/dog.png')
|
||||
|
||||
sb.save('sobel-dog.png')
|
||||
# Draw only the ASCII edges
|
||||
pg.init()
|
||||
font = pg.font.SysFont("Monocraft Medium", 12)
|
||||
window = pg.display.set_mode((text_grid_width * TEXT_WIDTH, text_grind_height * TEXT_HEIGHT))
|
||||
|
||||
# dog.save('dog.png')
|
||||
y_offset = 0
|
||||
|
||||
# sb_x = dog.filter(sobel_x_kernel)
|
||||
for y in range(math.floor(h / SCALED_HEIGHT)):
|
||||
x_offset = 0
|
||||
|
||||
# sb_x.save('pil-sobel_x.png')
|
||||
for x in range(math.floor(w / SCALED_WIDTH)):
|
||||
histogram = {}
|
||||
|
||||
# image_np = np.array(dog)
|
||||
# Collect the most common char for a group of pixels
|
||||
colors = []
|
||||
|
||||
# sb_x = nd.sobel(image_np, 1)
|
||||
# sb_y = nd.sobel(image_np, 0)
|
||||
for y2 in range(SCALED_HEIGHT):
|
||||
for x2 in range(SCALED_WIDTH):
|
||||
|
||||
# combined_sobel = np.sqrt(np.square(sb_x) + np.square(sb_y))
|
||||
real_x = x2 + x_offset
|
||||
real_y = y2 + y_offset
|
||||
|
||||
# i = Image.fromarray(combined_sobel, 'L')
|
||||
gradient_v = gradient[real_y * w + real_x]
|
||||
|
||||
# i.save('sobel.png')
|
||||
char = ' '
|
||||
|
||||
# iio.imwrite('sobel_x.png', sb_x)
|
||||
# iio.imwrite('sobel_y.png', sb_y)
|
||||
if gradient_v:
|
||||
char = match_gradient(gradient_v)
|
||||
else:
|
||||
char = ' '
|
||||
|
||||
if char in histogram:
|
||||
histogram[char] += 1
|
||||
else:
|
||||
histogram[char] = 1
|
||||
|
||||
colors.append(image.getpixel((real_x, real_y)))
|
||||
|
||||
color_avg = average_colors(colors)
|
||||
|
||||
# get most common
|
||||
most_common = None
|
||||
score = float('-inf')
|
||||
for char in histogram:
|
||||
if histogram[char] > score:
|
||||
score = histogram[char]
|
||||
most_common = char
|
||||
|
||||
rendered_char = font.render(most_common, 1, color_avg)
|
||||
window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT))
|
||||
|
||||
x_offset += SCALED_WIDTH
|
||||
|
||||
y_offset += SCALED_HEIGHT
|
||||
|
||||
pg.display.update()
|
||||
pg.image.save(window, "debug/edges-render.png")
|
||||
pg.quit()
|
||||
|
||||
|
||||
|
||||
|
||||
def subtract_colors(t1: tuple, t2: tuple) -> tuple:
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user