Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Display an Image as Grayscale in Python Matplotlib?

#1
How to Display an Image as Grayscale in Python Matplotlib?

How to Display an Image as Grayscale in Python Matplotlib?


You can convert a given image to a grayscale image using four simple steps:

  1. Import the PIL and Matplotlib libraries
  2. Open the image with PIL.Image.open(filename).
  3. Convert the opened image to grayscale using img.convert("L") with greyscale mode “L”.
  4. Display the image using Matplotlib’s plt.imshow(gray_img, cmap='gray') function.

Here’s the minimal code to convert any given .jpg image to a grayscale image:

import PIL
import matplotlib.pyplot as plt img = PIL.Image.open("pic.jpg")
gray_img = img.convert("L")
plt.imshow(gray_img, cmap='gray')

Now, you may wonder:

How to Save a Grayscale Image with Matplotlib?


? To save the grayscale image generated by Matplotlib’s plt.imshow(), add another line plt.savefig("gray.jpg"). This takes the shown grayscale image and saves it in the file "gray.jpg".

Here’s the code with the additional line highlighted:

import PIL
import matplotlib.pyplot as plt img = PIL.Image.open("pic.jpg")
gray_img = img.convert("L")
plt.imshow(gray_img, cmap='gray')
plt.savefig("gray_pic.jpg")

You may wonder where Matplotlib’s plt.savefig() function takes the image to be saved—you only pass the outfile path name as a string but no explicit image to be saved! The reason is that Matplotlib’s plt object works like a state machine. If you call plt.savefig(path) it takes the lastly generated figure and saves it at the given path.

Try It in Our Interactive Jupyter Notebook


If you’re like me, you want to try things quickly. To help you do this, I’ve set up an interactive Jupyter notebook so that you can execute this in your own browser:

How to Display an Image as Grayscale in Python Matplotlib?<br />
Interactive Code Example

Just click on the code to try it in a new tab.


If you really want to dive into Matplotlib, check out our in-depth academy course here:

It’s one of the most in-depth Matplotlib courses in the web—and it’ll boost your data science and data visualization skills to a level that’ll impress your colleagues. Check it out, it’s very inexpensive! ?

The post How to Display an Image as Grayscale in Python Matplotlib? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/04/...atplotlib/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Crop an Image Using PIL? xSicKxBot 0 36 03-27-2021, 01:27 PM
Last Post: xSicKxBot
  [Tut] Python OpenCV Image Processing – Resize, Blend, Blur, Threshold, Convert xSicKxBot 0 46 03-23-2021, 12:13 PM
Last Post: xSicKxBot
  [Tut] What’s the Best Way to Save Image Metadata Alongside a TIFF? xSicKxBot 0 48 03-10-2021, 11:07 PM
Last Post: xSicKxBot
  [Tut] How to Customize Multiple Subplots in Matplotlib xSicKxBot 0 68 03-02-2021, 07:29 AM
Last Post: xSicKxBot
  [Tut] Best Matplotlib Cheat Sheet xSicKxBot 0 82 01-30-2021, 09:08 AM
Last Post: xSicKxBot
  [Tut] Matplotlib Cursor — How to Add a Cursor and Annotate Your Plot xSicKxBot 0 73 01-24-2021, 08:26 AM
Last Post: xSicKxBot
  [Tut] Python complex() — A Useless Python Feature? xSicKxBot 0 96 12-19-2020, 02:05 AM
Last Post: xSicKxBot
  [Tut] Matplotlib Scatter Plot – Simple Illustrated Guide xSicKxBot 0 95 11-25-2020, 01:23 AM
Last Post: xSicKxBot
  [Tut] Hello World! A Python One-Liner to Get Started with Python Quickly xSicKxBot 0 138 09-07-2020, 06:10 PM
Last Post: xSicKxBot
  [Tut] How To Display The Latest Python News On Your Webpage? xSicKxBot 0 112 08-27-2020, 05:32 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
https://www.fettleride.com/lycium-serum-...
Last Post: joiranshom
Today 06:18 AM
» Replies: 0
» Views: 2
News - WWE 2K22 Will Get Regular Dev Vid...
Last Post: xSicKxBot
Today 06:13 AM
» Replies: 0
» Views: 2
(Indie Deal) Warhammer 40k: Gladius & Br...
Last Post: xSicKxBot
Today 04:44 AM
» Replies: 0
» Views: 5
(Free Game Key) The Dream Machine: Chapt...
Last Post: xSicKxBot
Today 04:44 AM
» Replies: 0
» Views: 4
Mobile - Demon Tower Defense codes – fre...
Last Post: xSicKxBot
Yesterday 11:43 PM
» Replies: 0
» Views: 6
News - Trials Of Mana 25th Anniversary C...
Last Post: xSicKxBot
Yesterday 11:43 PM
» Replies: 0
» Views: 5
News - This Samsung Monitor Can Do It Al...
Last Post: xSicKxBot
Yesterday 11:43 PM
» Replies: 0
» Views: 32
Xbox Wire - Boris the Rocket is Now Avai...
Last Post: xSicKxBot
Yesterday 06:45 PM
» Replies: 0
» Views: 5
News - Turkish mobile studio Ace Games r...
Last Post: xSicKxBot
Yesterday 06:45 PM
» Replies: 0
» Views: 33
News - Blog: Making a meaningful scent s...
Last Post: xSicKxBot
Yesterday 06:44 PM
» Replies: 0
» Views: 48

[-]
Twitter

Copyright © SickGaming.net 2012-2020