python實(shí)現(xiàn)二次元圖片展示(屏保)
下面實(shí)現(xiàn)內(nèi)容:
程序的端口是https://www.dmoe.cc/random.php,也是這位謫仙人給的。需要一個(gè)參數(shù):return=json。說明文檔見:https://www.dmoe.cc/random.php。
卷 Data 的文件夾 PATH 列表
卷序列號(hào)為 90AF-CB35
D:.
│ 圖片展示.py
│
└─temp
直接敲程序,還需要一個(gè)名為temp的文件夾。
請?zhí)崆鞍惭brequests、pygame模塊。
首先放出備用程序:
from requests import get
from json import dumps
from random import randint
import pygame
from pygame.locals import *?
url = 'https://www.dmoe.cc/random.php'
params = {'return':'json'}
response = get(url,params).json()
width,height,img = int(response['width']),int(response['height']),response['imgurl']
content = get(img)
number = randint(100000,999999)
with open('temp/%d.jpg' % number,'wb') as f:
? ? f.write(content.content)
pygame.init()
canvas = pygame.display.set_mode((width,height))
canvas.fill((255,255,255))
pygame.display.set_caption('Show')
def handle():
? ? for event in pygame.event.get():
? ? ? ? if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? exit()
? ??
background = pygame.image.load('temp/%d.jpg' % number) ? ?
while True:
? ? canvas.blit(background,(0,0))
? ? handle()
? ? pygame.display.update()Okay,這里就不展示了,就是一個(gè)簡單的屏保效果,關(guān)閉請ESC鍵。
再放出真實(shí)程序:
小歪API,https://api.ixiaowai.cn/api/api.php,直接發(fā)get請求即可,可以用Postman/APIfox調(diào)試。

from requests import get
from json import dumps
from random import randint
import pygame
from pygame.locals import *?
content = get('https://api.ixiaowai.cn/api/api.php')
number = randint(100000,999999)
with open('temp/%d.jpg' % number,'wb') as f:
? ? f.write(content.content)
pygame.init()
canvas = pygame.display.set_mode((1920,1080))
canvas.fill((255,255,255))
pygame.display.set_caption('Show')
def handle():
? ? for event in pygame.event.get():
? ? ? ? if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? exit()
? ??
background = pygame.image.load('temp/%d.jpg' % number) ? ?
while True:
? ? canvas.blit(background,(0,0))
? ? handle()
? ? pygame.display.update()更新:
想要會(huì)變化的?這里:
from requests import get
from json import dumps
from random import randint
import pygame
from pygame.locals import *?
#創(chuàng)建pygame窗口
pygame.init()
canvas = pygame.display.set_mode((1920,1080))
canvas.fill((255,255,255))
pygame.display.set_caption('Show')
#事件處理
def handle():
? ? for event in pygame.event.get():
? ? ? ? if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? exit()
? ? ??
while True:
?? ?#下載圖片
? ? content = get('https://api.ixiaowai.cn/api/api.php')
? ? number = randint(100000,999999)
? ? with open('temp/%d.jpg' % number,'wb') as f:
? ? ? ? f.write(content.content)
? ? #裝填圖片
? ? background = pygame.image.load('temp/%d.jpg' % number) ?
? ? canvas.blit(background,(0,0))
? ? #設(shè)置窗口
? ? handle()
? ? pygame.display.update()
? ? pygame.time.delay(5000) #秒數(shù)是多少,就寫幾千秒,這是五秒一換圖片下載器:
from requests import get ?# get請求方法
from json import dumps ?# json序列處理
from random import randint ?# 隨機(jī)數(shù)
from os.path import exists ?# 檢測文件夾是否存在
from os import mkdir ?# 創(chuàng)建文件夾
# 下載的爬蟲
def download(path):
? ? url = "https://api.ixiaowai.cn/api/api.php" ?# 請求URL
? ? content = get(url) ?# 發(fā)送網(wǎng)絡(luò)請求
? ? number = randint(100000, 999999) ?# 生成隨機(jī)數(shù)
? ? print("保存圖片 >>> ./%s/%d.jpg" % (path, number)) ?# 輸出保存信息
? ? with open("%s/%d.jpg" % (path, number), "wb") as f: ?# 保存圖片信息
? ? ? ? f.write(content.content)
# 主函數(shù)
def main(path):
? ? # 下載
? ? for i in range(1, int(input("您需要多少張圖片 >>> ")) + 1):
? ? ? ? download(path)
? ? # 保留解釋器窗口
? ? input("下載完成。")
# 保存路徑的程序
if __name__ == "__main__":
? ? try: ?# 包含異常
? ? ? ? folder = input("您需要將圖片保存到哪里(輸入相對路徑) >>> ")
? ? ? ? if exists(folder): ?# 有這個(gè)文件夾就直接保存
? ? ? ? ? ? main(folder) ?# 下載
? ? ? ? else: ?# 如果沒有這個(gè)文件夾
? ? ? ? ? ? mkdir(folder) ?# 創(chuàng)建文件夾
? ? ? ? ? ? main(folder) ?# 下載
? ? except Exception as e: ?# 提取異?;?
? ? ? ? print("ERROR:%s" % e) ?# 輸出異常
到此這篇關(guān)于python實(shí)現(xiàn)二次元圖片展示(屏保)的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)二次元圖片展示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python經(jīng)典練習(xí)百題之猴子吃桃三種解法
這篇文章主要給大家介紹了關(guān)于python經(jīng)典練習(xí)百題之猴子吃桃三種解法的相關(guān)資料, Python猴子吃桃子編程是一個(gè)趣味性十足的編程練習(xí),在這個(gè)練習(xí)中,我們將要使用Python語言來模擬一只猴子吃桃子的過程,需要的朋友可以參考下2023-10-10
python分布式爬蟲中消息隊(duì)列知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是python分布式爬蟲中消息隊(duì)列知識(shí)點(diǎn)詳解內(nèi)容,有興趣的朋友們可以參考下。2020-11-11
python中使用numpy包的向量矩陣相乘np.dot和np.matmul實(shí)現(xiàn)
本文主要介紹了python中使用numpy包的向量矩陣相乘np.dot和np.matmul實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python?for循環(huán)詳細(xì)講解(附代碼實(shí)例)
這篇文章主要給大家介紹了關(guān)于Python?for循環(huán)詳細(xì)講解的相關(guān)資料,在Python中,for循環(huán)是一種常用的控制結(jié)構(gòu),用于遍歷序列(如列表、元組、字符串等)中的元素,需要的朋友可以參考下2024-03-03
Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊(duì)列的操作方法
這篇文章主要介紹了Python 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)-循環(huán)隊(duì)列的操作方法,需要的朋友可以參考下2019-07-07
python基于Tkinter庫實(shí)現(xiàn)簡單文本編輯器實(shí)例
這篇文章主要介紹了python基于Tkinter庫實(shí)現(xiàn)簡單文本編輯器,實(shí)例分析了Python使用Tkinter庫實(shí)現(xiàn)簡單桌面應(yīng)用程序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法
今天小編就為大家分享一篇Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

