14
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DOS窓上のASCIIアートが私は好きだ。なのでPythonでGETしよう。

Posted at

1.はじめに

どうも、趣味でデータ分析している猫背なエンジニアです。

皆さんはDOS窓上に展開される「アート」は好きですか?
私は好きです。特に影付きのアート文字なんか好きです。

ふと思ったこととして、データの飾りとしてPythonで使えたらいいのに...を今回実現しましたので記録していきます。

2. 「DOS窓でASCIIアートして、スクショする」をPythonで

■ 概要
着想として、いま進めている制作物のN-MAGIのUIを一新する計画をしています。MAGIと言ったら、機械仕掛けのUIですよね。
それを表現するためにDOS窓を使いたかったのです。
ライブラリとしてはshutil,pyautogui,pygetwindowsのライブラリをメインで使っています。
ちなみにshutilをここで使うとは思っていませんでした(笑)

■ コーディング
アルゴリズムはいたって簡単で、Pythonコード上で作ったASCIIアートをDOS窓で展開して、スクリーンショットして保存するというものになっています。
今回はN-MAGIを動かすためのUIというのを前提にしているので、N-MAGIという文字が強調されていますが、自由に変えてください👍
また、入力値はstrを6つ入力するという想定で作っています。

import os
import shutil
import pyautogui
import time
import pygetwindow as gw

# コンソール書式設定
def print_right_aligned(text_lines):
    columns = shutil.get_terminal_size().columns
    for line in text_lines:
        print(line.ljust(columns))

def right_pad(text, width):
    return text.rjust(width)

# 出力メイン
def display_numbers(numbers3_casper, numbers3_balthasar, numbers3_melchior, numbers4_casper, numbers4_balthasar, numbers4_melchior):
    os.system("")  # ANSIカラー対応
    orange = "\033[38;2;255;153;51m"
    reset = "\033[0m"

    logo = [
        "║ ███╗    ██╗     ███╗   ███╗ █████╗  ██████╗ ███████╗ ║",
        "║ ██╔██╗  ██║     ████╗ ████║██╔══██╗██╔════╝ ██╔════╝ ║",
        "║ ██║╚██╗ ██║████╗██╔████╔██║███████║██║ ████╗█████╗   ║",
        "║ ██║ ╚██╗██║╚═══╝██║╚██╔╝██║██╔══██║██║  ╚██║██╔══╝   ║",
        "║ ██║  ╚████║     ██║ ╚═╝ ██║██║  ██║╚██████╔╝███████╗ ║",
        "║ ╚═╝   ╚═══╝     ╚═╝     ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚══════╝ ║"
    ]
    
    # 各列の文字列を作成
    col_width = 24  # 各列の幅を指定

    os.system("cls")
    # logo 表示
    print()
    print_right_aligned([orange + "" + "" * 54 + "" + reset])
    print_right_aligned([orange + line + reset for line in logo])
    print_right_aligned([orange + "" + "" * 54 + "" + reset])
    print()

    numbers3_lines = [
        orange + right_pad(f"╔═NUMBERS3═══════╗", col_width) + reset,
        orange + right_pad(f"║ CASPER     {numbers3_casper}", col_width) + reset,
        orange + right_pad(f"║ BALTHASAR  {numbers3_balthasar}", col_width) + reset,
        orange + right_pad(f"║ MELCHIOR   {numbers3_melchior}", col_width) + reset,
        orange + right_pad(f"╚════════════════╝", col_width) + reset
    ]

    numbers4_lines = [
        orange + right_pad(f"╔═NUMBERS4════════╗", col_width) + reset,
        orange + right_pad(f"║ CASPER     {numbers4_casper}", col_width) + reset,
        orange + right_pad(f"║ BALTHASAR  {numbers4_balthasar}", col_width) + reset,
        orange + right_pad(f"║ MELCHIOR   {numbers4_melchior}", col_width) + reset,
        orange + right_pad(f"╚═════════════════╝", col_width) + reset
    ]

    # 横に並べて出力
    for line3, line4 in zip(numbers3_lines, numbers4_lines):
        print(line3 + "  " + line4)
    print()
    
    # ここからスクリーンショット処理
    time.sleep(15)  # 表示を安定させる
    screenshot_filename=r"D:\01.開発\06Numbers3\msdos_screenshot.png"
    # DOSウィンドウを取得(タイトルに合わせる)
    windows = gw.getWindowsWithTitle("C:\WINDOWS\system32\cmd")
    if windows:
        win = windows[0]
        x, y, width, height = win.left, win.top, win.width, win.height

        cut_width = 10
        x_new = x + cut_width
        width_new = width - cut_width
        half_width = width_new // 2 - 40
        
        cut_top = 50
        y_new = y + cut_top
        height_new = height - cut_top
        half_height = height_new // 2 + 10
        
        screenshot = pyautogui.screenshot(region=(x_new, y_new, half_width, half_height))
        screenshot.save(screenshot_filename)
        print(f"DOSウィンドウのみスクリーンショットを保存しました: {screenshot_filename}")
    else:
        print("DOSウィンドウを開いても取得できませんでした。")

■ 出力結果
image.png

いい感じになりましたね。実にMAGIっぽいですね...!

4.おわりに

珍しくいい感じのUIをデザインすることができました。
来週はこれをN-MAGIに組み込んでポストするまでを記事にしたいと思います。

5. N-MAGI関連記事(MAGI伝記)

1. 【作戦概要】:全体構想と設計方針
  - 全体構想と設計方針
2. 【データ収集編】:過去の当選番号の収集
  - 過去の当選番号をどう集める?
3. 【モデル構築編】:3種のモデル(CASPER / BALTHASAR / MELCHIOR)実装と精度比較
  - CASPER / BALTHASAR / MELCHIORの実装と精度比較
4. 【投稿編】:X APIを用いた投稿
  - X APIを用いて予測を自動投稿
5. 【UI&演出編】:MAGI風UIをTkinterで再現して、予測を視覚化!
  - TkinterでMAGI風UIを構築

14
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?