MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / tobytes

Method tobytes

src/__init__.py:13971–13999  ·  view source on GitHub ↗

Convert to binary image stream of desired type.

(self, output="png", jpg_quality=95)

Source from the content-addressed store, hash-verified

13969 k += 1
13970
13971 def tobytes(self, output="png", jpg_quality=95):
13972 '''
13973 Convert to binary image stream of desired type.
13974 '''
13975 valid_formats = {
13976 "png": 1,
13977 "pnm": 2,
13978 "pgm": 2,
13979 "ppm": 2,
13980 "pbm": 2,
13981 "pam": 3,
13982 "tga": 4,
13983 "tpic": 4,
13984 "psd": 5,
13985 "ps": 6,
13986 'jpg': 7,
13987 'jpeg': 7,
13988 }
13989 idx = valid_formats.get(output.lower(), None)
13990 if idx is None:
13991 raise ValueError(f"Image format {output} not in {tuple(valid_formats.keys())}")
13992 if self.alpha and idx in (2, 6, 7):
13993 raise ValueError("'{output}' cannot have alpha")
13994 if self.colorspace and self.colorspace.n > 3 and idx in (1, 2, 4):
13995 raise ValueError(f"unsupported colorspace for '{output}'")
13996 if idx == 7:
13997 self.set_dpi(self.xres, self.yres)
13998 barray = self._tobytes(idx, jpg_quality)
13999 return barray
14000
14001 def set_dpi(self, xres, yres):
14002 """Set resolution in both dimensions."""

Callers 12

insert_fileMethod · 0.95
test_2369Function · 0.95
pixmaps_diffFunction · 0.95
_add_stamp_annotMethod · 0.45
test_2391Function · 0.45
test_2812Function · 0.45
test_4155Function · 0.45
test_spikesFunction · 0.45
test_rewrite_imagesFunction · 0.45
test_labels_styleAFunction · 0.45
test_encryptionFunction · 0.45

Calls 3

set_dpiMethod · 0.95
_tobytesMethod · 0.95
getMethod · 0.45

Tested by 9

test_2369Function · 0.76
test_2391Function · 0.36
test_2812Function · 0.36
test_4155Function · 0.36
test_spikesFunction · 0.36
test_rewrite_imagesFunction · 0.36
test_labels_styleAFunction · 0.36
test_encryptionFunction · 0.36