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.
Approach:
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.
pygame.init()
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.
running=True
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.
pygame.display.update()
Step 13: Finally, quit the GUI game
pygame.quit()
Implementation:
Python
import Image
import pygame
from pygame. locals import *
YELLOW = ( 255 , 255 , 0 )
BLUE = ( 0 , 0 , 255 )
pygame.init()
w, h = 640 , 350
screen = pygame.display.set_mode((w, h))
image = Image. open ( "gfg_image.jpg" )
mode = image.mode
size = image.size
data = image.tobytes()
py_image = pygame.image.fromstring(data, size, mode)
rect = py_image.get_rect()
rect.center = w / / 2 , h / / 2
running = True
while running:
for event in pygame.event.get():
if event. type = = QUIT:
running = False
screen.fill(YELLOW)
screen.blit(py_image, rect)
pygame.draw.rect(screen, BLUE, rect, 2 )
pygame.display.update()
pygame.quit()
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!