comparison tests/test_barcode.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 import os
2
3 import pymupdf
4
5
6 def test_barcode():
7 if pymupdf.mupdf_version_tuple < (1, 26):
8 print(f'Not testing barcode because {pymupdf.mupdf_version=} < 1.26')
9 return
10 path = os.path.normpath(f'{__file__}/../../tests/test_barcode_out.pdf')
11
12 url = 'http://artifex.com'
13 text_in = '012345678901'
14 text_out = '123456789012'
15 # Create empty document and add a qrcode image.
16 with pymupdf.Document() as document:
17 page = document.new_page()
18
19 pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
20 pymupdf.mupdf.FZ_BARCODE_QRCODE,
21 url,
22 512,
23 4, # ec_level
24 0, # quiet
25 1, # hrt
26 )
27 pixmap = pymupdf.Pixmap('raw', pixmap)
28 page.insert_image(
29 (0, 0, 100, 100),
30 pixmap=pixmap,
31 )
32 pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
33 pymupdf.mupdf.FZ_BARCODE_EAN13,
34 text_in,
35 512,
36 4, # ec_level
37 0, # quiet
38 1, # hrt
39 )
40 pixmap = pymupdf.Pixmap('raw', pixmap)
41 page.insert_image(
42 (0, 200, 100, 300),
43 pixmap=pixmap,
44 )
45
46 document.save(path)
47
48 with pymupdf.open(path) as document:
49 page = document[0]
50 for i, ii in enumerate(page.get_images()):
51 xref = ii[0]
52 pixmap = pymupdf.Pixmap(document, xref)
53 hrt, barcode_type = pymupdf.mupdf.fz_decode_barcode_from_pixmap2(
54 pixmap.this,
55 0, # rotate.
56 )
57 print(f'{hrt=}')
58 if i == 0:
59 assert hrt == url
60 elif i == 1:
61 assert hrt == text_out
62 else:
63 assert 0