如何在Pygame中更改分辨率后保存图像?

huangapple go评论106阅读模式
英文:

How to save an image after changing its resolution in Pygame?

问题

目前,我是初学者,对于Pygame还不太熟悉,一直在尝试让用户手绘数字到画布上。我已经成功实现了在画布上绘制和清除的功能。然而,我想要保存的图片必须是28x28像素大小的。我的想法是将用户在画布上绘制的内容保存为PNG文件,然后加载它,使用smoothscale方法缩小成28x28像素,将其blit()到另一个28x28像素大小的表面上,然后再从那里保存它。

以下是我的代码:

import pygame
screen = pygame.display.set_mode((600, 600))
canvas = pygame.Surface((555, 405))
new_res_canvas = pygame.Surface((28, 28))

clock = pygame.time.Clock()
black = [0, 0, 0]
white = [255, 255, 255]
rect_size = 30
out = False
pos_draw_list = []

def predict_screen():
    pygame.image.save(canvas, 'digit_large.png')  # 保存绘制内容的画布图像
    predict_img = pygame.image.load('digit_large.png')  # 加载图像
    predict_img = pygame.transform.smoothscale(predict_img.convert_alpha(), (28, 28))  # 缩放为28x28像素
    new_res_canvas.blit(predict_img, (0, 0))  # 将其绘制到新的28x28表面上
    pygame.image.save(new_res_canvas, 'digit.png')  # 保存它

当我检查我的文件时,我发现用户绘制的图像 ('digit_large.png') 正确并且工作正常。然而,较小尺寸的图像完全为空白(全黑)。相反,它应该具有黑色背景并包含用户绘制的较小尺寸版本。作为参考,用户绘制的画布是黑色的,而用户绘制的是30x30的白色矩形。

英文:

Currently, I am a beginner when it comes to Pygame, and have been trying to allow the user to hand-draw digits into a canvas. I have managed to get the code to draw and clear the canvas to work. However, the pictures I want to save must be 28 by 28 pixels big. My idea was to save what the user drew on the canvas surface as a PNG file, load it back in, smoothscale that down into 28 by 28 pixels, .blit() that into another surface that is 28 by 28 pixels big, then save it from there.

Here is my code:

import pygame
screen = pygame.display.set_mode((600, 600))
canvas = pygame.Surface((555, 405))
new_res_canvas = pygame.Surface((28, 28))

clock = pygame.time.Clock()
black = [0, 0, 0]
white = [255, 255, 255]
rect_size = 30
out = False
pos_draw_list = []


def predict_screen():
    pygame.image.save(canvas, 'digit_large.png') #img of the canvas with drawing
    predict_img = pygame.image.load('digit_large.png') #load the img
    predict_img = pygame.transform.smoothscale(predict_img.convert_alpha(), (28, 28)) #change it to 28 by 28
    predict_img.blit(new_res_canvas, (28, 28)) #put it onto a surface to save it
    pygame.image.save(new_res_canvas, 'digit.png') #save it

When I check my files, I find that the picture of the user's drawing ('digit_large.png') is correct and working fine. However, the smaller scale picture is completely empty (black). Instead, it should have a black background with a smaller scale version of what the user has drawn. For reference, the canvas the user is drawing on is black, while the user draws 30 by 30 white rectangles.

答案1

得分: 1

You have to blit the image at the top right instead of (28, 28). Note, the size of predict_img is just 28x28, so blit at (28, 28) means to blit it completely out of bounds. Also you have to blit predict_img on new_res_canvas, but not the other way around:

<s>predict_img.blit(new_res_canvas, (28, 28))</s>

new_res_canvas.blit(predict_img, (0, 0))

Actually, you don't need to blit at all if you use the dest_surface argument of pygame.transform.smoothscale():

pygame.transform.smoothscale(
    predict_img.convert_alpha(), (28, 28), dest_surface = new_res_canvas) 
英文:

You have to blit the image at the top right instead of (28, 28). Note, the size of predict_img is just 28x28, so blit at (28, 28) means to blit it completely out of bounds. Also you have to blit predict_img on new_res_canvas, but not the other way around:

<s>predict_img.blit(new_res_canvas, (28, 28))</s>

new_res_canvas.blit(predict_img, (0, 0))

Actually, you don't need to blit at all if you use the dest_surface argument of pygame.transform.smoothscale():

pygame.transform.smoothscale(
    predict_img.convert_alpha(), (28, 28), dest_surface = new_res_canvas) 

huangapple
  • 本文由 发表于 2023年3月7日 02:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定