Jieke Wei
Jieke Wei

Reputation: 333

pygame image scale failed

I am about to resize a image. Here is the code:

def display_image(self):
    print('the original size of the image is: ' + str(self.image.get_rect().size))
    self.resize_image(self.image)
    print('the size after resizing is: ' + str(self.image.get_rect().size))
    #some other code in the function

def resize_image(self, image):
    ratio_w = image.get_width()/self.width  #self.width is the width of the screen
    ratio_h = image.get_height()/self.height  #self.height is the height of the screen
    ration = max(ratio_w, ratio_h)
    new_w = image.get_width()/ratio
    new_h = image.get_height()/ration
    image = pygame.transform.scale(image, (int(new_w),int(new_h)))

According to the print result, the size of the image is not changed.

Upvotes: 0

Views: 55

Answers (1)

001
001

Reputation: 13533

In resize_image,

image = pygame.transform.scale(image, (int(new_w),int(new_h)))

is reassigning the parameter to the function. It's not changing self.image.

You could return the new image and assign it back to self.image.

def resize_image(self, image):
    #....
    return pygame.transform.scale(image, (int(new_w),int(new_h)))

And

def display_image(self):
    print('the original size of the image is: ' + str(self.image.get_rect().size))
    self.image = self.resize_image(self.image)
    print('the size after resizing is: ' + str(self.image.get_rect().size))
    #some other code in the function

Upvotes: 1

Related Questions