[PYTHON] I wrote a script to help goodnotes5 and Anki work together

Self-introduction

A student writing a program as a hobby. I understand the for statement.

environment

macOS Catalina 10.15.7 python 3.8.6

Motivation

I want to make flashcards with goodnotes5 and study with Anki. The experimental features of flashcards in goodnotes 5 are not good for now.

flow

Do 3 and 4 with python.

  1. Export flashcards (image below) from goodnotes5 in image format![Grammar Express Memorization-78.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/ 0/804364/d5b63208-0404-638a-b20f-e0b690ce0801.jpeg)
  2. Store the exported image (full) in any directory
  3. Divide the exported image into two and save each in Anki's collection.media
  4. Create a csv file that can display the split image in Anki (example.csv)
  5. Import csv with Anki for PC

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 ">

Actual code

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')

at the end

markdown is difficult. No mention of the contents of the code. Any questions or suggestions.

Recommended Posts

I wrote a script to help goodnotes5 and Anki work together
I wrote a script to upload a WordPress plugin
I wrote a script to get a popular site in Japan
I wrote a script to combine the divided ts files
I wrote a script to get you started with AtCoder fast!
I just wrote a script to build Android on another machine
I wrote a function to load a Git extension script in Python
Scraping and tabelog ~ I want to find a good restaurant! ~ (Work)
I wrote a script to extract a web page link in Python
I wrote a class in Python3 and Java
I made a script to put a snippet in README.md
I want to work with a robot in python.
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
I was surprised to receive a nice review when I wrote Python to CheckIO and its explanation
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
I wrote a program quickly to study DI with Python ①
I wrote a script that splits the image in two
I made a script to say hello at my Koshien
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I want to record the execution time and keep a log.
I tried to make a periodical process with Selenium and Python
I tried to create Bulls and Cows with a shell program
I want to create a pipfile and reflect it in docker
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a POST script to create an issue on Github and register it in the Project
I tried to take the difference of Config before and after work with pyATS / Genie self-made script
I wrote a book that allows you to learn machine learning implementations and algorithms in a well-balanced manner.
I made a CUI-based translation script
I wrote a code to convert quaternions to z-y-x Euler angles in Python
I made a network to convert black and white images to color images (pix2pix)
I made a script in python to convert .md files to Scrapbox format
I made a program to input what I ate and display calories and sugar
I tried to create a sample to access Salesforce using Python and Bottle
I want to make a web application using React and Python flask
Story of making a virtual planetarium [Until a beginner makes a model with a script and manages to put it together]
Suddenly I needed to work on a project using Python and Pyramid, so a note of how I'm studying
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.