import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from PyQt4 import QtCore, QtGui, uic
import requests
import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
tc_api='http://image.baidu.com/pictureup/uploadshitu'
files={
'image':''
}
data={'pos':'upload'
,'uptype':'upload_pc'
,'fm':'index'}
def upload_file(filepath):
try:
img=open(filepath,'rb')
except Exception,e:
print e
sys.exit(0)
files['image']=img
c=requests.post(tc_api,files=files,data=data)
img_url=re.findall('queryImageUrl=(.*?)&querySign',c.url)[0]
img1=re.sub('%3A',':',img_url)
img2=re.sub('%2F','/',img1)
return img2
qtCreatorFile = "baidu_ui.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.thread=Worker()
self.fileSelect.clicked.connect(self.selectFile)
self.thread.sinOut2.connect(self.show_result)
def selectFile(self):
filepath=self.to_utf8(QtGui.QFileDialog.getOpenFileName(self,u'选择图片','',r'Image Files(*.png *.jpg *.bmp *.jpeg *.gif)'))
self.thread.getPath((filepath,))
self.fileSelect.setEnabled(False)
def show_result(self,result):
img,isTrue=result[0],result[1]
self.markdown_show.setText('![]('+img+')')
self.realurl.setText(img)
self.fileSelect.setEnabled(isTrue)
def to_utf8(self,input):
return unicode(input,'utf8','ignore')
class Worker(QtCore.QThread):
sinOut2 = pyqtSignal(tuple)
def __init__(self,parent=None):
super(Worker,self).__init__(parent)
def getPath(self,filepath):
self.filepath=filepath[0]
print self.filepath
def run(self):
while 1:
print 'start'
img=upload_file(self.filepath)
print img
self.sinOut2.emit((img,True))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
1
tumbzzc OP <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>395</width> <height>154</height> </rect> </property> <property name="windowTitle"> <string>百度图床</string> </property> <widget class="QLineEdit" name="markdown_show"> <property name="geometry"> <rect> <x>120</x> <y>20</y> <width>251</width> <height>31</height> </rect> </property> </widget> <widget class="QLineEdit" name="realurl"> <property name="geometry"> <rect> <x>120</x> <y>60</y> <width>251</width> <height>31</height> </rect> </property> </widget> <widget class="QLabel" name="label_markdown"> <property name="geometry"> <rect> <x>30</x> <y>20</y> <width>91</width> <height>21</height> </rect> </property> <property name="font"> <font> <family>微软雅黑</family> <weight>75</weight> <bold>true</bold> </font> </property> <property name="text"> <string>MarkDown</string> </property> <property name="textFormat"> <enum>Qt::RichText</enum> </property> </widget> <widget class="QLabel" name="label_markdown_2"> <property name="geometry"> <rect> <x>50</x> <y>60</y> <width>61</width> <height>21</height> </rect> </property> <property name="font"> <font> <family>微软雅黑</family> <weight>75</weight> <bold>true</bold> </font> </property> <property name="text"> <string>图片链接</string> </property> <property name="textFormat"> <enum>Qt::RichText</enum> </property> </widget> <widget class="QPushButton" name="fileSelect"> <property name="geometry"> <rect> <x>120</x> <y>110</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>选择图片</string> </property> </widget> </widget> <resources/> <connections/> </ui> 这是 baidu_ui.ui |
2
idea4j 2017-01-13 00:04:46 +08:00
帮顶
|