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

Add rudimentary qrcode support.

parent 89a25a23
No related branches found
No related tags found
1 merge request!24QR Codes now work with mpl and image engine.
Pipeline #803534 waiting for manual action
...@@ -33,6 +33,8 @@ class PlotOptions: ...@@ -33,6 +33,8 @@ class PlotOptions:
id_method for creating the ID. Create an ID by Unix time is referenced id_method for creating the ID. Create an ID by Unix time is referenced
as 'time', create a random ID with id_method='random'. as 'time', create a random ID with id_method='random'.
The default is 'time'. The default is 'time'.
qrcode : bool, optional
Experimental status. Print qrcode on exported plot. Default: False.
""" """
def __init__(self, figs, rotation, position, **kwargs): def __init__(self, figs, rotation, position, **kwargs):
...@@ -43,6 +45,7 @@ class PlotOptions: ...@@ -43,6 +45,7 @@ class PlotOptions:
self.position = position self.position = position
self.prefix = kwargs.get('prefix', '') self.prefix = kwargs.get('prefix', '')
self.id_method = kwargs.get('id_method', 'time') self.id_method = kwargs.get('id_method', 'time')
self.qrcode = kwargs.get('qrcode', False)
def __str__(self): def __str__(self):
"""Representation if an object of this class is printed.""" """Representation if an object of this class is printed."""
......
...@@ -55,6 +55,7 @@ def tagplot_image(plotid_object): ...@@ -55,6 +55,7 @@ def tagplot_image(plotid_object):
(int(img.width*plotid_object.position[0]), (int(img.width*plotid_object.position[0]),
int(img.height*(1-plotid_object.position[1]))), txt) int(img.height*(1-plotid_object.position[1]))), txt)
if plotid_object.qrcode:
qrcode = create_qrcode(img_id) qrcode = create_qrcode(img_id)
qrcode.thumbnail((100, 100), Image.ANTIALIAS) qrcode.thumbnail((100, 100), Image.ANTIALIAS)
img.paste(qrcode, box=(img.width-100, img.height-100)) img.paste(qrcode, box=(img.width-100, img.height-100))
......
...@@ -52,7 +52,9 @@ def tagplot_matplotlib(plotid_object): ...@@ -52,7 +52,9 @@ def tagplot_matplotlib(plotid_object):
s=fig_id, ha='left', wrap=True, s=fig_id, ha='left', wrap=True,
rotation=plotid_object.rotation, rotation=plotid_object.rotation,
fontsize=fontsize, color=color) fontsize=fontsize, color=color)
qrcode = create_qrcode(figure_id)
if plotid_object.qrcode:
qrcode = create_qrcode(fig_id)
qrcode.thumbnail((100, 100), Image.ANTIALIAS) qrcode.thumbnail((100, 100), Image.ANTIALIAS)
fig.figimage(qrcode, fig.bbox.xmax - 100, 0, cmap='bone') fig.figimage(qrcode, fig.bbox.xmax - 100, 0, cmap='bone')
fig.tight_layout() fig.tight_layout()
......
...@@ -5,7 +5,9 @@ Unittests for create_id ...@@ -5,7 +5,9 @@ Unittests for create_id
""" """
import unittest import unittest
import qrcode
import plotid.create_id as cid import plotid.create_id as cid
from plotid.create_id import create_qrcode
class TestCreateID(unittest.TestCase): class TestCreateID(unittest.TestCase):
...@@ -30,6 +32,11 @@ 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('time')), 10)
self.assertEqual(len(cid.create_id('random')), 8) 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__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -44,7 +44,7 @@ class TestTagplot(unittest.TestCase): ...@@ -44,7 +44,7 @@ class TestTagplot(unittest.TestCase):
"<class 'plotid.plotoptions.PlotOptions'>: {'figs': " "<class 'plotid.plotoptions.PlotOptions'>: {'figs': "
"'FIG', 'figure_ids': [], 'rotation': 270, 'position'" "'FIG', 'figure_ids': [], 'rotation': 270, 'position'"
": (100, 200), 'prefix': 'xyz', 'id_method': " ": (100, 200), 'prefix': 'xyz', 'id_method': "
"'random'}") "'random', 'qrcode': False}")
def test_str_plotidtransfer(self): def test_str_plotidtransfer(self):
""" """
......
...@@ -40,7 +40,7 @@ class TestTagplotImage(unittest.TestCase): ...@@ -40,7 +40,7 @@ class TestTagplotImage(unittest.TestCase):
respectively. respectively.
""" """
options = PlotOptions(IMGS_AS_LIST, ROTATION, POSITION, options = PlotOptions(IMGS_AS_LIST, ROTATION, POSITION,
prefix=PROJECT_ID, id_method=METHOD) prefix=PROJECT_ID, id_method=METHOD, qrcode=True)
options.validate_input() options.validate_input()
figs_and_ids = tagplot_image(options) figs_and_ids = tagplot_image(options)
self.assertIsInstance(figs_and_ids.figs[0], self.assertIsInstance(figs_and_ids.figs[0],
......
...@@ -30,7 +30,7 @@ class TestTagplotMatplotlib(unittest.TestCase): ...@@ -30,7 +30,7 @@ class TestTagplotMatplotlib(unittest.TestCase):
def test_mplfigures(self): def test_mplfigures(self):
""" Test of returned objects. Check if they are matplotlib figures. """ """ Test of returned objects. Check if they are matplotlib figures. """
options = PlotOptions(FIGS_AS_LIST, ROTATION, POSITION, options = PlotOptions(FIGS_AS_LIST, ROTATION, POSITION,
prefix=PROJECT_ID, id_method=METHOD) prefix=PROJECT_ID, id_method=METHOD, qrcode=True)
options.validate_input() options.validate_input()
figs_and_ids = tagplot_matplotlib(options) figs_and_ids = tagplot_matplotlib(options)
self.assertIsInstance(figs_and_ids.figs[0], Figure) 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