forked from Amritpal-001/pdf_to_anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_to_anki.py
97 lines (69 loc) · 2.54 KB
/
pdf_to_anki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pdf2image
from pdf2image import convert_from_path as cfp
import ntpath
import cv2
import os
import time
from glob import glob
try:
os.mkdir("images")
except:
pass
try:
os.mkdir("images_final")
except:
pass
def convert_to_image(path_to_pdf , deck_name):
print("Devolped by ")
print(" @@@ @@@. @@@. @@@@@@@@@@& @@ @@@@@@@@@@@@@ ")
print(" @@@@% @@@@ @@@@. @@ @@# @@ @@ ")
print(" @@ @@, @@ @@ %@ @@. @@ @@/ @@ @@ ")
print(" @@ @@ @@ @@ @( @@. @@@@@@@@@@/ @@ @@ ")
print(" @@@@@@@@@@ @@ @@ @@ @@. @@ @@ @@ @@ ")
print(" @@ @@ @@ @@@@ @@. @@ @@ @@ @@ ")
print(" @@ @@ @@ @@@@ @@. @@ @@ @@ @@ ")
print("[So that he can be spare himself from AKSHDEEP's tourture!] ")
print()
print()
print()
print("Starting to load pdf")
images = cfp(path_to_pdf)
print("pdf loaded to memory")
for i in range(len(images)):
images[i].save("./images/" +deck_name + str(i) + str(time.time()) + ".jpg" , "JPEG" )
if i % 10 == 0:
print("saved" , i)
convert_to_image( "sample.pdf" , "deck_name" )
file_list = glob('./images/*.jpg')
#print(file_list)
rand_int = time.time()
rand_int = round(rand_int)
complete_list = []
direct_copy = True
for filename in file_list:
image = cv2.imread(filename)
def crop_doublet_anki_notes(image , x , y):
a = int(x*(image.shape[0])/(x+y))
img1 = image[ 0:a , 0:image.shape[0]]
img2 = image[ a:image.shape[0] , 0:image.shape[0] ]
return(img1 , img2 )
img1 , img2 = crop_doublet_anki_notes(image , 14,13.5)
#print(img1)
#print(type(img1))
filename = ntpath.basename(filename)
a1 = filename[0:-4] + str(rand_int) + "_01" + ".jpg"
a2 = filename[0:-4] + str(rand_int) + "_02" + ".jpg"
name1 = "<img src='" + a1 + "'>"
name2 = "<img src='" + a2 + "'>"
cv2.imwrite( "./images_final/" + a1 , img1)
cv2.imwrite("./images_final/" + a2 , img2)
#print("./images_final/" + a1 )
complete_list.append([name1 , name2])
import pandas as pd
df = pd.DataFrame(complete_list)
#print(df)
df.to_csv("output.csv", index = None,header=False)
#Use this Output.csv file to load the anki deck to Anki software.
#Before loading the CSV file, copy the images generated in images_final folder to following path -
# "./home/$$$$$YOUR USER NAME$$$$$$$$/.local/share/Anki2/User 1/collection.media/" # in Ubuntu
# e.g. "./home/amrit/.local/share/Anki2/User 1/collection.media/"