In this tutorial we want to learn How to Add Image in Python Applications, when you want to add an image in Python, than you will need a third party library, for this we are going to use PyQt6 library, so PyQt6 is popular Python library for building GUI applications.
First of all you need to install PyQt6 library and you can use pip for the installation.
1 |
pip install PyQt6 |
After that we creates a class that extends from the QWidget class. This class will serve as our main application window, where we want to display the image.
1 2 3 4 5 6 7 8 9 |
class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Image Application") self.setGeometry(300, 300, 400, 300) self.label = QLabel(self) self.label.setGeometry(50, 50, 300, 200) |
In the MainWindow class, we override __init__ method to perform some initial setup. we set the window title and dimensions using setWindowTitle and setGeometry methods.
We also creates a QLabel widget called label and position it at coordinates (50, 50) with a width of 300 and height of 200 pixels. This label will display the image.
For loading and displaying the image inside the application, we need to convert the image file into a QPixmap object and set it as the pixmap for the QLabel widget.
1 2 3 4 |
image_path = "icon.png" pixmap = QPixmap(image_path) self.label.setPixmap(pixmap) |
In the above code we creates a QPixmap object called pixmap by passing the image file path to it. after that we set the pixmap as the image for the QLabel widget using the setPixmap method.
For runing the application, we need to create an instance of the QApplication class and pass the command line arguments to it. we also creates an instance of the MainWindow class and make it visible using the show method.
1 2 3 4 5 |
if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import sys from PyQt6.QtWidgets import QApplication, QLabel, QWidget from PyQt6.QtGui import QPixmap class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Image Application") self.setGeometry(300, 300, 400, 300) self.label = QLabel(self) self.label.setGeometry(50, 50, 500, 200) image_path = "icon.png" pixmap = QPixmap(image_path) self.label.setPixmap(pixmap) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
Run the complete code and this will be the result
FAQs:
How to insert an image in a Python program?
To insert an image into a Python program, you typically need to specify file path of the image inside your code. You can use libraries like PIL (Python Imaging Library), OpenCV or matplotlib to handle image processing tasks.
For example, using PIL:
1 2 3 4 5 6 7 8 9 |
from PIL import Image # Specify the file path of the image image_path = "path/to/your/image.jpg" # Load the image img = Image.open(image_path) # Now you can manipulate the image as needed inside your Python program |
How to display an image in Python?
To display an image in Python, you can use libraries like PIL, OpenCV or matplotlib. This is a basic approach using PIL and matplotlib:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from PIL import Image import matplotlib.pyplot as plt # Specify the file path of the image image_path = "python.png" # Load the image img = Image.open(image_path) # Display the image using matplotlib plt.imshow(img) plt.axis('off') # Turn off axes plt.show() |
This will be the result
Subscribe and Get Free Video Courses & Articles in your Email