Python | Working with PNG Images using Matplotlib

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.

In this article, we will see how can we work with PNG images using Matplotlib.

Code #1: Read a PNG image using Matplotlib

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pyplot and image from matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as img
  
# reading png image file
im = img.imread('imR.png')
  
# show image
plt.imshow(im)

chevron_right


Output:

Code #2: Applying pseudocolor to image

Pseudocolor is useful for enhancing contrast of image.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pyplot and image from matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as img
  
# reading png image
im = img.imread('imR.png')
  
# applying pseudocolor 
# default value of colormap is used.
lum = im[:, :, 0]
  
# show image
plt.imshow(lum)

chevron_right


Output:

Code #3: We can provide another value to colormap with colorbar.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pyplot and image from matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as img
  
# reading png image
im = img.imread('imR.png')
lum = im[:, :, 0]
  
# setting colormap as hot
plt.imshow(lum, cmap ='hot')
plt.colorbar()

chevron_right


Output:

Interpolation Schemes:
Interpolation calculates what the color or value of a pixel “should” be and this needed when we resize the image but want the same information. There’s missing space when you resize image because pixels are discrete and interpolation is how you fill that space.

Code # 4: Interpolation

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing PIL and matplotlib
from PIL import Image 
import matplotlib.pyplot as plt
  
# reading png image  file
img = Image.open('imR.png')
  
# resizing the image
img.thumbnail((50, 50), Image.ANTIALIAS)
imgplot = plt.imshow(img)

chevron_right


Output:

Code #6: Here, ‘bicubic’ value is used for interpolation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pyplot from matplotlib
import matplotlib.pyplot as plt
  
# importing image from PIL
from PIL import Image 
  
# reading image
img = Image.open('imR.png')
  
img.thumbnail((30, 30), Image.ANTIALIAS) 
  
# bicubic used for interpolation
imgplot = plt.imshow(img, interpolation ='bicubic')

chevron_right


Output:

Code #7: ‘sinc’ value is used for interpolation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing PIL and matplotlib
from PIL import Image 
import matplotlib.pyplot as plt
  
# reading image
img = Image.open('imR.png')
  
img.thumbnail((30, 30), Image.ANTIALIAS)
  
# sinc used for interpolation
imgplot = plt.imshow(img, interpolation ='sinc')

chevron_right


Output:

 
Reference: https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.