|
16 | 16 | WIDGETS_PATH = osp.join(PKG_PATH, "cdlclient", "widgets") |
17 | 17 |
|
18 | 18 |
|
19 | | -def convert_png_to_code(filename: str) -> bytes: |
20 | | - """Convert PNG image to Python code, so that it can be bundled with the |
21 | | - application, without having to load the image from disk.""" |
| 19 | +def imagefile_to_base64(filename: str) -> bytes: |
| 20 | + """Convert image file to Base64-encoded bytes |
| 21 | +
|
| 22 | + Args: |
| 23 | + filename: image filename |
| 24 | +
|
| 25 | + Returns: |
| 26 | + Base64-encoded bytes |
| 27 | + """ |
22 | 28 | image = QG.QImage(filename) |
23 | 29 | data = QC.QByteArray() |
24 | 30 | buf = QC.QBuffer(data) |
25 | 31 | image.save(buf, "PNG") |
26 | 32 | return data.toBase64().data() |
27 | 33 |
|
28 | 34 |
|
29 | | -def test_conv(filename: str, destmod: str) -> str: |
30 | | - """Test image to code conversion""" |
| 35 | +def imagefile_to_python_module(filename: str, destmod: str) -> None: |
| 36 | + """Convert image file to Python module |
| 37 | +
|
| 38 | + Args: |
| 39 | + filename: image filename |
| 40 | + destmod: destination module name |
| 41 | + """ |
| 42 | + data = imagefile_to_base64(filename) |
| 43 | + destmod_path = osp.join(WIDGETS_PATH, destmod + ".py") |
| 44 | + with open(destmod_path, "wb") as fn: |
| 45 | + fn.write("# -*- coding: utf-8 -*-\n\n".encode("utf-8")) |
| 46 | + fn.write("# pylint: skip-file\n\n".encode("utf-8")) |
| 47 | + fn.write("DATA = b'".encode("utf-8")) |
| 48 | + fn.write(data) |
| 49 | + fn.write("'".encode("utf-8")) |
| 50 | + |
| 51 | + |
| 52 | +def test_conv(filename: str, destmod: str) -> None: |
| 53 | + """Test image to code conversion |
| 54 | +
|
| 55 | + Args: |
| 56 | + filename: image filename |
| 57 | + destmod: destination module name |
| 58 | + """ |
31 | 59 | with qt_app_context(exec_loop=True): |
32 | 60 | widget = QW.QWidget() |
33 | 61 | vlayout = QW.QVBoxLayout() |
34 | 62 | widget.setLayout(vlayout) |
35 | 63 | label1 = QW.QLabel() |
36 | 64 | label1.setPixmap(QG.QPixmap(filename)) |
37 | 65 | label2 = QW.QLabel() |
38 | | - data = convert_png_to_code(filename) |
39 | | - destmod_path = osp.join(WIDGETS_PATH, destmod + ".py") |
40 | | - with open(destmod_path, "wb") as fn: |
41 | | - fn.write("DATA = b'".encode("utf-8")) |
42 | | - fn.write(data) |
43 | | - fn.write("'".encode("utf-8")) |
| 66 | + imagefile_to_python_module(filename, destmod) |
44 | 67 | mod = __import__("cdlclient.widgets." + destmod, fromlist=[destmod]) |
45 | 68 | pixmap = QG.QPixmap() |
46 | 69 | pixmap.loadFromData(QC.QByteArray.fromBase64(mod.DATA)) |
|
0 commit comments