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

Method set_alpha

src/__init__.py:13876–13969  ·  view source on GitHub ↗

Set alpha channel to values contained in a byte array. If omitted, set alphas to 255. Args: alphavalues: (bytes) with length (width * height) or 'None'. premultiply: (bool, True) premultiply colors with alpha values. opaque: (tuple, length colorsp

(self, alphavalues=None, premultiply=1, opaque=None, matte=None)

Source from the content-addressed store, hash-verified

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.
13878 If omitted, set alphas to 255.
13879
13880 Args:
13881 alphavalues: (bytes) with length (width * height) or 'None'.
13882 premultiply: (bool, True) premultiply colors with alpha values.
13883 opaque: (tuple, length colorspace.n) this color receives opacity 0.
13884 matte: (tuple, length colorspace.n)) preblending background color.
13885 """
13886 pix = self.this
13887 alpha = 0
13888 m = 0
13889 if pix.alpha() == 0:
13890 raise ValueError( MSG_PIX_NOALPHA)
13891 n = mupdf.fz_pixmap_colorants(pix)
13892 w = mupdf.fz_pixmap_width(pix)
13893 h = mupdf.fz_pixmap_height(pix)
13894 balen = w * h * (n+1)
13895 colors = [0, 0, 0, 0] # make this color opaque
13896 bgcolor = [0, 0, 0, 0] # preblending background color
13897 zero_out = 0
13898 bground = 0
13899 if opaque and isinstance(opaque, (list, tuple)) and len(opaque) == n:
13900 for i in range(n):
13901 colors[i] = opaque[i]
13902 zero_out = 1
13903 if matte and isinstance( matte, (tuple, list)) and len(matte) == n:
13904 for i in range(n):
13905 bgcolor[i] = matte[i]
13906 bground = 1
13907 data = bytes()
13908 data_len = 0
13909 if alphavalues:
13910 #res = JM_BufferFromBytes(alphavalues)
13911 #data_len, data = mupdf.fz_buffer_storage(res)
13912 #if data_len < w * h:
13913 # THROWMSG("bad alpha values")
13914 # fixme: don't seem to need to create an fz_buffer - can
13915 # use <alphavalues> directly?
13916 if isinstance(alphavalues, (bytes, bytearray)):
13917 data = alphavalues
13918 data_len = len(alphavalues)
13919 else:
13920 assert 0, f'unexpected type for alphavalues: {type(alphavalues)}'
13921 if data_len < w * h:
13922 raise ValueError( "bad alpha values")
13923 if 1:
13924 # Use C implementation for speed.
13925 mupdf.Pixmap_set_alpha_helper(
13926 balen,
13927 n,
13928 data_len,
13929 zero_out,
13930 mupdf.python_buffer_data( data),
13931 pix.m_internal,
13932 premultiply,
13933 bground,

Callers 3

recoverpixFunction · 0.95
recoverpixFunction · 0.95
test_setalphaFunction · 0.95

Calls 1

alphaMethod · 0.80

Tested by 1

test_setalphaFunction · 0.76