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

Merge branch 'qrcode' into 'dev'

QR Codes now work with mpl and image engine.

See merge request !24
parents ad6a6043 a91cb997
No related branches found
No related tags found
7 merge requests!41Include latest changes in main branch,!37Merge dev upstream changes into improve/metadata,!34Include architecture diagram in docs,!32SAST implementation,!27Update documentation and version number,!26Merge !23, !24, !25 into main,!24QR Codes now work with mpl and image engine.
Pipeline #808085 waiting for manual action
......@@ -7,6 +7,7 @@ Functions:
"""
import time
import uuid
import qrcode
def create_id(id_method):
......@@ -40,3 +41,24 @@ def create_id(id_method):
'"time": Unix time converted to hexadecimal\n'
'"random": Random UUID')
return figure_id
def create_qrcode(figure_id):
"""
Create a QR Code from an identifier.
Parameters
----------
figure_id : str
Identifier which will be embedded in the qrcode.
Returns
-------
QR Code as PilImage.
"""
qrc = qrcode.QRCode(version=1, box_size=10, border=0)
qrc.add_data(figure_id)
qrc.make(fit=True)
img = qrc.make_image(fill_color="black", back_color="white")
return img
......@@ -37,7 +37,7 @@ plt.plot(x, y, color='blue')
plt.plot(x, y_2, color='red')
plt.savefig(IMG2)
# %% TagPlot
# %% tagplot
# If multiple figures should be tagged, figures must be provided as list.
FIGS_AS_LIST = [FIG1, FIG2]
......@@ -51,6 +51,7 @@ IMGS_AS_LIST = [IMG1, IMG2]
FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID,
id_method='time', location='west')
# %% Publish
# Arguments: Source directory or files as list, destination directory, figures,
# plots or images.
......
......@@ -33,6 +33,8 @@ class PlotOptions:
id_method for creating the ID. Create an ID by Unix time is referenced
as 'time', create a random ID with id_method='random'.
The default is 'time'.
qrcode : bool, optional
Experimental status. Print qrcode on exported plot. Default: False.
"""
def __init__(self, figs, rotation, position, **kwargs):
......@@ -43,6 +45,7 @@ class PlotOptions:
self.position = position
self.prefix = kwargs.get('prefix', '')
self.id_method = kwargs.get('id_method', 'time')
self.qrcode = kwargs.get('qrcode', False)
def __str__(self):
"""Representation if an object of this class is printed."""
......
......@@ -80,8 +80,7 @@ def tagplot(figs, engine, location='east', **kwargs):
position = (0.75, 0.015)
case 'custom':
# TODO: Get rotation and position from user input & check if valid
rotation = 0
position = (0.5, 0.5)
pass
case _:
warnings.warn(f'Location "{location}" is not a defined '
'location, TagPlot uses location "east" '
......
......@@ -7,7 +7,7 @@ Functions:
"""
import os
from PIL import Image, ImageDraw, ImageFont, ImageOps
from plotid.create_id import create_id
from plotid.create_id import create_id, create_qrcode
from plotid.plotoptions import PlotOptions, PlotIDTransfer
......@@ -55,6 +55,10 @@ def tagplot_image(plotid_object):
(int(img.width*plotid_object.position[0]),
int(img.height*(1-plotid_object.position[1]))), txt)
if plotid_object.qrcode:
qrcode = create_qrcode(img_id)
qrcode.thumbnail((100, 100), Image.ANTIALIAS)
img.paste(qrcode, box=(img.width-100, img.height-100))
plotid_object.figs[i] = img
figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids)
......
......@@ -8,7 +8,8 @@ Functions:
import matplotlib
import matplotlib.pyplot as plt
from plotid.create_id import create_id
from PIL import Image
from plotid.create_id import create_id, create_qrcode
from plotid.plotoptions import PlotOptions, PlotIDTransfer
......@@ -51,6 +52,11 @@ def tagplot_matplotlib(plotid_object):
s=fig_id, ha='left', wrap=True,
rotation=plotid_object.rotation,
fontsize=fontsize, color=color)
if plotid_object.qrcode:
qrcode = create_qrcode(fig_id)
qrcode.thumbnail((100, 100), Image.ANTIALIAS)
fig.figimage(qrcode, fig.bbox.xmax - 100, 0, cmap='bone')
fig.tight_layout()
figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids)
......
......@@ -8,4 +8,5 @@ packaging==21.3
Pillow==9.1.0
pyparsing==3.0.8
python-dateutil==2.8.2
qrcode==7.3.1
six==1.16.0
......@@ -5,7 +5,9 @@ Unittests for create_id
"""
import unittest
import qrcode
import plotid.create_id as cid
from plotid.create_id import create_qrcode
class TestCreateID(unittest.TestCase):
......@@ -30,6 +32,11 @@ class TestCreateID(unittest.TestCase):
self.assertEqual(len(cid.create_id('time')), 10)
self.assertEqual(len(cid.create_id('random')), 8)
def test_qrcode(self):
"""Test if qrcode returns a image."""
self.assertIsInstance(create_qrcode('test_ID'),
qrcode.image.pil.PilImage)
if __name__ == '__main__':
unittest.main()
......@@ -44,7 +44,7 @@ class TestTagplot(unittest.TestCase):
"<class 'plotid.plotoptions.PlotOptions'>: {'figs': "
"'FIG', 'figure_ids': [], 'rotation': 270, 'position'"
": (100, 200), 'prefix': 'xyz', 'id_method': "
"'random'}")
"'random', 'qrcode': False}")
def test_str_plotidtransfer(self):
"""
......
......@@ -40,7 +40,7 @@ class TestTagplotImage(unittest.TestCase):
respectively.
"""
options = PlotOptions(IMGS_AS_LIST, ROTATION, POSITION,
prefix=PROJECT_ID, id_method=METHOD)
prefix=PROJECT_ID, id_method=METHOD, qrcode=True)
options.validate_input()
figs_and_ids = tagplot_image(options)
self.assertIsInstance(figs_and_ids.figs[0],
......
......@@ -30,7 +30,7 @@ class TestTagplotMatplotlib(unittest.TestCase):
def test_mplfigures(self):
""" Test of returned objects. Check if they are matplotlib figures. """
options = PlotOptions(FIGS_AS_LIST, ROTATION, POSITION,
prefix=PROJECT_ID, id_method=METHOD)
prefix=PROJECT_ID, id_method=METHOD, qrcode=True)
options.validate_input()
figs_and_ids = tagplot_matplotlib(options)
self.assertIsInstance(figs_and_ids.figs[0], Figure)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment