python - pygame 图像缩放失败
问题描述
我即将调整图像大小。这是代码:
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)))
根据打印结果,图像的大小没有改变。
解决方案
在resize_image
,
image = pygame.transform.scale(image, (int(new_w),int(new_h)))
正在将参数重新分配给函数。它没有改变self.image
。
您可以返回新图像并将其分配回self.image
.
def resize_image(self, image):
#....
return pygame.transform.scale(image, (int(new_w),int(new_h)))
和
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
推荐阅读
- python - Pandas 安装失败并显示“OSError:[WinError8] 没有足够的存储空间来处理此命令”
- django - django 存储 AWS S3 SigVer4:SignatureDoesNotMatch
- java - 如何配置 FlatFileItemWriter 将文件输出到 ByteArrayRecource?
- mysql - 如何在mysql中插入具有下一个id的行
- azure - 如何在 azure 中托管?
- html - 悬停动画不流畅
- nix - 如何修复 NixOS 中的“坏消息”?
- javascript - Vuetify v-data-iterator 自定义“键:[]”名称
- python - 从另一个 Pandas 数据框中查找搜索词
- python - 如何阻止我的 If 语句向我提供错误信息?