Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active April 10, 2024 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdkfz181tiger/aab7b5683ecaafa1c040f5e7b30a263c to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/aab7b5683ecaafa1c040f5e7b30a263c to your computer and use it in GitHub Desktop.
基数変換で遊ぼう_その3_ASCII変換問題ジェネレーター
# coding: utf-8
"""
ASCII変換問題ジェネレーター
"""
import os, math, random
from PIL import Image, ImageFont, ImageDraw
# D2A or A2D
flg_d2a = False
# Directory
dir_name = "d2a" if flg_d2a else "a2d"
# Title
text_title = "ASCII \\(x_x;)/ HELL"
if flg_d2a == True:
text_explain = "次にある10進数の値を、ASCII文字に変換しなさい"
text_example = "※8bit2進数に変換->コンピュータ概論:p46参照\n\n解答例: [72, 69, 76, 76, 79] = HELLO"
else:
text_explain = "次にあるASCII文字を、10進数に変換しなさい"
text_example = "※8bit2進数に変換->コンピュータ概論:p46参照\n\n解答例: HELLO = [72, 69, 76, 76, 79]"
# Font
font_path_w4 = "/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc";
font_path_w6 = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc";
font_color = (0, 0, 0)
# ASCII符号表(コンピュータ概論:p46)
ASCII = [[], [],
["SP", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/"],
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?"],
["@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"],
["P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "¥", "]", "^", "_"],
["`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"],
["p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "DEL"]
]
# 問題リストと解答リスト
str_words = ["NE", "USI", "TORA", "U", "TAMA", "MI", "UMA", "HITUJI", "NEKO", "TORI", "INU", "I"]
str_ascii = []
base_x = 250
base_y = 800
pad_x = 980
pad_y = 210
rows = 10
cols = 2
def main():
# Words -> ASCII
for i in range(len(str_words)):
str_ascii.append(str(str2ascii(str_words[i])))
# PDF
for i in range(2):
# Type
type_flg = i % 2
type_name = "mondai" if type_flg else "kaito"
# Images
images = []
image = Image.new("RGB", (2150, 3035), (255, 255, 255))
# Draw
draw = ImageDraw.Draw(image)
# Title
font = ImageFont.truetype(font_path_w6, 80)
draw.text((base_x, 200), text_title, font=font, fill=font_color)
# Name
font = ImageFont.truetype(font_path_w6, 40)
draw.text((image.size[0]/2 + 500, 250), "氏名:", font=font, fill=font_color)
# Explain
font = ImageFont.truetype(font_path_w6, 50)
draw.text((260, 420), text_explain, font=font, fill=font_color)
# Example
font = ImageFont.truetype(font_path_w4, 40)
draw.text((260, 540), text_example, font=font, fill=font_color)
for r in range(rows):
for c in range(cols):
n = c + r * cols
if(len(str_words) <= n): break
if flg_d2a == True:
text = "(" + str(n+1) + ")" + "\n\n " + str_ascii[n] + " = "
if type_flg == 0: text += str_words[n]
else:
text = "(" + str(n+1) + ")" + "\n\n " + str_words[n] + " = "
if type_flg == 0: text += str_ascii[n]
pos_x = base_x + c * pad_x
pos_y = base_y + r * pad_y
pos = (pos_x, pos_y)
font = ImageFont.truetype(font_path_w6, 40)
draw.text(pos, text, font=font, fill=font_color)
# Directory
if(not os.path.isdir(dir_name)): os.mkdir(dir_name)
images.append(image)
# Save
image_out = "./" + dir_name + "/" + type_name + ".pdf"
images[0].save(image_out, "PDF",
quality=100, save_all=True, append_images=images[1:], optimize=True)
def str2ascii(s):
result = []
for c in s:
i, j = char2ascii(c)
result.append(b2d(d2b(i) + d2b(j)))
return result
def char2ascii(c):
for i in range(len(ASCII)):
for j in range(len(ASCII[i])):
if c == ASCII[i][j]: return i, j
def bin2dec(bin):
result = []
for b in bin: result.append(b2d(b))
return result
def dec2str(dec):
result = []
for d in dec: result.append(chr(d))
return result
def d2b(d): return bin(d)[2:].zfill(4)
def d2h(d): return hex(d)[2:].zfill(1)
def b2d(b): return int(b, 2)
def h2b(h): return int(h, 16)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment