第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Pygame 中縮放圖像同時保持縱橫比?

如何在 Pygame 中縮放圖像同時保持縱橫比?

梵蒂岡之花 2023-12-12 20:40:01
如何使用pygame.transform.scale但保持相同的縱橫比重新縮放圖像?就像我有一張 16:9 的圖像,我想在保持相同比例的情況下對其進行縮放。我的主要目標是制作一個圖像查看器,因為 Windows 10 需要很長時間才能加載,這是我當前的代碼:import pygamefrom sys import argvfrom win32api import GetSystemMetricspygame.init()pygame.display.init()width=GetSystemMetrics(0)*0.9   #Window size a bit smaller than monoitor sizeheight=GetSystemMetrics(1)*0.8img=pygame.image.load(argv[-1]) #Get image file opened with itimg=pygame.transform.scale(img, (int(width), int(height)))  #Scales image to window sizeMain=pygame.display.set_mode((int(width), int(height)))pygame.display.set_caption(str(argv[-1].split("\\")[-1]))   #Set window title as filenameimgrect=img.get_rect()Main.blit(img, imgrect)pygame.display.update()while True:    for event in pygame.event.get():        if event.type==pygame.QUIT:            pygame.quit()            exit()我想問題是我可以調(diào)整它的大小,但圖像扭曲,或者我可以將其顯示為圖像的寬度和高度,但我無法將圖像縮放到窗口尺寸中的大小,同時保持縱橫比,總是有空格。抱歉,我的問題很模糊,我不知道如何解釋。編輯:已修復。如果其他人也有同樣的問題,我必須標準化比例,通過將兩側除以寬度將其變?yōu)?1:something。然后,我將 1:something 的比率乘以窗口的寬度,并在 while 循環(huán)中,如果圖像的寬度大于窗口的寬度,則減小比例。來源:import pygamefrom sys import argvfrom win32api import GetSystemMetricspygame.init()pygame.display.init()windowwidth=GetSystemMetrics(0)*0.9windowheight=GetSystemMetrics(1)*0.8Main=pygame.display.set_mode((int(windowwidth), int(windowheight)))img=pygame.image.load(argv[-1])imgratiox=int(1)imgratioy=int(img.get_height()/img.get_width())imgwindowwidth=int(windowwidth)imgwindowheight=int(round(img.get_height()/img.get_width(), 2)*windowwidth)scale=1while imgwindowheight>windowheight:    imgwindowheight*=scale    imgwindowwidth*=scale    scale-=0.05img=pygame.transform.scale(img, (int(imgwindowwidth), int(imgwindowheight)))pygame.display.set_caption(str(argv[-1].split("\\")[-1])+ "    Img width:"+str(img.get_width())+"    Img height:"+str(img.get_height()))imgrect=img.get_rect()Main.blit(img, imgrect)pygame.display.update()while True:    for event in pygame.event.get():        if event.type==pygame.QUIT:            pygame.quit()            exit()
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

加載圖像時,調(diào)整窗口以匹配圖像比例。


在此代碼中,設置寬度,然后根據(jù)圖像比例調(diào)整高度。如果高度太大,則減小寬度以允許更小的高度。


import pygame

from sys import argv

from win32api import GetSystemMetrics

pygame.init()

pygame.display.init()


img=pygame.image.load(argv[-1]) #Get image file opened with it


width=GetSystemMetrics(0)*0.9   #Window size a bit smaller than monoitor size

height=width*img.get_height()/img.get_width()  # keep ratio


if height > GetSystemMetrics(1)*0.8:  # too tall for screen

    width = width * (GetSystemMetrics(1)*0.8)/height  # reduce width to keep ratio 

    height = GetSystemMetrics(1)*0.8  # max height


img=pygame.transform.scale(img, (int(width), int(height)))  #Scales image to window size

Main=pygame.display.set_mode((int(width), int(height)))

pygame.display.set_caption(str(argv[-1].split("\\")[-1]))   #Set window title as filename

imgrect=img.get_rect()


Main.blit(img, imgrect)

pygame.display.update()

while True:

    for event in pygame.event.get():

        if event.type==pygame.QUIT:

            pygame.quit()

            exit()

測試輸出


原來的

https://img1.sycdn.imooc.com/657854f2000187ac02570192.jpg

  • 腳本


https://img1.sycdn.imooc.com/657854fd0001f3f702940231.jpg

查看完整回答
反對 回復 2023-12-12
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號