How to Convert PIL Image into pygame surface image?
In this article, we will know How to Convert PIL Image into pygame surface image.
Pygame Surface Image: A surface that not only has fixed resolution and pixel format but also represents the image in Pygame is known as Pygame Surface Image.
Are you constructing any game in Pygame in which you want to use only Pygame Surface Image as an input? As most of the images entered by the user will be in the PIL Image format, hence you need to convert the PIL images into Pygame surface images before using it. Don’t you know how to convert it into Pygame Surface Image? Don’t worry, it’s too easy to do so. What you need to do is just to use the fromstring() function after taking the PIL image as an input from the user. Read the article given below to know more in detail.
Step 1: First, import the libraries Image and Pygame.
import Image import pygame from pygame.locals import *
Step 2: Now, take the colors as input that you want to use in the game.
color_1 = #RGB value of color 1 color_2 = #RGB value of color 2 color_n = #RGB value of color n
Step 3: Then, construct the GUI game.
Step 4: Further, set the dimensions of your GUI game.
w, h = #width of game, #height of game screen = pygame.display.set_mode((w, h))
Step 5: Next, take the PIL image as input which you wish to convert into Pygame Surface Image.
image = Image.open("#Enter the image")
Step 6: Calculate the values of mode which will be used while converting PIL image to pygame surface image.
mode = image.mode
Step 7: Further, calculate the value of size which will be used while converting PIL image to pygame surface image.
size = image.size
Step 8: Calculate the value of data that will be used while converting PIL image to pygame surface image.
data = image.tobytes()
Step 9: Moreover, convert the PIL Image to Pygame Surface Image.
py_image = pygame.image.fromstring(data, size, mode)
Step 10: Later on, construct the rectangle around the converted image.
rect = py_image.get_rect() rect.center = w//2, h//2
Step 11: Set the running value for running the game.
Step 12: Set the things which you want your game to do when it is in running state.
while running: for event in pygame.event.get():
Step 12.1: Once the app is in a running state, make it quit if the user wants to quit.
if event.type == pygame.QUIT: running = False
Step 12.2: Moreover, set the background color which you wish to see in your app.
screen.fill(color_1) screen.blit(py_image, rect)
Step 12.3: Further, construct the border around the image.
pygame.draw.rect(screen, color_2, rect, 2)
Step 12.4: Then, make your app do whatever you want it do while being in a running state.
Step 12.5: After doing everything you wish to do, update the changes done.
Step 13: Finally, quit the GUI game
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course