python實現(xiàn)二次元圖片展示(屏保)
下面實現(xiàn)內容:
程序的端口是https://www.dmoe.cc/random.php
,也是這位謫仙人給的。需要一個參數(shù):return=json。說明文檔見:https://www.dmoe.cc/random.php
。
卷 Data 的文件夾 PATH 列表
卷序列號為 90AF-CB35
D:.
│ 圖片展示.py
│
└─temp
直接敲程序,還需要一個名為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,這里就不展示了,就是一個簡單的屏保效果,關閉請ESC鍵。
再放出真實程序:
小歪API,https://api.ixiaowai.cn/api/api.php
,直接發(fā)get
請求即可,可以用Postman/APIfox調試。
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()
更新:
想要會變化的?這里:
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)) ? ? #設置窗口 ? ? handle() ? ? pygame.display.update() ? ? pygame.time.delay(5000) #秒數(shù)是多少,就寫幾千秒,這是五秒一換
圖片下載器:
from requests import get ?# get請求方法 from json import dumps ?# json序列處理 from random import randint ?# 隨機數(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)絡請求 ? ? number = randint(100000, 999999) ?# 生成隨機數(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): ?# 有這個文件夾就直接保存 ? ? ? ? ? ? main(folder) ?# 下載 ? ? ? ? else: ?# 如果沒有這個文件夾 ? ? ? ? ? ? mkdir(folder) ?# 創(chuàng)建文件夾 ? ? ? ? ? ? main(folder) ?# 下載 ? ? except Exception as e: ?# 提取異?;? ? ? ? ? print("ERROR:%s" % e) ?# 輸出異常
到此這篇關于python實現(xiàn)二次元圖片展示(屏保)的文章就介紹到這了,更多相關python實現(xiàn)二次元圖片展示內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python中使用numpy包的向量矩陣相乘np.dot和np.matmul實現(xiàn)
本文主要介紹了python中使用numpy包的向量矩陣相乘np.dot和np.matmul實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02Python 實現(xiàn)數(shù)據(jù)結構-循環(huán)隊列的操作方法
這篇文章主要介紹了Python 實現(xiàn)數(shù)據(jù)結構-循環(huán)隊列的操作方法,需要的朋友可以參考下2019-07-07python基于Tkinter庫實現(xiàn)簡單文本編輯器實例
這篇文章主要介紹了python基于Tkinter庫實現(xiàn)簡單文本編輯器,實例分析了Python使用Tkinter庫實現(xiàn)簡單桌面應用程序的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05