goodnotes5とAnkiの連携を助けるスクリプトをを書いた
自己紹介
趣味でプログラムを書いている学生。for文がわかる。
環境
macOS Catalina 10.15.7
python 3.8.6
動機
goodnotes5でフラッシュカードを作ってAnkiで学習したい。
goodnotes5にあるフラッシュカードの試験的な機能は今のところイマイチ。
流れ
3,4番をpythonでやる。
1. goodnotes5からフラッシュカード(下の画像)をイメージ形式で書き出す
2. 書き出したイメージ(いっぱい)を任意のディレクトリに格納しておく
3. 書き出したイメージを2分割してそれぞれをAnkiのcollection.mediaに保存
4. 2分割したイメージをAnkiで表示できるようなcsvファイルを作成する(example.csv)
5. PC版Ankiでcsvをimportする
example.csv
<img src="example_qst0.png"><img src="example_ans0.png">
<img src="example_qst1.png"><img src="example_ans1.png">
<img src="example_qst2.png"><img src="example_ans2.png">
実際のコード
命名規則、英文法わかりません。
path/to/以下は環境に合わせて変更する。
toAnki.py
import os
from PIL import Image
# Anki用csv作成のため宣言
Anki_csv = []
# Ankiのメディアファイルの場所
anki_media_path = '/path/to/collection.media'
# 画像ファイルの入ったフォルダを指定
print('tell me target dir under homedir')
target_dir_path = '/path/to/userhome'+input()
# 画像ファイルの名前を指定
print('tell me image file name')
image_file_name = input()
# 画像ファイルの名前を指定
print('tell me image index start')
index = int(input())
# 指定したフォルダ内の画像ファイルを全て取得
target_list = os.listdir(target_dir_path)
# MacOSのおせっかい対策
if '. DS_Store' in target_list:
target_list.remove('.DS_Store')
# 各画像ファイルごとに画像処理・保存
for target in target_list:
# 画像ファイルをImageオブジェクトに格納
img = Image.open(target_dir_path + '/' + target)
# 画像の幅と高さを取得
width = img.size[0]
height = img.size[1]
# 画像を上と下に分割・保存
img_qst = img.crop((0, 0, width, height/2))
img_ans = img.crop((0, height/2, width, height))
img_qst.save(anki_media_path + '/' + image_file_name + '_qst' + str(index)+ '.png')
img_ans.save(anki_media_path + '/' + image_file_name + '_ans' + str(index)+ '.png')
# csv用
qst_tag = '<img src="' + image_file_name + '_qst' + str(index)+ '.png' + '">'
ans_tag = '<img src="' + image_file_name + '_ans' + str(index)+ '.png' + '">'
Anki_csv.append(qst_tag + ',' + ans_tag + '\n')
# 終わったら逐一報告
print('done ' + str(index))
# 保存する画像ファイルのインデックスをカウント
index += 1
# csv作成
with open('/path/to/Desktop/toAnki.csv',mode='w') as f:
for j in Anki_csv:
f.write(j)
print('All work was done')
さいごに
markdown難しい。コードの中身について言及していない。
質問・ご指摘等よろしく。
Author And Source
この問題について(goodnotes5とAnkiの連携を助けるスクリプトをを書いた), 我々は、より多くの情報をここで見つけました https://qiita.com/hmd114514/items/e8ec7ab9577dfac99bd8著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .