The Wayback Machine - https://web.archive.org/web/20240820123815/https://www.geeksforgeeks.org/python-display-images-with-pygame/
Open In App

Python | Display images with PyGame

Last Updated : 07 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.

Command to install pygame :

pip install pygame

How to display images with PyGame ?

There are four basic steps to displaying images on the pygame window :

  • Create a display surface object using display.set_mode() method of pygame.
  • Create a Image surface object i.e. surface object in which image is drawn on it, using image.load() method of pygame.
  • Copy the image surface object to the display surface object using blit() method of pygame display surface object.
  • Show the display surface object on the pygame window using display.update() method of pygame.

Display images using PyGame

Here we are first importing the required library and then setting the width and height of the image then creating the display surface for that size then give the path of the required image in then image.load() function and then finally iterate over the list of event objects.

Python3




# importing required library
import pygame
 
# activate the pygame library .
pygame.init()
X = 600
Y = 600
 
# create the display surface object
# of specific dimension..e(X, Y).
scrn = pygame.display.set_mode((X, Y))
 
# set the pygame window name
pygame.display.set_caption('image')
 
# create a surface object, image is drawn on it.
imp = pygame.image.load("C:\\Users\\DELL\\Downloads\\gfg.png").convert()
 
# Using blit to copy content from one surface to other
scrn.blit(imp, (0, 0))
 
# paint screen one time
pygame.display.flip()
status = True
while (status):
 
  # iterate over the list of Event objects
  # that was returned by pygame.event.get() method.
    for i in pygame.event.get():
 
        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if i.type == pygame.QUIT:
            status = False
 
# deactivates the pygame library
pygame.quit()


Output:

 Display images with PyGame

 Display images with PyGame


Previous Article
Next Article

Similar Reads

Adding Collisions Using pygame.Rect.colliderect in Pygame
Prerequisite: Drawing shapes in Pygame, Introduction to pygame In this article, we are going to use pygame.Rect.colliderect for adding collision in a shape using Pygame in Python. We can easily add collisions in Pygame shapes using the colliderect( ). For this, we are going to draw two rectangles then we will check if the rectangles are colliding o
3 min read
Python | Display text to PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.. Command to install pygame on wind
6 min read
Snowfall display using Pygame in Python
Not everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming. Before starting the topic, it is highly recommended revising the basics of Pygame. Steps for snowfall creation 1. Importing modules First, we need to import the Pygame modu
3 min read
How to Rotate and Scale images using PyGame ?
In this article, we are going to see how to Rotate and Scale the image. Image Scaling refers to the resizing of the original image and Image Rotation refers to turning off an image with some angle. Rotations in the coordinate plane are counterclockwise. Let's proceed with the methods used and the complete code to perform these tasks. Scaling the Im
3 min read
How to create walking character using multiple images from sprite sheet using Pygame?
In this article, we will cover how to create a walking character using multiple images from a sprite sheet using Pygame. Any video or films that are made are actually the frames of pictures or images. The more the frame of images one has more will be the smoothness of that film or video. In sprite, we can develop any video or game with the use of i
5 min read
How to Display Multiple Images in One Window using OpenCV Python?
Prerequisites: Opencv In this article, we will show how to display Multiple Images In One window using OpenCV in Python. Approach Import moduleLoad the Multiple images using cv2.imread()Concatenate the images using concatenate(), with axis value provided as per orientation requirementDisplay all the images using cv2.imshow()Wait for keyboard button
1 min read
Display Images on Terminal using Python
In this article, we will discuss how to display images on a terminal using Python. We will be using the climage module for the same. This module has the following features - It helps to convert images to its ANSI Escape Codes to be able to convert be printable on Command-Line Interfaces.It allows 8/16/256 bit color codings for vivid images.It provi
3 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
How to Display Multiple Images in One Figure Correctly in Matplotlib?
The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt.figure() and then add an axes object to the fig by calling add_subplot() method. Then will display the image using imshow() method. Syntax
2 min read
How to Display Images within Tkinter Frames
Tkinter is a Python module used to create Graphical User Interface (GUI) applications. Tkinter has many widgets such as Button, Label, Text, Entry, Frame, Checkbox and more that make it easy and quick to develop desktop applications. How to Insert Image inside Frame in Tkinter?To insert an image inside a frame is the same as inserting an image in T
1 min read
Python | Drawing different shapes on PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip instal
3 min read
Python | Making an object jump in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. So, in this article, we will learn how
3 min read
Python | Playing audio file in Pygame
Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the best modules for doing so. Note: For more information, refer to Introdu
2 min read
Python - Moving an object in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this tutorial, we are going to lear
3 min read
Slide Puzzle using PyGame - Python
Slide Puzzle game is a 2-dimensional game i.e the pieces can only be removed inside the grid and reconfigured by sliding them into an empty spot. The slide puzzle game developed here is a 3X3 grid game i.e 9 cells will be there from 1 to 8(9 is not here since a blank cell is needed for sliding the neighboring cells). In this article, we are going t
15 min read
Python - Drawing design using arrow keys in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this article we will see how we can
3 min read
Binary Search Visualization using Pygame in Python
An algorithm like Binary Search can be understood easily by visualizing. In this article, a program that visualizes the Binary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sorting algorithm, and fill the pygame window with ba
4 min read
Ternary Search Visualization using Pygame in Python
An algorithm like Ternary Search can be understood easily by visualizing. In this article, a program that visualizes the Ternary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sorting algorithm, and fill the pygame window with
5 min read
Difference between Pygame VS Arcade Library in Python
Game programming is very rewarding nowadays and it can also be used in advertising or as a teaching tool. Game development encompasses mathematics, logic, physics, AI, and much more and it can be amazingly fun. In Python, up until now, Pygame library was employed for the same, but there is a new module: Arcade Library. Here we will discuss how they
3 min read
Building Space Invaders Using PyGame - Python
In this article, we're going to build a Space Bullet shooter game using PyGame in Python. The used file can be downloaded from here. Approach: Import the required module.Initialize the pygame.Create three functions:isCollision(): Which tells us whether the collision has occurred or not?game_over(): Which returns True or False on the basis of which
13 min read
How to create MS Paint clone with Python and PyGame?
In this article, we will create a simple MS paint program with Python and PyGame. MS Paint is a simple program made by Microsoft, it allows users to create basic art and painting. Since its inception, MS Paint has been included with every version of Microsoft Windows. MS Paint provides features for drawing and painting in color and tools to create
9 min read
Differences Between Pyglet and Pygame in Python
In this article, we will see the difference between Pygame and Pyglet gaming libraries. What is Pyglet? Pyglet is a cross-platform window and media library for Python, intended for developing games and other visually rich applications. It supports windows, UI event handling, gamepads, OpenGL graphics, image and video loading, and sound and music pl
4 min read
Brick Breaker Game In Python using Pygame
Brick Breaker is a 2D arcade video game developed in the 1990s. The game consists of a paddle/striker located at the bottom end of the screen, a ball, and many blocks above the striker. The basic theme of this game is to break the blocks with the ball using the striker. The score is calculated by the number of blocks broken and each time the player
14 min read
Create Settings Menu in Python - Pygame
Python is a flexible programming language with a large selection of libraries and modules for a variety of applications. Pygame menu is one such toolkit that enables programmers to design graphical user interfaces for games and apps. In this tutorial, we'll look at how to use the pygame menu package to build a simple options menu. Users will have c
9 min read
Build a Music Player with Tkinter and Pygame in Python
We all use music players like Windows Media Player or VLC to play music on our system. Have you ever wanted to create your own music player if your answer is yes then with Python, you can build a customized Music Player. In this article, you will see how to build a simple yet functional music player using two powerful Python libraries: Tkinter and
7 min read
Create a "Play Again" Option with Python Pygame
Adding a "Play Again" feature to your Snake game using Python's Pygame module involves several steps. Game Variables Reset: restart_game method resets the snake, food, direction, and game over flag.Game Over Display: show_game_over method displays a message and a button to restart the game.Event Binding: We bind the keyboard events to control the s
6 min read
Snake Game in Python - Using Pygame module
Snake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself. Creating a snake game can be taken as a challenge while learning Python or Pygame. It is one of the best beginner-friendly projects that every novice programmer s
15+ min read
Dodger Game using Pygame in Python
A Dodger game is a type of game in which a player has access to control an object or any character to dodge (to avoid) upcoming obstacles and earn points according to the number of items dodged if the player is not able to dodge the object the game will be over and final score will be displayed in the console. After certain points or times the spee
5 min read
How to create a text input box with Pygame?
In this article, we will discuss how to create a text input box using PyGame. Installation Before initializing pygame library we need to install it. This library can be installed into the system by using pip tool that is provided by Python for its library installation. Pygame can be installed by writing these lines into the terminal. We can install
3 min read
How to add moving platforms in PyGame
Prerequisite: Drawing in Pygame In this article, we will learn how we can add moving platforms to our game using PyGame in Python. Creating a Platform We can easily create any type of platform in pygame using the draw( ) method. For this, we will create react with specific width and height using the draw.rect() function. Syntax: pygame.draw.rect(su
5 min read
three90RightbarBannerImg