PyQt4 爬虫 鼠标事件的综合应用

#-*- coding:utf-8 -*-

from  PyQt4.QtGui  import *
from  PyQt4.QtCore  import *
import sys,re
import urllib2
import argparse
import os
import random
import requests
#reload(sys)
#sys.setdefaultencoding("utf-8")

headers = {
    "User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36",
   'Referer':'http://m.mzitu.com/15309'}

PIC=[".jpg",".JPG",".PNG",".png",".JPEG",".jpeg",".bmp",".BMP",".gif",".GIF"]

#*******************************************************************
#*******************************************************************
#***************************布局类**********************************
#*******************************************************************
#*******************************************************************
class graphicsView(QGraphicsView):
    def __init__(self,parent=None):
        super(graphicsView,self).__init__(parent)
        self._parent=parent
        self.zoomscale=1
        self.pos=0

    def read_txt(self,path):
        mt=[]
        f=open(path,'r')
        for t in f.readlines():
            mt.append(t)
        f.close()
        return mt
    def mouseDoubleClickEvent(self,event):
            path="data.txt"
            if event.button()==Qt.RightButton:
                #drag = QDrag()
                mt=self.read_txt(path)
                self._parent._tree.zoomscale=1
                url=mt[random.randrange(0,len(mt))]
                url=url.replace("\n","")
                self.pos+=1
                self._parent.pic_address.setText(url)
                
   
                request =  urllib2.Request(url, headers=headers)
                response = urllib2.urlopen(request)
                                       
                self.image=QPixmap()
                
                self.image.loadFromData(response.read())

                
                self.graphicsView= QGraphicsScene()            
                self._parent._tree.item = QGraphicsPixmapItem(self.image)
                self._parent._tree.item.setFlag(QGraphicsItem.ItemIsMovable)  
                self._parent._tree.graphicsView.addItem(self._parent._tree.item)

                self._parent._tree.setAlignment(Qt.AlignLeft)
                if self.image.width()!=500:
                    try:
                        self._parent._tree.item.setScale(500.0/self.image.width())
                    except:
                        self._parent._tree.item.setScale(0.7)
                self._parent._tree.setScene(self.graphicsView)
                #drag.exec_() #exec()函数并不会阻塞主函数
     
            
            
 


    def dragEnterEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()
    def dragMoveEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()

    def dropEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                filepath = str(urls[0].path())[1:]
                temp=0
                for pic in PIC:
                    if pic == os.path.splitext(filepath)[1]:
                        temp=1
                if temp==1 :
                    self._parent.pic_address.setText(filepath)
                    print(filepath)
                    self.image=QPixmap(filepath)
                    self.graphicsView= QGraphicsScene()            
                    self._parent._tree.item = QGraphicsPixmapItem(self.image)
                    
                    self._parent._tree.item.setFlag(QGraphicsItem.ItemIsMovable)  # 使图元可以拖动,非常关键!!!!!
                    #self.item.setScale(self.zoomscale)
                    self._parent._tree.graphicsView.addItem(self.item)
                    #self.setAlignment(Qt.AlignLeft and Qt.AlignTop)
                    self._parent._tree.setAlignment(Qt.AlignLeft)
                    if self.image.width()!=500:
                        self._parent._tree.item.setScale(500.0/self.image.width()) 
                    self._parent._tree.setScene(self.graphicsView)
                else:
                    QMessageBox.information(self,u"提示", u"不是图片")


    def wheelEvent(self, event):
            angle=event.delta()                        
  
            #print(angleX,angleY)
            if angle >= 0:
                if self.zoomscale<1.5:
                    try:
                        self.zoomscale=self.zoomscale+0.1
                        self._parent._tree.item.setScale(self.zoomscale)
                        #self.setAlignment(Qt.AlignCenter and Qt.AlignTop)
                    except:
                        pass
           
           
            elif angle <=  0:
                if self.zoomscale>0.2:
                    try:
                        self.zoomscale=self.zoomscale-0.1
                        self._parent._tree.item.setScale(self.zoomscale)
                        #self.setAlignment(Qt.AlignCenter and Qt.AlignTop)
                    except:
                        pass
           
                
#*******************************************************************
#*******************************************************************
#***************************拖拽类**********************************
#*******************************************************************
#*******************************************************************

class MyLineEdit(QLineEdit):
        def __init__( self, parent=None ):
            super(MyLineEdit, self).__init__(parent)
            self.setDragEnabled(True)

        def dragEnterEvent( self, event ):
            
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()
        def dragMoveEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()

        def dropEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                filepath = str(urls[0].path())[1:]
                self.setText(filepath)

#*******************************************************************
#*******************************************************************
#***************************功能类**********************************
#*******************************************************************
#*******************************************************************
class Nude_transform_gui(QWidget):
    
    def __init__(self):
        super(Nude_transform_gui,self).__init__()
        #self.setWindowFlags(Qt.Window)
        self.setWindowTitle("Ai_Nude_Tool_a")
        
        self.initUI()
    def initUI(self):


        pic_address=QLabel(u'图片地址:')
        self.pic_address=MyLineEdit(r'http://www.laotuzi.com/d/file/meizitu/9351fa8c196482040f2d46811a2585eb.jpg')
        pic_button=QPushButton(u"加载")

        save_address=QLabel(u'图片地址:')
        self.save_address=MyLineEdit(os.path.abspath(os.path.dirname(__file__)))
        save_button=QPushButton(u"浏览")

        
        self._tree=graphicsView(self)
 
        
        start_work=QPushButton(u"转换图片")
 


        pbar=QLabel(u"进度")
        self.time_pos=QLineEdit()
        self.pbar = QProgressBar()

        #print dir(self.pbar)

        
        #groupNameData.setFrameStyle(QFrame.Panel|QFrame.Sunken)
        

        
        laty_1=QHBoxLayout()
        laty_1.addWidget(pic_address)
        laty_1.addWidget(self.pic_address)
        laty_1.addWidget(pic_button)
     

        laty_2=QHBoxLayout()
        laty_2.addWidget(self._tree)
        
        
      

        laty_3=QHBoxLayout()
        laty_3.addWidget(save_address)
        laty_3.addWidget(self.save_address)
        laty_3.addWidget(save_button)


        laty_4=QHBoxLayout()
        laty_4.addStretch(10)
        laty_4.addSpacing(10)
        laty_4.addWidget(self.time_pos,1)
        laty_4.addWidget(pbar,1)
        laty_4.addWidget(self.pbar ,3)
        laty_4.addWidget(start_work,2)




        all_lay=QVBoxLayout()
        all_lay.addLayout(laty_1)
        all_lay.addLayout(laty_2)
        all_lay.addLayout(laty_3)
        all_lay.addLayout(laty_4)


      
        self.setLayout(all_lay)
        
        self.resize(600,700)


        pic_button.clicked.connect(self.get_pic_address)
        save_button.clicked.connect(self.get_save_address)
        start_work.clicked.connect(self.transform_nude)
   
   


    def get_pic_address(self):
            pic_address=self.pic_address.text()
            if(pic_address==""):
                QMessageBox.information(self,u"提示", u"请输入图片地址")
                return
            name=pic_address
            if os.path.exists(name):
                self._tree.zoomscale=1
                self._tree.image=QPixmap(name)
                self._tree.graphicsView= QGraphicsScene()            
                self._tree.item = QGraphicsPixmapItem(self._tree.image)
                self._tree.item.setFlag(QGraphicsItem.ItemIsMovable)  # 使图元可以拖动,非常关键!!!!!
                self._tree.graphicsView.addItem(self._tree.item)
                #self._tree.setAlignment(Qt.AlignLeft and Qt.AlignTop)
                self._tree.setAlignment(Qt.AlignLeft)
                if self._tree.image.width()!=500:
                    self._tree.item.setScale(500.0/self._tree.image.width()) 
                self._tree.setScene(self._tree.graphicsView)
            else:
                self._tree.zoomscale=1
                req = requests.get(name,headers=headers)
                self._tree.image=QPixmap()
                self._tree.image.loadFromData(req.content)

                
                self._tree.graphicsView= QGraphicsScene()            
                self._tree.item = QGraphicsPixmapItem(self._tree.image)
                self._tree.item.setFlag(QGraphicsItem.ItemIsMovable)  # 使图元可以拖动,非常关键!!!!!
                self._tree.graphicsView.addItem(self._tree.item)
                #self._tree.setAlignment(Qt.AlignLeft and Qt.AlignTop)
                self._tree.setAlignment(Qt.AlignLeft)
                if self._tree.image.width()!=500:
                    try:
                        self._tree.item.setScale(500.0/self._tree.image.width())
                    except:
                        self._tree.item.setScale(0.7)
                self._tree.setScene(self._tree.graphicsView)

                
                
    def get_save_address(self):
        filename = QFileDialog.getExistingDirectory()
        if filename:
            filename=filename.replace("\\",'/')

    def createRandomString(self,len):
        print ('wet'.center(10,'*'))
        raw = ""
        range1 = range(58, 65) # between 0~9 and A~Z
        range2 = range(91, 97) # between A~Z and a~z

        i = 0
        while i < len:
            seed = random.randint(48, 122)
            if ((seed in range1) or (seed in range2)):
                continue;
            raw += chr(seed);
            i += 1
        return raw
    


    def transform_nude(self):
        self.pbar.setValue(5)
        if str(self.pic_address.text())=="" or str(self.save_address.text())=="":
            QMessageBox.information(self,u"提示", u"请输入图片储存地址,视频名称可不写")
            return
        pic_address=str(self.pic_address.text())
        output_address=str(self.save_address.text())
        if "." in str(self.save_address.text()):
            output_address=str(self.save_address.text())
        else:
            
            output_address=str(self.save_address.text())+"/" +"_"+self.createRandomString(5)+"."+pic_address.split(".")[-1]
        if os.path.exists(pic_address):
            QMessageBox.information(self,u"提示", u"本地图片不可下载!")
            return
        else:
            request =  urllib2.Request(url=pic_address, headers=headers)
            response = urllib2.urlopen(request)
            
            with open(output_address, "wb") as f:
                f.write(response.read())

            QMessageBox.information(self,u"提示", u"下载成功!")


#*******************************************************************
#*******************************************************************
#***************************主函数***********************************
#*******************************************************************
#******************************************************************* 

if __name__ == '__main__': 
    app = QApplication(sys.argv)
    bili = Nude_transform_gui()
    bili.show()
    sys.exit(app.exec_())

data.txt 建立一个名称为data的txt文件。放入图片链接,这里的链接是通过爬虫批量爬去的,自己想添加可以手动复制进去,必须每个链接为一行,否则无法识别。

http://i.meizitu.net/2018/11/09b07.jpg
http://i.meizitu.net/2018/11/09b06.jpg
http://i.meizitu.net/2018/11/09b05.jpg
http://i.meizitu.net/2018/11/06b08.jpg
http://i.meizitu.net/2018/11/06b07.jpg
http://i.meizitu.net/2018/06/02c08.jpg
http://i.meizitu.net/2018/06/02c07.jpg
http://i.meizitu.net/2018/06/02c06.jpg
http://i.meizitu.net/2018/06/02c05.jpg
http://i.meizitu.net/2018/06/02c04.jpg
http://i.meizitu.net/2018/06/02c19.jpg
http://i.meizitu.net/2018/06/02c18.jpg
http://i.meizitu.net/2018/06/02c17.jpg
http://i.meizitu.net/2018/06/02c34.jpg
http://i.meizitu.net/2018/06/02c33.jpg
http://i.meizitu.net/2018/06/02c32.jpg
http://i.meizitu.net/2018/06/02c31.jpg
http://i.meizitu.net/2018/06/02c30.jpg
http://i.meizitu.net/2018/06/02c42.jpg
http://i.meizitu.net/2018/06/02c41.jpg
http://i.meizitu.net/2018/06/02c40.jpg
http://i.meizitu.net/2018/05/09b42.jpg
http://i.meizitu.net/2018/03/31a09.jpg
http://i.meizitu.net/2018/01/03b08.jpg
http://i.meizitu.net/2018/01/03b07.jpg
http://i.meizitu.net/2018/01/03b06.jpg
http://i.meizitu.net/2018/02/21c07.jpg
http://i.meizitu.net/2018/02/21c09.jpg
http://i.meizitu.net/2018/02/21c05.jpg
http://i.meizitu.net/2018/02/21c08.jpg
http://i.meizitu.net/2018/02/21c06.jpg
http://i.meizitu.net/2018/06/28f05.jpg
http://i.meizitu.net/2018/06/28f03.jpg
http://i.meizitu.net/2018/06/28f04.jpg
http://i.meizitu.net/2018/06/28f20.jpg
http://i.meizitu.net/2018/06/28f19.jpg
http://i.meizitu.net/2018/06/28f18.jpg
http://i.meizitu.net/2018/06/28f17.jpg
http://i.meizitu.net/2018/06/28f16.jpg
http://i.meizitu.net/2018/06/28f34.jpg
http://i.meizitu.net/2018/06/28f33.jpg
http://i.meizitu.net/2018/06/28f32.jpg
http://i.meizitu.net/2018/03/30b61.jpg
http://i.meizitu.net/2018/03/30b60.jpg
http://i.meizitu.net/2018/03/30b59.jpg
http://i.meizitu.net/2018/10/01b09.jpg
http://i.meizitu.net/2018/10/01b08.jpg
http://i.meizitu.net/2018/10/01b23.jpg
http://i.meizitu.net/2018/10/01b22.jpg
http://i.meizitu.net/2018/10/01b21.jpg
http://i.meizitu.net/2018/08/12a08.jpg
http://i.meizitu.net/2018/08/12a07.jpg
http://i.meizitu.net/2018/08/12a06.jpg
http://i.meizitu.net/2018/08/12a05.jpg
http://i.meizitu.net/2018/08/12a20.jpg
http://i.meizitu.net/2018/08/12a19.jpg
http://i.meizitu.net/2018/08/12a18.jpg
http://i.meizitu.net/2018/08/12a17.jpg
http://i.meizitu.net/2018/08/12a36.jpg
http://i.meizitu.net/2018/08/12a35.jpg
http://i.meizitu.net/2018/08/12a34.jpg
http://i.meizitu.net/2018/08/12a33.jpg
http://i.meizitu.net/2018/08/12a43.jpg
http://i.meizitu.net/2018/08/12a42.jpg
http://i.meizitu.net/2018/08/12a41.jpg
http://i.meizitu.net/2018/08/12a40.jpg
http://i.meizitu.net/2018/07/19a29.jpg
http://i.meizitu.net/2018/07/19a28.jpg
http://i.meizitu.net/2018/07/19a27.jpg
http://i.meizitu.net/2018/07/19a26.jpg
http://i.meizitu.net/2018/03/19a65.jpg
http://i.meizitu.net/2018/03/19a64.jpg
http://i.meizitu.net/2018/03/19a63.jpg
http://i.meizitu.net/2018/03/19a62.jpg
http://i.meizitu.net/2018/03/21c40.jpg
http://i.meizitu.net/2018/03/08b09.jpg
http://i.meizitu.net/2018/03/08b08.jpg
http://i.meizitu.net/2018/03/08b07.jpg
http://i.meizitu.net/2018/03/08b06.jpg
http://i.meizitu.net/2018/03/08b17.jpg
http://i.meizitu.net/2018/03/08b16.jpg
http://i.meizitu.net/2018/03/08b15.jpg
http://i.meizitu.net/2018/03/08b14.jpg
http://i.meizitu.net/2018/03/08b25.jpg
http://i.meizitu.net/2018/03/08b24.jpg
http://i.meizitu.net/2018/03/08b23.jpg
http://i.meizitu.net/2018/03/08b22.jpg
http://i.meizitu.net/2018/03/08b34.jpg
http://i.meizitu.net/2018/03/08b33.jpg
http://i.meizitu.net/2018/03/08b32.jpg
http://i.meizitu.net/2018/03/08b31.jpg
http://i.meizitu.net/2018/03/08b42.jpg
http://i.meizitu.net/2018/03/08b41.jpg
http://i.meizitu.net/2018/03/08b40.jpg
http://i.meizitu.net/2018/03/08b39.jpg
http://i.meizitu.net/2018/03/08b46.jpg
http://i.meizitu.net/2018/03/08b45.jpg
http://i.meizitu.net/2018/03/08b44.jpg
http://i.meizitu.net/2018/03/08b43.jpg
http://i.meizitu.net/2018/08/14c46.jpg
http://i.meizitu.net/2018/08/14c45.jpg
http://i.meizitu.net/2018/08/14c44.jpg
http://i.meizitu.net/2018/08/14c43.jpg
http://i.meizitu.net/2018/08/14c55.jpg
http://i.meizitu.net/2018/08/14c54.jpg
http://i.meizitu.net/2018/08/14c60.jpg
http://i.meizitu.net/2018/08/14c59.jpg
http://i.meizitu.net/2018/08/14c58.jpg
http://i.meizitu.net/2018/08/14c64.jpg
http://i.meizitu.net/2018/08/14c63.jpg
http://i.meizitu.net/2018/08/14c62.jpg
http://i.meizitu.net/2018/08/14c61.jpg
http://i.meizitu.net/2018/08/27c03.jpg
http://i.meizitu.net/2018/08/27c02.jpg
http://i.meizitu.net/2018/10/19a10.jpg
http://i.meizitu.net/2018/08/27c11.jpg
http://i.meizitu.net/2018/08/27c10.jpg
http://i.meizitu.net/2018/08/27c09.jpg
http://i.meizitu.net/2018/08/27c08.jpg
http://i.meizitu.net/2018/10/13a05.jpg
http://i.meizitu.net/2018/10/13a04.jpg
http://i.meizitu.net/2018/10/13a03.jpg
http://i.meizitu.net/2018/10/13a02.jpg
http://i.meizitu.net/2018/09/03c03.jpg
http://i.meizitu.net/2018/09/03c02.jpg
http://i.meizitu.net/2018/01/22c05.jpg
http://i.meizitu.net/2018/09/03c12.jpg
http://i.meizitu.net/2018/09/03c11.jpg
http://i.meizitu.net/2018/09/03c10.jpg
http://i.meizitu.net/2018/09/03c09.jpg
http://i.meizitu.net/2018/09/03c22.jpg
http://i.meizitu.net/2018/09/03c21.jpg
http://i.meizitu.net/2018/09/03c20.jpg
http://i.meizitu.net/2018/09/03c19.jpg
http://i.meizitu.net/2018/09/03c27.jpg

PyQt4 爬虫 鼠标事件的综合应用_第1张图片

PyQt4 爬虫 鼠标事件的综合应用_第2张图片
1,第一个文本框可以直接复制网络图片地址,点击加载可以预览,点击转换图片可以下载,第二个文本框若为目录,图片会自动名称,为具体图片名称,以图片名称为准。
2,双击右键可以切换图片,不过必须有data.txt,里面必须有图片的地址,这些图片并没有下载,必须点击转换图片才会下载。
3,左键可以拖动图片到任意位置。
4,滑轮可以缩放图片。
5,文本框皆支持拖入路径的操作。
6,也可以直接拖入本地图片预览,本地图片不支持下载。
7,本地图片可以直接拖入中间区域,也可以拖入文本框后,点击加载,效果一致。
8,data.txt网络图片地址可以支持自己清空之后在复制进去,不过每个图片地址必须单独为一行。

你可能感兴趣的:(python,PyQt4,爬虫)