A student writing a program as a hobby. I understand the for statement.
macOS Catalina 10.15.7 python 3.8.6
I want to make flashcards with goodnotes5 and study with Anki. The experimental features of flashcards in goodnotes 5 are not good for now.
Do 3 and 4 with python.
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 ">
I don't understand the naming convention and English grammar. Change path / to / and below according to the environment.
toAnki.py
import os
from PIL import Image
#Declared to create csv for Anki
Anki_csv = []
#Anki media file location
anki_media_path = '/path/to/collection.media'
#Specify the folder containing the image file
print('tell me target dir under homedir')
target_dir_path = '/path/to/userhome'+input()
#Specify the name of the image file
print('tell me image file name')
image_file_name = input()
#Specify the name of the image file
print('tell me image index start')
index = int(input())
#Get all image files in the specified folder
target_list = os.listdir(target_dir_path)
target_list.remove('.DS_Store')
#Image processing and saving for each image file
for target in target_list:
#Store image file in Image object
img = Image.open(target_dir_path + '/' + target)
#Get the width and height of the image
width = img.size[0]
height = img.size[1]
#Split and save images above and below
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')
#For 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')
#Report one by one when finished
print('done ' + str(index))
#Count the index of the image file to save
index += 1
#csv creation
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 is difficult. No mention of the contents of the code. Any questions or suggestions.
Recommended Posts