Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Display an Image in Grayscale in Matplotlib?
Next article icon
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 MatplotlibPython3
# importing pyplot and image from matplotlibimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading png image fileim=img.imread('imR.png')# show imageplt.imshow(im)
Output:Code #2: Applying pseudocolor to image Pseudocolor is useful for enhancing contrast of image.Python3
# importing pyplot and image from matplotlibimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading png imageim=img.imread('imR.png')# applying pseudocolor# default value of colormap is used.lum=im[:,:,0]# show imageplt.imshow(lum)
Output:Code #3: We can provide another value to colormap with colorbar.Python3
# importing pyplot and image from matplotlibimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading png imageim=img.imread('imR.png')lum=im[:,:,0]# setting colormap as hotplt.imshow(lum,cmap='hot')plt.colorbar()
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: InterpolationPython3 1==
# importing PIL and matplotlibfromPILimportImageimportmatplotlib.pyplotasplt# reading png image  fileimg=Image.open('imR.png')# resizing the imageimg.thumbnail((50,50),Image.ANTIALIAS)imgplot=plt.imshow(img)
Output:Code #6:Here, 'bicubic' value is used for interpolation.Python3 1==
# importing pyplot from matplotlibimportmatplotlib.pyplotasplt# importing image from PILfromPILimportImage# reading imageimg=Image.open('imR.png')img.thumbnail((30,30),Image.ANTIALIAS)# bicubic used for interpolationimgplot=plt.imshow(img,interpolation='bicubic')
Output:Code #7:'sinc' value is used for interpolation.Python3 1==
# importing PIL and matplotlibfromPILimportImageimportmatplotlib.pyplotasplt# reading imageimg=Image.open('imR.png')img.thumbnail((30,30),Image.ANTIALIAS)# sinc used for interpolationimgplot=plt.imshow(img,interpolation='sinc')
Output:  Reference:
https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp