diff --git a/README.md b/README.md index e7c6c38a86a58c0a85b2521c55126b1a8c2fc868..926eb080a06a6782f694ed7446f68417aed877f1 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ The argument "plot_engine" defines which plot engine was used to create the figu tagplot returns a PlotIDTransfer object that contains the tagged figures and the corresponding IDs as strings. Optional parameters can be used to customize the tag process. +- *figure_ids*: list of str, optional + IDs that will be printed on the plot. If empty, IDs will be generated for each plot. If this option is used, an ID for each plot has to be specified. Default: []. - *prefix* : str, optional Will be added as prefix to the ID. - *id_method* : str, optional diff --git a/examples/image_example.py b/examples/image_example.py index 251429ff26fc43aac32ff76f4769da3c8dac5de2..1172450cd7dbb7f950388a0c649dd5b33c4b4661 100644 --- a/examples/image_example.py +++ b/examples/image_example.py @@ -29,6 +29,7 @@ FIGS_AND_IDS = tagplot( "image", prefix=PROJECT_ID, id_method="time", + location="west", qrcode=True, ) # Required arguments: tagplot(images as list, desired plot engine) diff --git a/plotid/plotoptions.py b/plotid/plotoptions.py index 274aa48abe92a7640a917741ce6154d870903a1a..3a60c6db3b0bda2beadb52a4bc066fd4f01b0189 100644 --- a/plotid/plotoptions.py +++ b/plotid/plotoptions.py @@ -86,6 +86,9 @@ class PlotOptions: self.position = position self.prefix = kwargs.get("prefix", "") self.id_method = kwargs.get("id_method", "time") + # Overwrite id_method if figure_ids were defined by the user + if self.figure_ids: + self.id_method = "custom" self.qrcode = kwargs.get("qrcode", False) self.qr_position = kwargs.get("qr_position", (1, 0)) self.qr_size = kwargs.get("qr_size", 100) diff --git a/plotid/tagplot.py b/plotid/tagplot.py index f5dd420c5a53854fdb0b62abfa1a0e28724a484d..b43b3a36bef27e7327e5d3146d015b30546d6287 100644 --- a/plotid/tagplot.py +++ b/plotid/tagplot.py @@ -39,6 +39,10 @@ def tagplot( Other Parameters ---------------- + figure_ids: list of str, optional + IDs that will be printed on the plot. If empty, IDs will be generated for each + plot. If this option is used, an ID for each plot has to be specified. + Default: []. location : str, optional Location for ID to be displayed on the plot. Default is "east". rotation: float or int, optional diff --git a/plotid/tagplot_image.py b/plotid/tagplot_image.py index 86a92dce2dd61bae1fd1205b7f9c2d18931d93de..a1f60eb7c86c2af040dbd3563cd882ecd01360fb 100644 --- a/plotid/tagplot_image.py +++ b/plotid/tagplot_image.py @@ -56,9 +56,22 @@ def tagplot_image(plotid_object: PlotOptions) -> PlotIDTransfer: except OSError: warnings.warn("Font was not found.\nplotID continues with fallback font.") - for i, img in enumerate(plotid_object.figs): - img_id = plotid_object.prefix + create_id(plotid_object.id_method) - plotid_object.figure_ids.append(img_id) + if plotid_object.font: + try: + # Absolute path to font file (.ttf or .otf) has to be given + font = ImageFont.truetype(plotid_object.font, plotid_object.fontsize) + except OSError: + warnings.warn("Font was not found.\nplotID continues with fallback font.") + + for j, img in enumerate(plotid_object.figs): + if plotid_object.id_method == "custom": + # If IDs were given by the user, use them + img_id: str = str(plotid_object.figure_ids[j]) + else: + # Create ID with given method + img_id = plotid_object.prefix + create_id(plotid_object.id_method) + plotid_object.figure_ids.append(img_id) + img = Image.open(img) if plotid_object.id_on_plot: @@ -92,7 +105,7 @@ def tagplot_image(plotid_object: PlotOptions) -> PlotIDTransfer: ), ), ) - plotid_object.figs[i] = img + plotid_object.figs[j] = img figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids) return figs_and_ids diff --git a/plotid/tagplot_matplotlib.py b/plotid/tagplot_matplotlib.py index 1da6cce9e058d1586f51047c9db0935b3abf053c..2a0b44145537be999324662fac3b0b8d7232c710 100644 --- a/plotid/tagplot_matplotlib.py +++ b/plotid/tagplot_matplotlib.py @@ -50,10 +50,15 @@ def tagplot_matplotlib(plotid_object: PlotOptions) -> PlotIDTransfer: font = matplotlib.font_manager.FontProperties(fname=font) # Loop to create and position the IDs - for fig in plotid_object.figs: - fig_id: str = create_id(plotid_object.id_method) - fig_id = plotid_object.prefix + fig_id - plotid_object.figure_ids.append(fig_id) + for j, fig in enumerate(plotid_object.figs): + if plotid_object.id_method == "custom": + # If IDs were given by the user, use them + fig_id: str = str(plotid_object.figure_ids[j]) + else: + # Create ID with given method + fig_id = create_id(plotid_object.id_method) + fig_id = plotid_object.prefix + fig_id + plotid_object.figure_ids.append(fig_id) plt.figure(fig) if plotid_object.id_on_plot: diff --git a/tests/test_tagplot_image.py b/tests/test_tagplot_image.py index 727243fada6612c2a87a084646c199f38d10ee01..2dd88fc35cee0f5d3907dabfd0947771ff90b617 100644 --- a/tests/test_tagplot_image.py +++ b/tests/test_tagplot_image.py @@ -16,7 +16,6 @@ FIG1 = plt.figure() IMG1 = "image1.png" FIG2 = plt.figure() IMG2 = "image2.jpg" -IMGS_AS_LIST = [IMG1, IMG2] # Constants for tests PROJECT_ID = "MR01" @@ -40,9 +39,10 @@ class TestTagplotImage(unittest.TestCase): respectively. """ options = PlotOptions( - IMGS_AS_LIST, + [IMG1, IMG2], ROTATION, POSITION, + figure_ids=["test123456", "654321tset"], prefix=PROJECT_ID, id_method=METHOD, qrcode=True, diff --git a/tests/test_tagplot_matplotlib.py b/tests/test_tagplot_matplotlib.py index d7e59d9f3825a465614f3179088a8b4d501106d9..d93e768610983d0dcabe6deaa913973e8516a8dd 100644 --- a/tests/test_tagplot_matplotlib.py +++ b/tests/test_tagplot_matplotlib.py @@ -33,6 +33,7 @@ class TestTagplotMatplotlib(unittest.TestCase): FIGS_AS_LIST, ROTATION, POSITION, + figure_ids=["test123456", "654321tset"], prefix=PROJECT_ID, id_method=METHOD, qrcode=True,