📜  python show png - Python (1)

📅  最后修改于: 2023-12-03 15:34:04.404000             🧑  作者: Mango

Python Show PNG

Python is a powerful programming language widely used in various domains. It has many libraries that allow easy creation and manipulation of different file formats, including images. In this article, we will explore how to show a PNG image in Python using some of the popular libraries.

Required Libraries
  • Pillow
  • Matplotlib
Show PNG Image Using Pillow

Pillow is a popular Python library for image processing. It allows us to open, create, and manipulate various image formats. To show a PNG image using the Pillow library, we need to follow these steps:

  1. Import the necessary modules:
from PIL import Image
from IPython.display import display
  1. Load the image using the Image module:
img = Image.open('sample.png')
  1. Show the loaded image using the display module:
display(img)
Show PNG Image Using Matplotlib

Matplotlib is a popular Python library for creating high-quality scientific visualizations. It has a submodule called pyplot, which provides a user-friendly interface to create various charts and plots. To show a PNG image using Matplotlib, we need to follow these steps:

  1. Import the necessary modules:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
  1. Load the image using the imread function:
img = mpimg.imread('sample.png')
  1. Show the loaded image using the imshow function:
plt.imshow(img)
plt.show()
Conclusion

In this article, we have explored how to show a PNG image in Python using the Pillow and Matplotlib libraries. It is essential to have prior knowledge about these libraries to efficiently use them for image processing tasks.