Convert to binary image stream of desired type.
(self, output="png", jpg_quality=95)
| 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.""" |