-
I want to have a new encoder which handles ZLIB. The easiest way to do this is to generate a raw stream first and then encode it. When I try to use tobytes() in the encoder it complains: def encode(self, bufsize):
buf1 = self.im.tobytes("raw")
Is there anyway to get this to work?? The work-around seems to be to write a custom _save routine def _save(im, fp, filename):
buf1 = im.tobytes()
buf2 = zlib.compress(buf1);
fp.write(buf2) so then can save to an io.BytesIO data2 = io.BytesIO()
img.save(data2,"ZLIB") However, given I never want to save to a file, this does not seem as clear .. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The simplest way to do this would be to construct an image around the ImagingCore object. buf1 = Image.Image()._new(self.im).tobytes("raw") If you would like to work at a lower level, you could start extracting the code from encoder = Image.core.raw_encoder(self.im.mode, self.im.mode)
encoder.setimage(self.im)
bufsize = max(65536, self.im.size[0] * 4)
bytes_consumed, errcode, data = encoder.encode(bufsize)
if errcode < 0:
msg = f"encoder error {errcode} in tobytes"
raise RuntimeError(msg)
buf1 = data |
Beta Was this translation helpful? Give feedback.
The simplest way to do this would be to construct an image around the ImagingCore object.
If you would like to work at a lower level, you could start extracting the code from
tobytes()
instead.