Skip to content
Snippets Groups Projects
Commit 87de8cb3 authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

First implementation of fallback/image engine.

parent b5762ebf
Branches
Tags
2 merge requests!19merge dev for new release v.0.1.2,!17Fallback engine implementation
## Implement a new plot engine
If you want to add another plot engine "$engine" to plotID, this document helps you to do it.
Create a new module named "tagplot_$engine.py". Paste the following code and replace every "$engine" with the name of your engine:
"""
Tag your picture with an ID.
Functions:
tagplot_$engine(PlotOptions instance) -> list
"""
import $engine
from plotid.create_id import create_id
from plotid.plotoptions import PlotOptions
def tagplot_$engine(plotid_object):
"""
Add IDs to plots with $engine.
The ID is placed visual on the figure window and returned as string in a
list together with the figures.
TagPlot can tag multiple figures at once.
"""
# Check if plotid_object is a valid instance of PlotOptions
if not isinstance(plotid_object, PlotOptions):
raise TypeError('The given options container is not an instance'
'of PlotOptions.')
ids_as_list = []
for fig in plotid_object.figs:
figure_id = create_id(plotid_object.id_method)
ids_as_list.append(figure_id)
"""
Insert here the tagging with $engine:
Open the figure fig.
Place the string figure_id on it.
Use plotid_object.position and plotid_object.rotation for position and rotation.
Save the tagged figure to plotid_object.figs.
"""
return [plotid_object.figs, ids_as_list]
Last step:
Add the following code in tagplot.py:
match engine:
[...]
case '$engine':
return tagplot_$engine(option_container)
case _:
[...]
\ No newline at end of file
......@@ -18,7 +18,7 @@ from plotid.publish import publish
PROJECT_ID = "MR04_"
# %% Choose Plot engine
PLOT_ENGINE = "matplotlib"
PLOT_ENGINE = "image"
# %% Create sample data
x = np.linspace(0, 10, 100)
......@@ -28,23 +28,28 @@ y_2 = np.sin(x) + 2
# %% Create figures
# 1. figure
img1 = 'image1.png'
FIG1 = plt.figure()
plt.plot(x, y, color='black')
plt.plot(x, y_2, color='yellow')
plt.savefig(img1)
# 2. figure
img2 = 'image2.png'
FIG2 = plt.figure()
plt.plot(x, y, color='blue')
plt.plot(x, y_2, color='red')
plt.savefig(img2)
# %% TagPlot
# If multiple figures should be tagged, figures must be provided as list.
FIGS_AS_LIST = [FIG1, FIG2]
IMGS_AS_LIST = [img1, img2]
[TAGGED_FIGS, ID] = tagplot(FIGS_AS_LIST, PLOT_ENGINE, prefix=PROJECT_ID,
id_method='random', location='west')
[TAGGED_FIGS, ID] = tagplot(IMGS_AS_LIST, PLOT_ENGINE, prefix=PROJECT_ID,
id_method='random', location='north')
print(type(TAGGED_FIGS[0]))
# %% Save figure as tiff-file, but publish also exports the plot to a picture
# file in the destination folder.
# for i, figure in enumerate(TAGGED_FIGS):
......
......@@ -41,7 +41,7 @@ class PlotOptions:
Raises
------
TypeError
TypeError is thrown if one of the attributes is not the correct
TypeError is thrown if one of the attributes is not of correct
type.
Returns
......@@ -50,14 +50,14 @@ class PlotOptions:
"""
# Input validation for figs is done in submodules tagplot_$engine.py
if isinstance(self.prefix, str):
pass
else:
if not isinstance(self.prefix, str):
raise TypeError("Prefix is not a string.")
if isinstance(self.id_method, str):
pass
else:
if not isinstance(self.id_method, str):
raise TypeError('The chosen id_method is not a string.')
# Store figs in a list, even if it is only one.
if not isinstance(self.figs, list):
self.figs = [self.figs]
return 0
......@@ -13,6 +13,7 @@ Functions:
import warnings
from plotid.plotoptions import PlotOptions
from plotid.tagplot_matplotlib import tagplot_matplotlib
from plotid.tagplot_image import tagplot_image
def tagplot(figs, engine, prefix='', id_method='time', location='east'):
......@@ -88,6 +89,8 @@ def tagplot(figs, engine, prefix='', id_method='time', location='east'):
match engine:
case 'matplotlib' | 'pyplot':
return tagplot_matplotlib(option_container)
case 'image' | 'fallback':
return tagplot_image(option_container)
case _:
raise ValueError(
f'The plot engine "{engine}" is not supported.')
# -*- coding: utf-8 -*-
"""
Tag your picture with an ID.
Functions:
tagplot_image(PlotOptions instance) -> list
"""
from PIL import Image, ImageDraw, ImageFont, ImageOps
from plotid.create_id import create_id
from plotid.plotoptions import PlotOptions
def tagplot_image(plotid_object):
"""
Add IDs to images/pictures with pillow.
The ID is placed visual on the figure window and returned as string in a
list together with the figures.
TagPlot can tag multiple figures at once.
"""
# Check if plotid_object is a valid instance of PlotOptions
if not isinstance(plotid_object, PlotOptions):
raise TypeError('The given options container is not an instance'
'of PlotOptions.')
# Check if figs is a valid file is done by pillow internally
ids_as_list = []
color = (128, 128, 128) # grey
font = ImageFont.load_default()
for i, img in enumerate(plotid_object.figs):
img_id = plotid_object.prefix + create_id(plotid_object.id_method)
ids_as_list.append(img_id)
img = Image.open(img)
img_txt = Image.new('L', font.getsize(img_id))
draw_txt = ImageDraw.Draw(img_txt)
draw_txt.text((0, 0), img_id, font=font, fill=255)
txt = img_txt.rotate(plotid_object.rotation, expand=1)
img.paste(ImageOps.colorize(txt, (0, 0, 0), color),
(int(img.width*plotid_object.position[0]),
int(img.height*(1-plotid_object.position[1]))), txt)
plotid_object.figs[i] = img
img.save(f'hello_{i}.png')
return [plotid_object.figs, ids_as_list]
......@@ -3,7 +3,7 @@
Tag your matplotlib plot with an ID.
Functions:
tagplot_matplotlib(figure object, string) -> list
tagplot_matplotlib(PlotOptions instance) -> list
"""
import matplotlib
......@@ -24,6 +24,7 @@ def tagplot_matplotlib(plotid_object):
if not isinstance(plotid_object, PlotOptions):
raise TypeError('The given options container is not an instance'
'of PlotOptions.')
# Check if figs is a valid figure or a list of valid figures
if isinstance(plotid_object.figs, matplotlib.figure.Figure):
plotid_object.figs = [plotid_object.figs]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment