How to Fill a Color in Python Using Matplotlib and PIL
Learn how to fill colors in Python images using matplotlib and PIL with simple code examples.
675 views
In Python, you can fill a color using libraries like matplotlib or PIL. For matplotlib, use: ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.add_patch(plt.Rectangle((0, 0), 1, 1, color='blue')) plt.show() ``` For PIL (Pillow), use: ```python from PIL import Image, ImageDraw image = Image.new('RGB', (100, 100), 'white') draw = ImageDraw.Draw(image) draw.rectangle([0, 0, 100, 100], fill='blue') image.show() ```
FAQs & Answers
- How do I fill a color using matplotlib in Python? Use matplotlib's patches, such as plt.Rectangle, to create a shape and set its color attribute, then display it with plt.show().
- Can I fill colors in images using the PIL library? Yes, PIL's ImageDraw module allows you to draw shapes like rectangles and fill them with specified colors on an image.
- What is the difference between matplotlib and PIL for color filling? Matplotlib is typically used for plotting and visualizations, whereas PIL (Pillow) is geared towards image processing and editing.