-
Notifications
You must be signed in to change notification settings - Fork 1
mapファイルからpngを生成する機能を作成しました #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ssuzzukki
wants to merge
7
commits into
develop
Choose a base branch
from
feature/#6_make_image_generator
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
006a410
mapファイルからpngを生成する機能を作成しました
ssuzzukki 22df12c
make checkの適用
ssuzzukki 48e2db7
動作に影響のない細かな修正
ssuzzukki 2bcabbb
if文の条件文の修正、with構文の使用、.readlinesの使用
ssuzzukki 7e277f1
生成元ディレクトリをパスで指定できるよう変更、内部処理もパスを使いこのファイル本体の移動をしないように変更
ssuzzukki c253710
アイコン用画像の名前の変更
ssuzzukki e5c7127
各マスの状態を判定するのに列挙型を使用 列挙型の活用方法はこれであってるんでしょうか?
ssuzzukki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import os | ||
| import re | ||
| import sys | ||
| from enum import Enum | ||
|
|
||
| from PIL import Image | ||
|
|
||
|
|
||
| # mapファイルのテキストデータを2次元配列に変換するメソッド | ||
| def changeList(map_data: list[str]) -> list[list[str]]: | ||
| map_list = [] | ||
| # 各行を一マスごとに分割して配列に格納 | ||
| for i in range(len(map_data)): | ||
| if map_data[i] == "": | ||
| break | ||
| elif map_data[i][0] == "D": | ||
| map_list.append(re.split(",|D:|\n", map_data[i])[1:-1]) | ||
| # print(map_data[i]) | ||
| elif map_data[i][0] == "H": | ||
| hot = re.split(",|\n", map_data[i].replace("H:", "")) | ||
| elif map_data[i][0] == "C": | ||
| cool = re.split(",|\n", map_data[i].replace("C:", "")) | ||
|
|
||
| map_list[int(cool[1])][int(cool[0])] = "C" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "H"や”C"だとなんのことか分かりづらいので |
||
| map_list[int(hot[1])][int(hot[0])] = "H" | ||
|
|
||
| return map_list | ||
|
|
||
|
|
||
| # 一つのmapファイルをpngに変換するメソッド | ||
| def drawMapImage( | ||
| file_name: str, | ||
| dir_path: str, | ||
| ) -> None: | ||
|
|
||
| # 各マスの状態を定義 | ||
| class Status(Enum): | ||
| FLOOR = "0" | ||
| WALL = "2" | ||
| ITEM = "3" | ||
| HOT = "H" | ||
| COOL = "C" | ||
|
|
||
| # mapファイルを開いてmap_dataにテキストを格納、行ごとに配列にする | ||
| with open(dir_path + "/" + file_name, "r") as file: | ||
| map_data = file.readlines() | ||
|
|
||
| map_list = changeList(map_data) | ||
|
|
||
| # アイコン用の画像を開く | ||
| floor = Image.open("./icons/floor.png") | ||
| wall = Image.open("./icons/wall.png") | ||
| item = Image.open("./icons/item.png") | ||
| hot = Image.open("./icons/hot.png") | ||
| cool = Image.open("./icons/cool.png") | ||
|
|
||
| icon_size = floor.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 Status(map_list[i][j]) == Status.FLOOR: | ||
| img.paste(floor, (j * icon_size, i * icon_size)) | ||
| elif Status(map_list[i][j]) == Status.WALL: | ||
| # 壁 | ||
| img.paste(wall, (j * icon_size, i * icon_size)) | ||
| elif Status(map_list[i][j]) == Status.ITEM: | ||
| # アイテム | ||
| img.paste(item, (j * icon_size, i * icon_size)) | ||
| elif Status(map_list[i][j]) == Status.COOL: | ||
| # cool | ||
| img.paste(cool, (j * icon_size, i * icon_size)) | ||
| elif Status(map_list[i][j]) == Status.HOT: | ||
| # hot | ||
| img.paste(hot, (j * icon_size, i * icon_size)) | ||
| else: | ||
| # それ以外(床) | ||
| img.paste(floor, (j * icon_size, i * icon_size)) | ||
|
|
||
| # 画像を保存 | ||
| img.save(dir_path + "/map_images/" + file_name.split(".")[0] + ".png") | ||
| # img.show("test.png") | ||
|
|
||
|
|
||
| # 全てのmapファイルをpngに変換するメソッド | ||
| def main() -> None: | ||
|
|
||
| dir_path = sys.argv[1] | ||
| file_list = os.listdir(dir_path) | ||
|
|
||
| # 画像を格納するディレクトリを作成 | ||
| os.mkdir(dir_path + "/map_images") | ||
|
|
||
| for i in range(len(file_list)): | ||
| drawMapImage(file_list[i], dir_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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]