API Reference#
Top-level functions#
- memeplotlib.meme(template, *lines, font=<object object>, color=<object object>, outline_color=<object object>, outline_width=<object object>, fontsize=<object object>, style=<object object>, backend=None, extension=None, width=None, height=None, layout=None, background=None, overlays=None, template_style=None, show=False, savefig=None, figsize=None, dpi=None, ax=None, **text_kwargs)[source]#
Create a meme from a template with text lines.
The default
backend="auto"selects the memegen rendering API for memegen-catalogue templates, and falls back to a Pillow client-side renderer for custom local images or whenever the user supplies a feature memegen can’t express (per-linefontsize, custom outline,Axes.textkwargs).- Parameters:
- template
str Template identifier – memegen ID, file path, or URL.
- *lines
str Text lines for each text position (top, bottom, etc.).
- font
str,optional Font family name (default
config["font"]).- color
str,optional Text fill color (default
config["color"]).- outline_color
str,optional Text outline color. Passing a non-default value under
backend="auto"forces the Pillow backend, since memegen renders a hard-coded black stroke.- outline_width
float,optional Outline stroke width. Passing a non-default value under
backend="auto"forces the Pillow backend.- fontsize
float,optional Explicit font size in points. Forces the Pillow backend under
backend="auto"(memegen always auto-fits).- style
str,optional Text transform –
"upper","lower", or"none".- backend
str,optional "auto"(default),"memegen","pillow", or"matplotlib"(legacy, draws captions withmatplotlib.axes.Axes.text()).- extension
str,optional Output format requested from memegen –
"png","jpg","gif", or"webp".- width, height
int,optional Output dimensions for memegen-rendered images.
- layout
str,optional memegen layout (e.g.
"top").- background
str,optional Custom background image URL for memegen.
- overlayssequence
ofdict,optional Ad-hoc overlay placements forwarded to memegen as repeated
style=/center=/scale=query params.- template_style
str,optional memegen template-specific style (e.g.
"maga"for"ds").- showbool,
optional Whether to call
matplotlib.pyplot.show()after rendering.- savefig
str,Path,orNone,optional Path to save the meme image to.
- figsize
tupleof(float,float)orNone,optional Figure size as
(width, height)in inches.- dpi
intorNone,optional Dots per inch.
- ax
AxesorNone,optional Existing matplotlib Axes to render onto.
- **text_kwargs
Additional keyword arguments forwarded to
Axes.text()under the"matplotlib"and"pillow"backends. Passing anytext_kwargsunderbackend="auto"forces the Pillow backend.
- template
- Returns:
- fig
matplotlib.figure.Figure The matplotlib Figure containing the rendered meme.
- ax
matplotlib.axes.Axes The matplotlib Axes containing the rendered meme.
- fig
Examples
>>> import memeplotlib as memes >>> fig, ax = memes.meme("buzz", "memes", "memes everywhere")
>>> fig, ax = memes.meme( ... "drake", "writing tests", "shipping to prod", ... font="impact", color="yellow", ... )
>>> fig, ax = memes.meme( ... "buzz", "hello", "world", ... fontsize=48, # forces pillow backend under auto ... )
- memeplotlib.memify(fig, *lines, position='top-bottom', font=None, color=None, outline_color=None, outline_width=None, fontsize=None, style=None, show=False, savefig=None, **text_kwargs)[source]#
Add meme-style text to an existing matplotlib figure.
Overlays bold, outlined text on top of any matplotlib figure – useful for turning plots, charts, or other visualizations into memes.
Note
memifyalways renders text with matplotlib’sAxes.text(the figure isn’t a memegen template, so the API doesn’t apply). Thebackendparameter onmeme()is intentionally not exposed here.- Parameters:
- fig
matplotlib.figure.Figure The matplotlib figure to add text to.
- *lines
str Text lines to overlay.
- position
str,optional Layout –
"top-bottom"(default),"top","bottom", or"center".- font
strorNone,optional Font family name.
- color
strorNone,optional Text fill color.
- outline_color
strorNone,optional Text outline color.
- outline_width
floatorNone,optional Outline stroke width.
- fontsize
floatorNone,optional Font size in points (auto if
None).- style
strorNone,optional Text transform –
"upper","lower", or"none".- showbool,
optional Whether to call
matplotlib.pyplot.show()after rendering (default:False).- savefig
str,Path,orNone,optional Path to save the result to.
- **text_kwargs
Additional keyword arguments forwarded to
Axes.text()for each rendered caption.
- fig
- Returns:
matplotlib.figure.FigureThe modified Figure.
Examples
>>> import matplotlib.pyplot as plt >>> import memeplotlib as memes >>> fig, ax = plt.subplots() >>> ax.plot([1, 2, 3], [1, 4, 9]) >>> memes.memify(fig, "stonks")
- memeplotlib.rc_context(rc=None)[source]#
Temporarily override config keys, restoring originals on exit.
Mirrors
matplotlib.rc_context(). Useful for scoped styling that should not leak to other code.- Parameters:
- rc
MappingorNone,optional Mapping of key-value pairs to apply within the context. If
None, the active config is yielded unchanged but still snapshotted for restoration on exit.
- rc
- Yields:
MemeplotlibConfigThe active config singleton.
Examples
>>> from memeplotlib import config, rc_context >>> config["font"] = "impact" >>> with rc_context({"font": "comic", "color": "yellow"}): ... config["font"] 'comic' >>> config["font"] 'impact'
- memeplotlib.build_memegen_url(template_id, lines, *, api_base, extension='png', template_style=None, font=None, color=None, width=None, height=None, layout=None, background=None, overlays=None)[source]#
Construct a memegen rendering URL.
- Parameters:
- template_id
str memegen template identifier (e.g.
"buzz","drake").- linessequence
ofstr Caption lines in slot order. Empty strings become
_to preserve the slot.- api_base
str Base URL of the memegen API (e.g.
"https://api.memegen.link").- extension
str,optional Output format. One of
"png","jpg","jpeg","gif","webp". Default"png".- template_style
strorNone,optional Template-specific style (e.g.
"maga"). May also be an arbitrary image URL for ad-hoc overlays.- font
strorNone,optional memegen font alias (see
MEMEGEN_FONT_ALIASES).- color
strorNone,optional "fg"or"fg,bg"colour spec (HTML name or hex).- width
intorNone,optional Output width in pixels.
- height
intorNone,optional Output height in pixels.
- layout
strorNone,optional Alternate layout (e.g.
"top").- background
strorNone,optional Custom background image URL.
- overlayssequence
ofOverlaySpecorNone,optional Ad-hoc overlay placements.
- template_id
- Returns:
strFully-formed memegen rendering URL.
- Raises:
ValueErrorIf extension is unsupported.
Examples
>>> build_memegen_url( ... "buzz", ["memes", "memes everywhere"], ... api_base="https://api.memegen.link", ... ) 'https://api.memegen.link/images/buzz/memes/memes_everywhere.png'
>>> build_memegen_url( ... "ds", ["a", "b"], ... api_base="https://api.memegen.link", ... template_style="maga", ... font="comic", ... color="white,black", ... width=600, ... ) 'https://api.memegen.link/images/ds/a/b.png?style=maga&font=comic&...'
Classes#
- class memeplotlib.Meme(template, *lines, font=None, color=None, outline_color=None, outline_width=None, fontsize=None, style=None, backend=None, extension=None, width=None, height=None, layout=None, background=None, overlays=None, template_style=None)[source]#
Bases:
objectA meme builder with a fluent (chainable) API.
- Parameters:
- template
strorTemplate Template identifier (memegen ID, file path, URL) or a
Templateinstance.- *lines
str Initial text lines.
- font
strorNone,optional Font family name.
- color
strorNone,optional Text fill color.
- outline_color
strorNone,optional Text outline color.
- outline_width
floatorNone,optional Outline stroke width.
- fontsize
floatorNone,optional Font size in points.
- style
strorNone,optional Text transform –
"upper","lower", or"none".- backend
strorNone,optional Override the rendering backend (
"auto","memegen","pillow","matplotlib").Noneusesconfig["backend"].
- template
Examples
>>> from memeplotlib import Meme >>> Meme("buzz").top("memes").bottom("memes everywhere").show()
>>> m = Meme("drake") >>> m.top("writing tests") >>> m.bottom("shipping to prod") >>> fig, ax = m.render() >>> m.save("output.png")
>>> # Per-line override forces the Pillow backend. >>> Meme("buzz").top("hi").line(1, "world", fontsize=48).save("out.png")
- line(index, text, *, fontsize=None, color=None, font=None, position=None)[source]#
Set text and per-line styling overrides for a single slot.
Passing any of
fontsize,color,font, orpositionforces the Pillow backend on render (memegen has no equivalent).- Parameters:
- index
int Zero-based line index.
- text
str The text to place at the given slot.
- fontsize
float,optional Override font size for this slot.
- color
str,optional Override fill colour for this slot.
- font
str,optional Override font family for this slot.
- position
TextPosition,optional Override the text-box position metadata for this slot.
- index
- Returns:
MemeSelf, for method chaining.
- class memeplotlib.MemeplotlibConfig[source]#
Bases:
MutableMapping[str,Any]RcParams-style validated mapping of memeplotlib defaults.
Backed by an internal dictionary. Setting an unknown key raises
KeyError. Setting a known key with an invalid value raisesValueError. Userc_context()for scoped overrides.Notes
Only the keys defined in
MemeplotlibConfig.VALID_KEYSare accepted. The set of keys is fixed at class definition time.Examples
>>> from memeplotlib import config >>> config["font"] = "comic" >>> config["dpi"] 150
- class memeplotlib.Template(id, name, image_url, text_positions=<factory>, keywords=<factory>, example=<factory>, lines_count=2, overlays_count=0, styles=<factory>, is_memegen=False)[source]#
Bases:
objectA meme template with a background image and text position metadata.
- Parameters:
- id
str Template identifier (e.g.,
"buzz","drake").- name
str Human-readable display name.
- image_url
str URL or local file path to the background image.
- text_positions
listofTextPosition,optional Regions where text lines are rendered. Defaults to a classic top/bottom layout.
- keywords
listofstr,optional Search keywords associated with the template.
- example
listofstr,optional Example text lines for the template.
- lines_count
int,optional Maximum number of caption slots the template supports (mirrors the memegen
linesfield). Defaults to2.- overlays_count
int,optional Number of template-defined overlay slots (mirrors the memegen
overlaysfield). Defaults to0.- styles
listofstr,optional Template-specific style names (mirrors the memegen
stylesfield). Defaults to[].- is_memegenbool,
optional Trueif this template was constructed from memegen API metadata;Falsefor custom local files / arbitrary URLs. Used by the rendering dispatcher to decide whether the memegen backend is even reachable.
- id
- classmethod from_memegen(template_id, api_base=None)[source]#
Fetch template metadata and blank image from the memegen API.
- Parameters:
- Returns:
TemplateA Template instance with metadata populated from the API.
- Raises:
TemplateNotFoundErrorIf the template ID doesn’t exist in the memegen API.
Examples
>>> t = Template.from_memegen("buzz") >>> t.name 'Buzz Lightyear'
- classmethod from_image(path_or_url, lines=2, name='')[source]#
Create a template from a local image file or URL.
- Parameters:
- Returns:
TemplateA Template instance backed by the given image.
Examples
>>> t = Template.from_image("photo.jpg") >>> t = Template.from_image("https://example.com/img.png", lines=3)
- get_image(cache=None)[source]#
Return the template background image as a NumPy RGBA array.
Downloads from the image URL if not already loaded or cached.
- Parameters:
- cache
TemplateCacheorNone,optional Cache instance for storing/retrieving downloaded images.
- cache
- Returns:
numpy.ndarrayRGBA image array with shape
(height, width, 4).
- class memeplotlib.TemplateRegistry(api_base=None, cache=None)[source]#
Bases:
objectRegistry that discovers and caches templates from the memegen API.
- Parameters:
Examples
>>> reg = TemplateRegistry() >>> results = reg.search("dog") >>> all_templates = reg.list_all()
Singletons and exceptions#
- memeplotlib.config = MemeplotlibConfig()#
RcParams-style validated mapping of memeplotlib defaults.
Backed by an internal dictionary. Setting an unknown key raises
KeyError. Setting a known key with an invalid value raisesValueError. Userc_context()for scoped overrides.Notes
Only the keys defined in
MemeplotlibConfig.VALID_KEYSare accepted. The set of keys is fixed at class definition time.Examples
>>> from memeplotlib import config >>> config["font"] = "comic" >>> config["dpi"] 150