python實(shí)現(xiàn)二次元圖片展示(屏保)
下面實(shí)現(xiàn)內(nèi)容:
程序的端口是https://www.dmoe.cc/random.php,也是這位謫仙人給的。需要一個(gè)參數(shù):return=json。說(shuō)明文檔見:https://www.dmoe.cc/random.php。
卷 Data 的文件夾 PATH 列表
卷序列號(hào)為 90AF-CB35
D:.
│ 圖片展示.py
│
└─temp
直接敲程序,還需要一個(gè)名為temp的文件夾。
請(qǐng)?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è)簡(jiǎn)單的屏保效果,關(guān)閉請(qǐng)ESC鍵。
再放出真實(shí)程序:
小歪API,https://api.ixiaowai.cn/api/api.php,直接發(fā)get請(qǐng)求即可,可以用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請(qǐng)求方法
from json import dumps ?# json序列處理
from random import randint ?# 隨機(jī)數(shù)
from os.path import exists ?# 檢測(cè)文件夾是否存在
from os import mkdir ?# 創(chuàng)建文件夾
# 下載的爬蟲
def download(path):
? ? url = "https://api.ixiaowai.cn/api/api.php" ?# 請(qǐng)求URL
? ? content = get(url) ?# 發(fā)送網(wǎng)絡(luò)請(qǐng)求
? ? 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("您需要多少?gòu)垐D片 >>> ")) + 1):
? ? ? ? download(path)
? ? # 保留解釋器窗口
? ? input("下載完成。")
# 保存路徑的程序
if __name__ == "__main__":
? ? try: ?# 包含異常
? ? ? ? folder = input("您需要將圖片保存到哪里(輸入相對(duì)路徑) >>> ")
? ? ? ? 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mac使用python識(shí)別圖形驗(yàn)證碼功能
這篇文章主要介紹了mac使用python識(shí)別圖形驗(yàn)證碼功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
python pyautogui手動(dòng)活動(dòng)(模擬鼠標(biāo)鍵盤)自動(dòng)化庫(kù)使用
這篇文章主要為大家介紹了python pyautogui手動(dòng)活動(dòng)(模擬鼠標(biāo)鍵盤)自動(dòng)化庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python內(nèi)建類型dict深入理解源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python內(nèi)建類型dict的深入理解及源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python中利用Scipy包的SIFT方法進(jìn)行圖片識(shí)別的實(shí)例教程
SIFT算法可以檢測(cè)圖片中的局部特征,算法原理相當(dāng)復(fù)雜...但是!Python強(qiáng)大的第三方包Scipy中帶有實(shí)現(xiàn)SIFT算法的SIFT方法,我們只要拿來(lái)用就可以了,下面就為大家?guī)?lái)Python中利用Scipy包的SIFT方法進(jìn)行圖片識(shí)別的實(shí)例教程.2016-06-06
python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression)
這篇文章主要介紹了python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression),幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10
python selenium 對(duì)瀏覽器標(biāo)簽頁(yè)進(jìn)行關(guān)閉和切換的方法
今天小編就為大家分享一篇python selenium 對(duì)瀏覽器標(biāo)簽頁(yè)進(jìn)行關(guān)閉和切換的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
django admin 根據(jù)choice字段選擇的不同來(lái)顯示不同的頁(yè)面方式
這篇文章主要介紹了django admin 根據(jù)choice字段選擇的不同來(lái)顯示不同的頁(yè)面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05

