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_())
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.