MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / save

Method save

src/__init__.py:13835–13874  ·  view source on GitHub ↗

Output as image in format determined by filename extension. Args: output: (str) only use to overrule filename extension. Default is PNG. Others are JPEG, JPG, PNM, PGM, PPM, PBM, PAM, PSD, PS.

(self, filename, output=None, jpg_quality=95)

Source from the content-addressed store, hash-verified

13833 return mupdf.fz_pixmap_samples_int(self.this)
13834
13835 def save(self, filename, output=None, jpg_quality=95):
13836 """Output as image in format determined by filename extension.
13837
13838 Args:
13839 output: (str) only use to overrule filename extension. Default is PNG.
13840 Others are JPEG, JPG, PNM, PGM, PPM, PBM, PAM, PSD, PS.
13841 """
13842 valid_formats = {
13843 "png": 1,
13844 "pnm": 2,
13845 "pgm": 2,
13846 "ppm": 2,
13847 "pbm": 2,
13848 "pam": 3,
13849 "psd": 5,
13850 "ps": 6,
13851 "jpg": 7,
13852 "jpeg": 7,
13853 }
13854
13855 if type(filename) is str:
13856 pass
13857 elif hasattr(filename, "absolute"):
13858 filename = str(filename)
13859 elif hasattr(filename, "name"):
13860 filename = filename.name
13861 if output is None:
13862 _, ext = os.path.splitext(filename)
13863 output = ext[1:]
13864
13865 idx = valid_formats.get(output.lower(), None)
13866 if idx is None:
13867 raise ValueError(f"Image format {output} not in {tuple(valid_formats.keys())}")
13868 if self.alpha and idx in (2, 6, 7):
13869 raise ValueError(f"'{output}' cannot have alpha")
13870 if self.colorspace and self.colorspace.n > 3 and idx in (1, 2, 4):
13871 raise ValueError(f"unsupported colorspace for '{output}'")
13872 if idx == 7:
13873 self.set_dpi(self.xres, self.yres)
13874 return self._writeIMG(filename, idx, jpg_quality)
13875
13876 def set_alpha(self, alphavalues=None, premultiply=1, opaque=None, matte=None):
13877 """Set alpha channel to values contained in a byte array.

Callers 15

test_saveFunction · 0.95
test_3058Function · 0.95
cleanFunction · 0.45
doc_joinFunction · 0.45
embedded_copyFunction · 0.45
embedded_delFunction · 0.45
embedded_addFunction · 0.45
embedded_updFunction · 0.45
extract_objectsFunction · 0.45
pil_saveMethod · 0.45
pil_tobytesMethod · 0.45
cleanFunction · 0.45

Calls 3

set_dpiMethod · 0.95
_writeIMGMethod · 0.95
getMethod · 0.45

Tested by 15

test_saveFunction · 0.76
test_3058Function · 0.76
test_checkboxFunction · 0.36
test_2742Function · 0.36
test_compressFunction · 0.36
test_707727Function · 0.36
test_3050Function · 0.36
test_3072Function · 0.36
test_3134Function · 0.36
test_3448Function · 0.36
test_3854Function · 0.36
test_4423Function · 0.36