forked from Gab0/fodmapview
/
viewer.py
93 lines (68 loc) · 2.66 KB
/
viewer.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
#!/bin/python
import json
import random
import os
from google_images_download import google_images_download
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QPlainTextEdit, QHBoxLayout, QLabel
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt
class Viewer(QApplication):
def __init__(self):
QApplication.__init__(self, [])
window = QWidget()
layout = QVBoxLayout()
Buttons = QHBoxLayout()
self.btn_Random = QPushButton("RANDDM")
self.btn_Random.clicked.connect(lambda: self.changeView(
random.choice(range(len(self.database)))))
nav_r = QPushButton('<-')
nav_r.clicked.connect(lambda: self.cycleDatabaseIndex(-1))
Buttons.addWidget(nav_r)
Buttons.addWidget(self.btn_Random)
nav_l = QPushButton('->')
nav_l.clicked.connect(lambda: self.cycleDatabaseIndex(1))
Buttons.addWidget(nav_l)
self.Image = QLabel()
self.Image.setFixedSize(800, 600)
self.textBox = QPlainTextEdit()
layout.addWidget(self.Image)
layout.addWidget(self.textBox)
layout.addLayout(Buttons)
window.setLayout(layout)
window.show()
self.imagesFolder = "Images"
self.database = json.load(open("fodmap_list/fodmap_repo.json"))
self.exec_()
def cycleDatabaseIndex(self, Value):
# Checando se a variavel data existe.
try:
print(self.data['id'])
targetIdx = int(self.data["id"]) - 1 + Value
self.changeView(targetIdx)
except:
self.changeView(random.randint(0, 99))
def changeView(self, viewIndex):
self.data = self.database[viewIndex]
self.showImage(self.data['name'].replace(",", "").replace("/", ""))
self.textBox.setPlainText(json.dumps(self.data, indent=4))
def showImage(self, imageName):
folderPath = os.path.join(self.imagesFolder, imageName)
if not os.path.isdir(folderPath):
filename = self.downloadImage(imageName)[imageName][0]
else:
filename = os.path.join(folderPath, os.listdir(folderPath)[0])
pixmap = QPixmap(filename)
self.Image.setPixmap(pixmap.scaledToHeight(
600, Qt.TransformationMode(Qt.FastTransformation)))
self.Image.resize(200, 100)
def downloadImage(self, keyword):
image = google_images_download.googleimagesdownload()
arguments = {
"keywords": keyword,
"limit": 1,
"output_directory": self.imagesFolder
}
paths = image.download(arguments)
print(paths)
return paths
a = Viewer()