日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 在线预览文件_用Python PyQt写一个在线预览图片的GUI

發(fā)布時間:2025/3/8 python 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 在线预览文件_用Python PyQt写一个在线预览图片的GUI 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在爬完網(wǎng)上一篇帖子,并得到其中的所有圖片鏈接后,寫一個GUI來實現(xiàn)在線預(yù)覽是一個很自然的想法, 相當于實現(xiàn)一個python版的圖片瀏覽器, 通過這個練習,可以讓我們更熟悉PyQt這個庫。

這里我用的是PyQt4。

以下是我的寫的程序,可以實現(xiàn)以下幾個功能。

預(yù)覽圖片:

實現(xiàn)鼠標左鍵單擊即可下翻至下一張圖片,鼠標右鍵單擊則是返回前一張圖片。

利用本地Cache來解決反復(fù)讀取的問題。 比如說對同樣的兩張圖片我們想來回比較,我們沒必要每次都重新下載。我們可以將圖片保存到本地,當以后調(diào)用同一張圖片時,直接從本地讀取緩存來加速。如果我們不想讓外界看到緩存的圖片,則可以對其進行加密 (尚未實現(xiàn))。

對動圖gif的支持。普通的 jpg 和 Png 格式,利用QLable 即可以顯示,但是對于gif,我們則必須利用QMovie來讓其動起來。

由于我剛接觸這個庫,仍然在學習,所以寫的不妥或者冗余的地方,希望大家指出。

# coding=utf-8

import sys

import pycurl

import os

import time

from StringIO import StringIO

import re

from PyQt4 import QtGui,QtCore

from PyQt4.QtGui import *

from PyQt4.QtCore import *

# class definition

class Pic_Label(QtGui.QLabel):

def __init__(self):

super(Pic_Label,self).__init__()

self.setFrameStyle(QtGui.QFrame.StyledPanel)

self.cache_map={}

def paintEvent(self, event):

if self.extention !="gif":

size = self.size()

painter = QtGui.QPainter(self)

point = QtCore.QPoint(0,0)

scaledPix = self.pixmap.scaled(size, Qt.KeepAspectRatio, transformMode = Qt.SmoothTransformation)

# start painting the label from left upper corner

point.setX((size.width() - scaledPix.width())/2)

point.setY((size.height() - scaledPix.height())/2)

#print point.x(), ' ', point.y()

painter.drawPixmap(point, scaledPix)

else:

QLabel.paintEvent(self, event)

def mouseReleaseEvent(self,ev):

#self.emit(SIGNAL('clicked()'))

if ev.button() == Qt.RightButton:

self.emit(SIGNAL("RightClick"))

else:

self.emit(SIGNAL("LeftClick"))

def set_image(self,pic_url,index):

if (index in self.cache_map) == False:

self.cache_map[index]=False

self.pixmap = QtGui.QPixmap()

self.retrieve_from_url_cache(pic_url,index)

def retrieve_from_url_cache(self,pic_url,index):

try:

self.extention=re.search(r"\.(\w+)$", pic_url).group(1)

except:

self.extention="jpg"

cache_pic_name="Pic_"+str(index)+"."+self.extention

cache_pic_path=os.getcwd()+"\Cache_Pic\\"+cache_pic_name

if self.cache_map[index]==True:

if self.extention =="gif":

movie = QtGui.QMovie(cache_pic_path)

self.setMovie(movie)

movie.start()

else:

#print "Cached!" + cache_pic_path

if self.pixmap.load(cache_pic_path) == False:

#print "use jpg to try again"

if self.pixmap.load(cache_pic_path)== False:

#last resort, try again

self.retrieve_from_url(pic_url,index,cache_pic_path)

self.setPixmap(self.pixmap) # udpate immediately

else:

if self.extention =="gif":

data=self.retrieve_from_url(pic_url,index,cache_pic_path)

self.pixmap.loadFromData(data)

f = open(cache_pic_path, 'wb')

f.write(data)

f.close()

movie = QtGui.QMovie(cache_pic_path)

self.setMovie(movie)

movie.start()

else:

data=self.retrieve_from_url(pic_url,index,cache_pic_path)

self.pixmap.loadFromData(data)

self.pixmap.save(cache_pic_path)

self.setPixmap(self.pixmap) # udpate immediately

def retrieve_from_url(self,pic_url,index,file_path):

c = pycurl.Curl()

c.setopt(pycurl.PROXY, 'http://192.168.87.15:8080')

c.setopt(pycurl.PROXYUSERPWD, 'LL66269:')

c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM)

buffer = StringIO()

c.setopt(pycurl.URL, pic_url)

c.setopt(c.WRITEDATA, buffer)

c.perform()

c.close()

data = buffer.getvalue()

self.cache_map[index]=True

return data

def setMovie(self,movie):

QLabel.setMovie(self, movie)

s=movie.currentImage().size()

self._movieWidth = s.width()

self._movieHeight = s.height()

class Example(QtGui.QWidget):

def __init__(self,thread_url_list):

super(Example, self).__init__()

self.url_list=thread_url_list

self.current_pic_index=0

cwd = os.getcwd()

#print cwd

directory=cwd+"\Cache_Pic"

#print directory

if not os.path.exists(directory):

os.makedirs(directory)

self.initUI()

# making subfolderss to cache pictures

def initUI(self):

layout = QtGui.QGridLayout()

self.label = Pic_Label()

self.label.set_image(self.url_list[0],0)

#self.label = QLabel()

#movie = QtGui.QMovie("Cache_Pic/Pic_0.gif")

#self.label.setMovie(movie)

#movie.start()

layout.addWidget(self.label)

layout.setRowStretch(0,1)

layout.setColumnStretch(0,1)

#self.connect(self.label,SIGNAL('clicked()'),self.fun_next)

self.connect(self.label,SIGNAL("LeftClick"),self.fun_next)

self.connect(self.label,SIGNAL("RightClick"),self.fun_prev)

#b1=QtGui.QPushButton("next")

#b2=QtGui.QPushButton("prev")

#b1.clicked.connect(self.fun_next)

#b2.clicked.connect(self.fun_prev)

#layout.addWidget(b1)

#layout.addWidget(b2)

self.setLayout(layout)

self.setGeometry(300, 300, 500, 500)

self.setWindowTitle('Picture Viewer')

self.show()

# Connect button to image updating

def fun_next(self):

if self.current_pic_index < len(self.url_list)-1:

self.current_pic_index=self.current_pic_index+1

else:

self.current_pic_index=0

self.label.set_image(self.url_list[self.current_pic_index],self.current_pic_index)

sys.stdout.write('\r')

sys.stdout.write("[ %d ] out of (%d)" % (self.current_pic_index+1,len(self.url_list)))

sys.stdout.flush()

def fun_prev(self):

if self.current_pic_index > 0:

self.current_pic_index=self.current_pic_index-1

else:

self.current_pic_index=len(self.url_list)-1

self.label.set_image(self.url_list[self.current_pic_index],self.current_pic_index)

sys.stdout.write('\r')

sys.stdout.write("[ %d ] out of (%d)" % (self.current_pic_index+1,len(self.url_list)))

sys.stdout.flush()

def view_image():

url_list=['https://i.imgur.com/waprhO3.gif','http://static.cnbetacdn.com/article/2017/0831/7f11d5ec94fa123.png','http://static.cnbetacdn.com/article/2017/0831/1b6595175fb5486.jpg']

viewer_app = QtGui.QApplication(sys.argv)

ex = Example(url_list)

sys.exit(viewer_app.exec_())

view_image()

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python 在线预览文件_用Python PyQt写一个在线预览图片的GUI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。