You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.5 KiB
72 lines
2.5 KiB
import sys
|
|
sys.path.append("wordCloud/")
|
|
|
|
import Draw
|
|
from PySide2.QtWidgets import QApplication, QFileDialog, QMainWindow
|
|
from PySide2.QtUiTools import QUiLoader
|
|
from PySide2.QtGui import QPixmap
|
|
from PySide2 import QtCore
|
|
|
|
import shutil
|
|
|
|
|
|
class Stats:
|
|
fileDirectory = ''
|
|
text_path = 'temp/text_tmp.txt'
|
|
output_img_path = 'temp/output.jpg'
|
|
ttf_path = ''
|
|
namedict_path = "wordCloud/text/namedict.txt"
|
|
stopwords_path = 'wordCloud/text/stopword.txt'
|
|
savepath = 'wordCloud/pic/convert.jpg'
|
|
|
|
def __init__(self):
|
|
self.ui = QUiLoader().load('mainUI.ui')
|
|
self.ui.pushButton.clicked.connect(self.get_img)
|
|
self.ui.fontButton.clicked.connect(self.get_ttf)
|
|
self.ui.pushButton_3.clicked.connect(self.generate_img)
|
|
self.ui.pushButton_2.clicked.connect(self.save_img)
|
|
|
|
def get_img(self):
|
|
self.fileDirectory = ''
|
|
self.fileDirectory = QFileDialog.getOpenFileName(QMainWindow(), "选择图片", "", "JPG (*.jpg)")[0]
|
|
if self.fileDirectory != '':
|
|
pixmap = QPixmap(self.fileDirectory).scaled(self.ui.label_3.size(), aspectMode=QtCore.Qt.KeepAspectRatio)
|
|
self.ui.label_3.setPixmap(pixmap)
|
|
self.ui.label_3.repaint()
|
|
|
|
def generate_img(self):
|
|
if self.fileDirectory == '' or self.ttf_path == '':
|
|
return
|
|
words = self.ui.textEdit.toPlainText()
|
|
self.out_txt(words)
|
|
|
|
max_words_num = self.ui.spinBox.value()
|
|
bg_color = self.ui.lineEdit.text()
|
|
|
|
Draw.draw_wordcloud(self.savepath, self.ttf_path, self.text_path, self.stopwords_path, bg_color, max_words_num, self.output_img_path, self.fileDirectory, self.savepath, self.namedict_path)
|
|
|
|
pixmap = QPixmap(self.output_img_path).scaled(self.ui.label_4.size(), aspectMode=QtCore.Qt.KeepAspectRatio)
|
|
self.ui.label_4.setPixmap(pixmap)
|
|
self.ui.label_4.repaint()
|
|
self.ui.pushButton_2.setEnabled(True)
|
|
|
|
def get_ttf(self):
|
|
self.ttf_path = ''
|
|
self.ttf_path = QFileDialog.getOpenFileName(QMainWindow(), "选择字体", "", "TTF (*.ttf)")[0]
|
|
if self.ttf_path != '':
|
|
self.ui.fontButton.setText(self.ttf_path)
|
|
|
|
def out_txt(self, info):
|
|
f = open(self.text_path, 'w', encoding='UTF-8')
|
|
f.write(info)
|
|
f.close()
|
|
|
|
def save_img(self):
|
|
my_save_path = QFileDialog.getSaveFileName(QMainWindow(), '保存文件', '', 'JPG (*.jpg)')[0]
|
|
shutil.copy(self.output_img_path, my_save_path)
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication([])
|
|
stats = Stats()
|
|
stats.ui.show()
|
|
app.exec_() |