Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Working with PNG Images using 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.

Working with Images in Python using Matplotlib

Theimage module inmatplotlib library is used for working with images in Python. Theimage module also includes two useful methods which areimread which is used to read images andimshow which is used to display the image.Below are some examples which illustrate various operations on images usingmatplotlib library:Example 1: In this example, the program reads an image using thematplotlib.image.imread() and displays that image usingmatplotlib.image.imread().Python3
# importing required librariesimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading the imagetestImage=img.imread('g4g.png')# displaying the imageplt.imshow(testImage)
Output:Example 2: The below program reads an image and then represents the image in array.Python3
# importing required librariesimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading the imagetestImage=img.imread('g4g.png')# displaying the image as an arrayprint(testImage)
Output:
[[[0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]] [[0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]] [[0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]] ... [[0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]] [[0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]] [[0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  [0.03529412 0.52156866 0.28235295]  ...  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]  [0.05490196 0.6156863  0.34117648]]]
Example 3: Here, the shape of the image is(225, 225, 3) which represents (height, width, mode) of the image, for colored image mode value is from 0 to 2 and for black and white image mode value is 0 and 1 only. In the output image, only the mode of the image is modified.Python3
# importing required librariesimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading the imagetestImage=img.imread('g4g.png')# displaying the shape of the imageprint(testImage.shape)# modifying the shape of the imagemodifiedImage=testImage[:,:,0]# displaying the modified imageplt.imshow(modifiedImage)
Output:
(225, 225, 3)
Example 4: In the below program, all the parameters of the shape of the image are modified. Here the height of the image is 150 pixels (displaying from the 50th pixel), width is 100 pixels (displaying from the 100th pixel) and mode value is 1.Python3
# importing required librariesimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading the imagetestImage=img.imread('g4g.png')# displaying the shape of the imageprint(testImage.shape)# modifying the shape of the imagemodifiedImage=testImage[50:200,100:200,1]# displaying the modified imageplt.imshow(modifiedImage)
Output:
(225, 225, 3)
Example 5: Here, none of the parameters are modified. So, the original image is displayed.Python3
# importing required librariesimportmatplotlib.pyplotaspltimportmatplotlib.imageasimg# reading the imagetestImage=img.imread('g4g.png')# displaying the shape of the imageprint(testImage.shape)# modifying the shape of the imagemodifiedImage=testImage[:,:,:]# displaying the modified imageplt.imshow(modifiedImage)
Output:
(225, 225, 3)

Improve
Practice Tags :

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