-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
141 lines (114 loc) · 3.69 KB
/
test.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
import os, random, csv, random
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
from pygame.locals import *
from datetime import datetime
def detect_labels(path):
"""Detects labels in the file."""
from google.cloud import vision
import io
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
return labels
def is_dog(labels):
for label in labels:
if label.description == "Dog":
return True
return False
def is_cat(labels):
for label in labels:
if label.description == "Cat":
return True
return False
def get_dog_pun(data):
num_dog_puns = 108
num = random.randint(0,num_dog_puns-1)
pun = data[num][0]
nc = 0
for char in pun:
if char == ".":
break
nc+=1
pun = pun[nc+2:]
return pun
def get_cat_pun(data):
num_cat_puns = 66 # q odd a even up to 65,66
num = random.randint(0,num_cat_puns-1)
if num%2 != 0:
num -= 1
if num < 0:
num = 0
pun = data[num][1]
nc = 0
for char in pun:
if char == ".":
break
nc+=1
pun = pun[nc+2:]
return [pun, data[num+1][1]]
def display_image(img_path, text):
font_path = "./fonts/Pacifico.ttf"
# font_path = None
pygame.init()
image = pygame.image.load(img_path)
screen = pygame.display.set_mode(image.get_rect().size, 0, 32)
image = image.convert()
screen.blit(image, (0,0))
pygame.display.set_caption('Scratching Post')
ofs = 1
if len(text) != 2:
font = pygame.font.Font(font_path, 30)
text1 = font.render(text, 1, (10, 10, 10))
textw = font.render(text, 1, (250, 250, 250))
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx
textposw = (textpos[0]+ofs, textpos[1]+ofs)
screen.blit(textw, textposw)
screen.blit(text1, textpos)
else:
font = pygame.font.Font(font_path, 30)
text1 = font.render(text[0], 1, (10, 10, 10))
text2 = font.render(text[1], 1, (10, 10, 10))
text1w = font.render(text[0], 1, (250, 250, 250))
text2w = font.render(text[1], 1, (250, 250, 250))
textpos1 = text1.get_rect()
textpos2 = text2.get_rect()
textpos1.centerx = screen.get_rect().centerx
textpos2.midbottom = screen.get_rect().midbottom
screen.blit(text1, textpos1)
screen.blit(text2, textpos2)
textposw1 = (textpos1[0]+ofs, textpos1[1]+ofs)
textposw2 = (textpos2[0]+ofs, textpos2[1]+ofs)
screen.blit(text1w, textposw1)
screen.blit(text2w, textposw2)
pygame.display.flip()
print("HERE")
# pygame.image.save(window,"screenshot.png")
filename = (str(datetime.now())+".jpeg")
pygame.image.save(self.screen, (filename))
os.rename(filename, "generated/" + filename)
# Event loop
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
def main():
path = random.choice(os.listdir("./images/images"))
img_path = "./images/images/" + path
labels = detect_labels(img_path)
# labels = detect_labels("cat.jpg") # COMMENT TO FIX RAND
# for label in labels:
# print(label.description)
pun_path = "minnehack_scraped_data.csv"
csvDataFile = open(pun_path)
data=list(csv.reader(csvDataFile))
if is_cat(labels):
pun = get_cat_pun(data)
else:
pun = get_dog_pun(data)
display_image(img_path, pun)
if __name__ == '__main__': main()