๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

Image to Text…? ์ด๋ฏธ์ง€๋กœ ์•„์Šคํ‚ค ์•„ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

by nowgeun 2023. 2. 11.
728x90

Spark๋ฅผ ์„ค์น˜ํ•ด ๋ณด์‹  ๋ถ„์ด๋ผ๋ฉด ์•„๋ž˜์˜ ์ด๋ฏธ์ง€๊ฐ€ ์นœ์ˆ™ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Spark๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋ฌธ์ž๋ฅผ ์ด์šฉํ•ด์„œ ํ…์ŠคํŠธ๋กœ ๊ทธ๋ฆฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋น„์Šทํ•œ ๋ฅ˜์˜ ๋ฌธ์ž๋กœ ๋œ ๊ทธ๋ฆผ์€ 2021๋…„๋„์ฏค ๋„ค์ด๋ฒ„ ์›นํˆฐ ๋Œ“๊ธ€์—์„œ๋„ ๋งŽ์ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๊ธ€์ž๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์„ ASCII Art๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ˆœํ•˜๊ฒŒ ๋ณต๋ถ™ ํ•ด์„œ ๋ณด๋‚ด๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜ ์ด๋ฏธ์ง€์— ๋น„ํ•˜๋ฉด ๊ณต์œ ๊ฐ€ ์ˆ˜์›”ํ•œ ํŽธ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š”  :)   ^_^    ์ด๋Ÿฌํ•œ ์ด๋ชจํ‹ฐ์ฝ˜๋“ค๋„ ์•„์Šคํ‚ค ์•„ํŠธ์˜ ๋ฒ”์ฃผ์— ์†ํ•ฉ๋‹ˆ๋‹ค. (ํ•œ ์ค„์งœ๋ฆฌ ์•„์Šคํ‚ค ์•„ํŠธ)

ํ˜„์žฌ ๋‹ค๋‹ˆ๋Š” ์ง์žฅ์—์„œ๋„ ์•„์Šคํ‚ค ์•„ํŠธ๋ฅผ ๊ฐ€๋” ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ๋‚ด ๋ฉ”์‹ ์ €์˜ ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐ€์ง“ ์ˆ˜๊ฐ€ ๋งค์šฐ ์ ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๋ฅผ ์•„์Šคํ‚ค ์•„ํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฉ”๋ชจ์žฅ์— ์ €์žฅํ•ด๋‘๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž.

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Python์œผ๋กœ ์•„์Šคํ‚ค ์•„ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒ๊ฐ ์™ธ๋กœ ์›๋ฆฌ๋Š” ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค:

 

1. ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค

2. ์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์•Œ๋งž๊ฒŒ ์กฐ์ •ํ•œ๋‹ค

3. ์ด๋ฏธ์ง€๋ฅผ ํ‘๋ฐฑ(Greyscale)์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค

4. ํ‘๋ฐฑ ์ด๋ฏธ์ง€์˜ Byte์ •๋ณด๋ฅผ ASCII ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•œ๋‹ค

 

Doge Dog
๋„์ง€์ฝ”์ธ ์‹œ๋ฐ”๊ฒฌ

์œ„์˜ ๊ท€์—ฌ์šด ์‹œ๋ฐ”๊ฒฌ ์ด๋ฏธ์ง€๋ฅผ Python์„ ์‚ฌ์šฉํ•ด์„œ ASCII Art๋กœ ๋ณ€ํ™˜ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

from PIL import Image

# ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ๋•Œ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ASCII ๋ฌธ์ž
### ๊ฐ€์žฅ ์–ด๋‘์šด ์ƒ‰ ํ‘œํ˜„ ๋ฌธ์ž --------> ๊ฐ€์žฅ ๋ฐ์€ ์ƒ‰ ํ‘œํ˜„ ๋ฌธ์ž
ascii_str = ["@", "#", "๏ผ„", "%", "*", "+", ";", ":", ",", "."]

##### DEFINE FUNCTION #####
# ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์กฐ์ •
def resize(image, resized_w=100):
    w, h = image.size
    resized_h = int(resized_w*h/w)
    return image.resize((resized_w, resized_h))

# ํ‘๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜
def greyscalify(image):
    return image.convert('L')

# ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ ASCII๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•˜๊ธฐ
def convert(image, resized_w):
    pix = image.getdata()
    ascii_ = ''
    for dot in pix:
        ascii_ += ascii_str[dot//26]
    
    # ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ๋Œ€๋กœ ์ž๋ฅด๊ธฐ
    ascii_str_len = len(ascii_)
    img_w = image.width
    
    ascii_art = ""
    for i in range(0, ascii_str_len, img_w):
        ascii_art += ascii_[i:i+img_w] + '\n'

    return ascii_art


##### CODE START #####


#1. ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—ด๊ธฐ

## ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ
path = './shibainu.jpeg'

## ์ด๋ฏธ์ง€ ์—ด๊ธฐ
image1 = Image.open(path)

#2. ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์กฐ์ •
resized_width = 50
image = resize(image1, resized_width)

#3. ์ด๋ฏธ์ง€ ํ‘๋ฐฑํ™”
greyscale = greyscalify(image)

#4. ์ด๋ฏธ์ง€ ๋‚ด ํ”ฝ์…€ ๊ฐ’ ASCII๋กœ ๋ณ€ํ™˜
ascii_art = convert(greyscale, resized_width)


# ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ €์žฅ
with open('ascii_art.txt','w') as f:
    f.write(ascii_art)

> ๊ฒฐ๊ณผ๋ฌผ: 

shibainu ascii art
์‹œ๋ฐ”๊ฒฌ ์•„์Šคํ‚ค ์•„ํŠธ

๐Ÿ’กTip

์•„์Šคํ‚ค ์•„ํŠธ๋กœ ์ด๋ฏธ์ง€ ๋ณ€ํ™˜์„ ์ž˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ƒ‰์˜ ๊ฒฝ๊ณ„๊ฐ€ ๋šœ๋ ทํ•œ ์‚ฌ์ง„์„ ๊ณ ๋ฅด์‹œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€