简体   繁体   English

如何旋转图像而不丢失像素数据? (Pygame)

[英]How to rotate an image without losing pixel data? (Pygame)

Here's my code (Python 3.5): 这是我的代码(Python 3.5):

import sys
import pygame
pygame.init()

screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
running = True

class Actor:

    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.surface = pygame.image.load("GFX/player.bmp")

    def draw(self):
        screen.blit(self.surface, (self.x, self.y))

class Player(Actor):

    def __init__(self):
        Actor.__init__(self, 0, 0, 32, 32)
        self.directions = [False, False, False, False]
        self.speed = 0.1

    def update(self):
        if self.directions[0]:
            self.y -= self.speed
        if self.directions[1]:
            self.y += self.speed
        if self.directions[2]:
            self.x -= self.speed
        if self.directions[3]:
            self.x += self.speed

player = Player()

def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

def redraw():
    screen.fill((75, 0, 0))
    player.draw()
    player.update()
    pygame.display.flip()

while (running):
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            sys.exit()
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_ESCAPE:
                sys.exit()
            if e.key == pygame.K_w:
                player.directions[0] = True
            if e.key == pygame.K_s:
                player.directions[1] = True
            if e.key == pygame.K_a:
                player.directions[2] = True
            if e.key == pygame.K_d:
                player.directions[3] = True
        elif e.type == pygame.KEYUP:
            if e.key == pygame.K_w:
                player.directions[0] = False
            if e.key == pygame.K_s:
                player.directions[1] = False
            if e.key == pygame.K_a:
                player.directions[2] = False
            if e.key == pygame.K_d:
                player.directions[3] = False
        elif e.type == pygame.MOUSEMOTION:
            player.surface = rot_center(player.surface, pygame.mouse.get_pos()[0] / 64)

    redraw()

Pretty straightforward pygame code. 相当简单的pygame代码。 I have a player with a simple image that I've created on mspaint, and I've used this function to rotate the image without causing out of memory issues. 我有一个具有在mspaint上创建的简单图像的播放器,并且使用此功能旋转图像而不会引起内存不足的问题。 I'm rotating the image with the mouse (Considering a player that "aims" somewhere). 我正在用鼠标旋转图像(考虑一个“瞄准”某处的播放器)。 Here's the original image: 这是原始图片:

在此处输入图片说明

And here's the extremely ugly result after moving the mouse a little: 这是在稍微移动鼠标后的极其丑陋的结果:

在此处输入图片说明

I know I'd have better precision using OpenGL (Pyglet, for example), but in that case the rotation function from pygame would be completely useless. 我知道使用OpenGL(例如,Pyglet)会获得更高的精度,但是在那种情况下,pygame的旋转功能将完全没有用。 What am I missing? 我想念什么? What am I doing wrong? 我究竟做错了什么?

Keep in mind that Surfaces in Python are just grids of pixels, not mathematically perfect vector graphics. 请记住,Python中的Surfaces只是像素网格,而不是数学上完美的矢量图形。 Rotating an image will cause slight destruction of quality. 旋转图像会导致质量的轻微破坏。 Continually doing so will eventually garble the image beyond recognition as you can see in your picture. 不断地这样做会最终使图像混乱,使您无法看到图片。 Maintain a reference to the original image and never overwrite it. 保留对原始图像的引用,切勿覆盖它。 When you call rotate, make sure you are rotating the original picture with the cumulative angle with respect to the original, not a previously and incrementally rotated version. 当您调用旋转时,请确保您以相对于原始图像的累积角度旋转原始图像,而不是先前和增量旋转的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM