2016-03-01 126 views
2

我是Python和pyGame的新手,我在縮放圖像時遇到了問題。 我想放大pygame中的圖像。 Pygame的文件稱,pyGame圖像縮放並不像預期的那樣工作

pygame.transform.scale()

應該擴展到新的分辨率。 但在我的下面的例子中它不起作用 - 它裁剪圖像,而不是調整它的大小!? 我在做什麼錯?

#!/usr/bin/env python3 
# coding: utf-8 

import pygame 
from pygame.locals import * 

# Define some colors 
BLACK = (0, 0, 0) 

pygame.init() 

# Set the width and height of the screen [width, height] 
screen = pygame.display.set_mode((1920, 1080)) 

pic = pygame.image.load('test.jpg').convert() 
pic_position_and_size = pic.get_rect() 

# Loop until the user clicks the close button. 
done = False 

# Clear event queue 
pygame.event.clear() 

# -------- Main Program Loop ----------- 
while not done: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      done = True 
     elif event.type == KEYDOWN: 
      if event.key == K_ESCAPE: 
       done = True 

    # background in black 
    screen.fill(BLACK) 

    # Copy image to screen: 
    screen.blit(pic, pic_position_and_size) 

    # Update the screen with what we've drawn. 
    pygame.display.flip() 
    pygame.display.update() 

    pygame.time.delay(10) # stop the program for 1/100 second 

    # decreases size by 1 pixel in x and y axis 
    pic_position_and_size = pic_position_and_size.inflate(-1, -1) 

    # scales the image 
    pic = pygame.transform.scale(pic, pic_position_and_size.size) 

# Close the window and quit. 
pygame.quit() 

回答

1

pygame.transform.scale()不適合您的情況。如果縮小Surface這麼少,算法只會裁剪最後一列和一行像素。如果你現在用同一個Surface一遍又一遍地重複這個過程,你會看到奇怪的行爲。

更好的方法是保留原始Surface的副本,並用它來創建縮放圖像。此外,使用smoothscale代替scale也可能會導致更好的效果;如果你想使用它,取決於你。

這是你的代碼的「固定」版本:

#!/usr/bin/env python3 
# coding: utf-8 

import pygame 
from pygame.locals import * 

# Define some colors 
BLACK = (0, 0, 0) 

pygame.init() 

# Set the width and height of the screen [width, height] 
screen = pygame.display.set_mode((1920, 1080)) 

org_pic = pygame.image.load('test.jpg').convert() 
pic_position_and_size = org_pic.get_rect() 
pic = pygame.transform.scale(org_pic, pic_position_and_size.size) 
# Loop until the user clicks the close button. 
done = False 

# Clear event queue 
pygame.event.clear() 

# -------- Main Program Loop ----------- 
while not done: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      done = True 
     elif event.type == KEYDOWN: 
      if event.key == K_ESCAPE: 
       done = True 

    # background in black 
    screen.fill(BLACK) 

    # Copy image to screen: 
    screen.blit(pic, (0,0)) 

    # Update the screen with what we've drawn. 
    pygame.display.flip() 
    pygame.display.update() 

    pygame.time.delay(10) # stop the program for 1/100 second 

    # decreases size by 1 pixel in x and y axis 
    pic_position_and_size = pic_position_and_size.inflate(-1, -1) 

    # scales the image 
    pic = pygame.transform.smoothscale(org_pic, pic_position_and_size.size) 

# Close the window and quit. 
pygame.quit() 
+0

非常感謝您的快速答覆!解決了我的問題!但是,我想知道爲什麼這在pygame文檔中沒有提及...花了我幾個小時來玩,我嘗試了一切...... – Franky1

相關問題