Skip to main content

mb.log_image(image)

Log a matplotlib Figure, PIL Image, or png/jpg image to Modelbit. The image will show in the deployment's logs viewer.

Parameters

  • image: [matplotlib.pyplot.Figure | PIL.Image | str] This argument can be a matplotlib Figure, a PIL Image, or a string filepath to a local .png or .jpg image.

Returns

None

Outside of a Modelbit deployment, calling log_image prints a success message if it's able to process the supplied image, or raises an exception if it cannot.

Example

Logging a matplotlib Figure

import matplotlib.pyplot as plt
from random import random

def example_plot():
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [random(), random(), random(), random()])
mb.log_image(fig)
return { "prediction": 1 }

Logging a PIL Image

from PIL import Image

def example_plot():
img = Image.open("my_image.jpg")
mb.log_image(img)
return { "prediction": 1 }

Logging an image file

def example_plot():
mb.log_image("my_image.jpg")
return { "prediction": 1 }

Logging multiple images

To log multiple images, call log_image multiple times.

import matplotlib.pyplot as plt

def example_plot():
fig, ax = plt.subplots()

print("Before:")
ax.plot([1, 2, 3, 4], [random(), random(), random(), random()])
mb.log_image(fig)

print("After:")
ax.plot([1, 2, 3, 4], [random(), random(), random(), random()])
mb.log_image(fig)

return { "prediction": 1 }