Returns a pymupdf.Pixmap that represents the difference between pixmaps and . Each byte in the returned pixmap is `128 + (b_byte - a_byte) // 2`.
(a, b, out_prefix='')
| 66 | |
| 67 | |
| 68 | def pixmaps_diff(a, b, out_prefix=''): |
| 69 | ''' |
| 70 | Returns a pymupdf.Pixmap that represents the difference between pixmaps <a> |
| 71 | and <b>. |
| 72 | |
| 73 | Each byte in the returned pixmap is `128 + (b_byte - a_byte) // 2`. |
| 74 | ''' |
| 75 | if isinstance(a, str): |
| 76 | print(f'{out_prefix}pixmaps_rms(): reading pixmap from {a=}.') |
| 77 | a = pymupdf.Pixmap(a) |
| 78 | if isinstance(b, str): |
| 79 | print(f'{out_prefix}pixmaps_rms(): reading pixmap from {b=}.') |
| 80 | b = pymupdf.Pixmap(b) |
| 81 | assert a.irect == b.irect, f'Differing rects: {a.irect=} {b.irect=}.' |
| 82 | a_mv = a.samples_mv |
| 83 | b_mv = b.samples_mv |
| 84 | c = pymupdf.Pixmap(a.tobytes()) |
| 85 | c_mv = c.samples_mv |
| 86 | assert len(a_mv) == len(b_mv) == len(c_mv) |
| 87 | if 1: |
| 88 | print(f'{len(a_mv)=}') |
| 89 | for i, (a_byte, b_byte, c_byte) in enumerate(zip(a_mv, b_mv, c_mv)): |
| 90 | assert 0 <= a_byte < 256 |
| 91 | assert 0 <= b_byte < 256 |
| 92 | assert 0 <= c_byte < 256 |
| 93 | # Set byte to 128 plus half the diff so we represent the full |
| 94 | # -255..+255 range. |
| 95 | c_mv[i] = 128 + (b_byte - a_byte) // 2 |
| 96 | return c |
nothing calls this directly
no test coverage detected
searching dependent graphs…