Python 이미지를 ASCII 온라인으로

27157 단어 pythontooling

소개



최근에 Python을 사용하여 사진을 ASCII 이미지로 변환하는 재미있는 Python 게임 방법을 찾았습니다. 대부분은 로컬 프로그램입니다. to-ASCII.

Online Python editor: https://lwebapp.com/en/python-playground



분석



Image-to-ASCII 페인팅의 기본 아이디어는
  • PIL을 사용하여 이미지의 각 픽셀에 대한 RGB 색상 값을 가져옵니다
  • .
  • 그런 다음 RGB 색상 값을 문자열에 매핑합니다
  • .
  • 마지막으로 문자열을 함께 연결하고 터미널에 인쇄하거나 텍스트로 저장하면 ASCII 이미지를 볼 수 있습니다.

  • 우리는 데모의 편의를 위해 여기에 있으며 print 방법을 사용하여 문자를 균일하게 인쇄합니다.

    암호



    웹 페이지에 표시하고 싶기 때문에 이미지를 로컬로 로드할 수 없으며 base64 인코딩에서 직접 이미지를 로드하거나 원격 URL에서 이미지를 로드하도록 선택합니다. 여기에서는 이미지를 로드하는 방법을 소개합니다.

    1. base64 이미지 사용



    정규 표현식을 사용하여 그림 base64 인코딩 문자열의 헤더 정보를 제거한 다음 base64 메서드를 사용하여 디코딩하고 BytesIOPIL와 협력하여 PIL 그림 인스턴스로 엽니다.

    Here I recommend my Online Image to Base64 tool, which supports directly pasting images and converting them into base64 strings, which is very convenient



    import base64
    import re
    from io import BytesIO
    from PIL import Image
    
    # random string
    char = list('M3NB6Q#OC?7>!:–;. ')
    
    # color value map string
    def get_char(r, g, b, alpha=256):
        if alpha == 0:
            return ' '
    
        grey = (2126 * r + 7152 * g + 722 * b) / 10000
    
        char_idx = int((grey / (alpha + 1.0)) * len(char))
        return char[char_idx]
    
    # image base64 string
    img_base64 = '''data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAHR5JREFUeF7tnQvUPlVVxh9FES1CU9SCLmhKoHkBwbxLamgm2sULmFqCrZVkllqaWmpGeb+irWVgmgmplYK3zPsNTQJTC8VL1ApKRVOkC6FY6wdn/r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8AyzmIZUYv3Xufp5F18B4t+/+wbgQK8D7xSA4XfThUbwcm26AAT/Bh0RgLjUvwnLraECZLl9X1seYYEKkAgj1VuWa4EKkOX2fW15hAX+H2d/zQWBav7aAAAAAElFTkSuQmCC'''
    
    # Regularly delete header information, that is, Data URI scheme
    image_data = re.sub('^data:image/.+;base64,', '', img_base64)
    # open image
    img = Image.open(BytesIO(base64.b64decode(image_data)))
    
    img_widht = img.size[0]
    img_height = img.size[1]
    
    # set zoom ratio
    scale_width = 0.3 # 0.75
    scale_height = 0.1 # 0.5
    
    # zoom image
    img = img.resize((int(img_widht*scale_width),
                     int(img_height*scale_height)), Image.NEAREST)
    
    # output ASCII image
    text = ''
    for i in range(int(img_height*scale_height)):
        for j in range(int(img_widht*scale_width)):
            text += get_char(*img.getpixel((j, i)))
        text += '\n'
    
    print(text)
    


    위의 코드를 로컬에 저장하거나 Online Python Editor에 복사하고 실행하여 효과를 확인하십시오. (실행하기 전에 베개를 설치해야 합니다.)

    2. URL에서 이미지 불러오기



    로컬 애플리케이션인 경우 내장 Python 라이브러리인 requests 를 사용하여 네트워크 이미지를 로드할 수 있습니다. 이 온라인 Python 도구는 pyodide를 사용하고 요청을 지원하지 않으므로 네트워크 요청을 구현하려면 공식 APIpyodide.http.pyfetch를 사용해야 합니다.

    response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')
    


    이미지 데이터를 가져온 후 PIL로 열 수도 있습니다. 자세한 코드는 다음과 같습니다

    from io import BytesIO
    from PIL import Image
    import pyodide
    
    char = list('M3NB6Q#OC?7>!:–;. ')
    
    
    def get_char(r, g, b, alpha=256):
        if alpha == 0:
            return ' '
    
        grey = (2126 * r + 7152 * g + 722 * b) / 10000
    
        char_idx = int((grey / (alpha + 1.0)) * len(char))
        return char[char_idx]
    
    
    def write_file(out_file_name, content):
        with open(out_file_name, 'w') as f:
            f.write(content)
    
    # Python API provided by pyodide for network requests, reference: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch
    response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')
    
    image_data = await response.bytes()
    img = Image.open(BytesIO(image_data))
    
    img_widht = img.size[0]
    img_height = img.size[1]
    
    scale_width = 0.3 # 0.75
    scale_height = 0.1 # 0.5
    
    img = img.resize((int(img_widht*scale_width),
                     int(img_height*scale_height)), Image.NEAREST)
    
    text = ''
    for i in range(int(img_height*scale_height)):
        for j in range(int(img_widht*scale_width)):
            text += get_char(*img.getpixel((j, i)))
        text += '\n'
    
    print(text)
    


    온라인 데모: Python Image to ASCII - URL Image

    결론



    위의 내용은 Python에서 이미지를 ASCII로 변환하는 방법을 요약한 것입니다. 웹 페이지에서 실행하고 친구들과 공유할 수 있습니다. 미흡한 부분이 있을 수 있으니 많은 관심 부탁드립니다. 앞으로 더 흥미로운 Python 사용법이 있을 것입니다. 내 업데이트를 팔로우하는 것을 환영합니다.

    참조


  • Python Image to ASCII Online
  • Python Online Editor
  • Image to Base64
  • pyodide
  • 좋은 웹페이지 즐겨찾기