-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMimesis0.1.py
143 lines (119 loc) · 5.74 KB
/
Mimesis0.1.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from google_images_download import google_images_download # Importing google_images_download module
from rake_nltk import Rake # Rapid Automatic Keyword Extraction algorithm
# For writing on the images.
from translate import Translator
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import glob
import os
import shutil
# Delete downloads and final_save folders to work with new ones
if os.path.exists("downloads"):
shutil.rmtree("downloads")
if os.path.exists("final_save"):
shutil.rmtree("final_save")
r = Rake(min_length=1, max_length=1) # Uses stopwords for english from NLTK, and all punctuation characters.
text = input("Hello!\nI will find images for your keywords and translate your sentence(s) for you.\nTo look up an image for every word, seperate with periods.\n\nWrite your sentence(s) below please:\n")
lang = input("\nWhat language do you want to translate the sentence(s) to?\n")
translator= Translator(from_lang="english",to_lang=lang)
text_sentences=list(text.split("."))
usr=r._generate_phrases(text_sentences)
print(usr)
response = google_images_download.googleimagesdownload()
search_queries = (usr)
# download the images using the key words
def downloadimages(query,picture_counter):
# keywords is the search query
# format is the image file format
# limit is the number of images to be downloaded
# print urls is to print the image file url
# size is the image size which can
# be specified manually ("large, medium, icon")
# aspect ratio denotes the height width ratio
# of images to download. ("tall, square, wide, panoramic")
arguments = {"keywords": "Picture of "+query,
"format": "jpg",
"limit":1,
"silent_mode":True,
"usage_rights":"labeled-for-reuse",
"print_urls":False,
"size": "medium"}
try:
response.download(arguments,picture_counter)
# Handling File NotFound Error
except FileNotFoundError:
arguments = {"keywords": "Picture of "+query,
"format": "jpg",
"limit":10,
"silent_mode":True,
"usage_rights":"labeled-for-reuse",
"print_urls":False,
"size": "medium"}
# Providing arguments for the searched query
try:
# Downloading the photos based
# on the given arguments
response.download(arguments)
except:
pass
# Writes on images the given text
def image_writer(d,i,text_given):
image_dir = (glob.glob(d+"/*"))
image_dir = image_dir[0]
img = Image.open(image_dir) # opening the image using the folder which is named using keyword
# will have to name pictures consistently
width, height = img.size # get size of image
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", int(((width+height)/2)/30)) # set font
message=text_given # set the message to write. Eventually move it to be the sentence in which the keyword appears
w, h = font.getsize(message) # size of text we wrote
draw.rectangle(((width-w)/2, (9*(height-h))/10, ((width-w)/2) + w, ((9*(height-h))/10) + h), fill='black') #black background for text
draw.text(((width-w)/2, (9*(height-h))/10), message,font=font) # set the text to be in the center bottom of the screen
print(str(translator.translate(text_given)))
trans_message=str(translator.translate(text_given))
w_trans, h_trans = font.getsize(trans_message) # size of text we wrote
draw.rectangle(((width-w_trans)/2, (1*(height-h_trans))/10, ((width-w_trans)/2) + w_trans, ((1*(height-h_trans))/10 ) + h_trans), fill='black') #black background for text
draw.text(((width-w_trans)/2, (1*(height-h_trans))/10), trans_message,font=font) # set the text to be in the center bottom of the screen
img.save('final_save/'+str(i)+'.jpg') # save the new image. Eventually save it in a folder along with all the other images
# Navigate the folders and drive image_writer
def image_maker(text_given):
try:
if not os.path.exists("final_save"):
os.makedirs("final_save")
except OSError:
print ('Error: Creating directory final_save')
directory = (glob.glob("downloads/*/"))
print(directory)
n=0
for d in directory:
image_writer(d,n,text_given[n])
n+=1
# divides sentences using keywords
def div_sen_key(sen_list,search_queries):
div_sen=[] # list of divided sentences to put on images
for sen in sen_list:
print("Sentence being observed:",sen)
word_start=0 # index of the word to start with
word_counter=0 # count words
for word in list(sen.split(" ")):
clean_word=word.replace(',','')
clean_word=clean_word.replace('!','')
clean_word=clean_word.replace('.','')
clean_word=clean_word.replace('?','')
clean_word=clean_word.replace(':','')
clean_word=clean_word.replace(';','')
clean_word=clean_word.lower()
word_counter+=len(word)+1
if clean_word in search_queries:
div_sen.append(sen[word_start:word_counter])
word_start=word_counter
return div_sen
# Driver Code
count=0
for query in search_queries:
print(query)
downloadimages(query,count)
count+=1
print("Sentences after division:",div_sen_key(text_sentences,search_queries))
image_maker(div_sen_key(text_sentences,search_queries))