Skip to content
95 changes: 95 additions & 0 deletions src/ImageGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import re
import sys
from typing import List
Comment thread
polyacetal marked this conversation as resolved.
Outdated

from PIL import Image

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Cannot find implementation or library stub for module named "PIL" [import-not-found]



# mapファイルのテキストデータを2次元配列に変換するメソッド
def changEArray(map_data: List[str]) -> List[List[str]]:
Comment thread
polyacetal marked this conversation as resolved.
Outdated
map_list = []
# 各行を一マスごとに分割して配列に格納
for i in range(len(map_data)):
if map_data[i] != "" and map_data[i][0] == "D":
Comment thread
ssuzzukki marked this conversation as resolved.
Outdated
map_list.append(re.split(",|\n", map_data[i].replace("D:", "")))
elif map_data[i] != "" and map_data[i][0] == "H":
hot = re.split(",|\n", map_data[i].replace("H:", ""))
elif map_data[i] != "" and map_data[i][0] == "C":
cool = re.split(",|\n", map_data[i].replace("C:", ""))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map_data[i] != ""は共通の条件だから先に確認してbreakできるね!


map_list[int(cool[1])][int(cool[0])] = "C"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"H"や”C"だとなんのことか分かりづらいので
列挙型(Enum)を定義して管理したい!

map_list[int(hot[1])][int(hot[0])] = "H"

return map_list


# 一つのmapファイルをpngに変換するメソッド
def draWPng(file_name: str) -> None:
Comment thread
polyacetal marked this conversation as resolved.
Outdated
# mapファイルを開いてmap_dataにテキストを格納
# print(file_name)
file = open("../" + file_name, "r")
map_data = file.read().split("\n")
Comment thread
polyacetal marked this conversation as resolved.
Outdated
file.close
Comment thread
polyacetal marked this conversation as resolved.
Outdated

map_list = changEArray(map_data)

# アイコン用の画像を開く
yuka = Image.open("../../icons/yuka.png")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パスは相対表記ではなく, __file__os.getcwd()を用いた方がOSなどの影響を受けづらいです

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item,hot,coolは英語表記なのでyuka,kabeも合わせたい!

kabe = Image.open("../../icons/kabe.png")
item = Image.open("../../icons/item.png")
hot = Image.open("../../icons/hot.png")
cool = Image.open("../../icons/cool.png")

icon_size = yuka.width

# 画像の下地を描く
size = (len(map_list[0]) * icon_size, len(map_list) * icon_size)
img = Image.new("RGB", size, (255, 255, 255))

# 各マスのデータに応じて画像を敷き詰める
for i in range(len(map_list)):
for j in range(len(map_list[i])):
if map_list[i][j] == "2":
# 壁
img.paste(kabe, (j * icon_size, i * icon_size))
elif map_list[i][j] == "3":
# アイテム
img.paste(item, (j * icon_size, i * icon_size))
elif map_list[i][j] == "C":
# cool
img.paste(cool, (j * icon_size, i * icon_size))
elif map_list[i][j] == "H":
# hot
img.paste(hot, (j * icon_size, i * icon_size))
else:
# それ以外(床)
img.paste(yuka, (j * icon_size, i * icon_size))

# 画像を保存
img.save(file_name.split(".")[0] + ".png")
# img.show("test.png")


# 全てのmapファイルをpngに変換するメソッド
def draWAll() -> None:
Comment thread
polyacetal marked this conversation as resolved.
Outdated

dir_name = sys.argv[1]
file_list = os.listdir(dir_name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実行時の引数をParseして処理する関数に切り出して
任意のパス型などをこの先の関数で渡してあげれると
文字列の連結などを考えなくてもよくなるのでトライして欲しい!


# mapファイルが入ったディレクトリに移動
os.chdir(dir_name)

# 画像を格納するディレクトリを作成し移動
os.mkdir("map_images")
os.chdir("map_images")

for i in range(len(file_list)):
draWPng(file_list[i])

# もとのディレクトリに戻る
os.chdir("../../")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want: ディレクトリ移動ではなく, 相対パスで指定してマップファイルの読み取りと画像ファイルの書き出しをしてほしい.

print("完了しました")
Comment thread
polyacetal marked this conversation as resolved.
Outdated


draWAll()
Comment thread
polyacetal marked this conversation as resolved.
Outdated
Binary file added src/icons/cool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/hot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/item.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/kabe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/yuka.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.