def save_img(self, data, width, height, output): cf_data = Quartz.CFDataCreateMutable(Quartz.kCFAllocatorDefault, 0) dest = Quartz.CGImageDestinationCreateWithData(cf_data, kUTTypePNG, 1, None) if not dest: err = 'MSS: CGImageDestinationCreateWithURL() failed.' raise ScreenshotError(err) Quartz.CGImageDestinationAddImage(dest, data, None) if not Quartz.CGImageDestinationFinalize(dest): raise ScreenshotError('MSS: CGImageDestinationFinalize() failed.') cf_data_bytes = Quartz.CFDataGetBytes(cf_data, Quartz.CFRangeMake(0, Quartz.CFDataGetLength(cf_data)), None) output.write(cf_data_bytes)
def cgimageref_to_image(imgref) -> Image: buf = Quartz.CFDataCreateMutable(None, 0) dest = Quartz.CGImageDestinationCreateWithData(buf, "public.tiff", 1, None) Quartz.CGImageDestinationAddImage(dest, imgref, None) Quartz.CGImageDestinationFinalize(dest) buf_size = Quartz.CFDataGetLength(buf) py_buf = io.BytesIO() py_buf.write(Quartz.CFDataGetBytePtr(buf).as_buffer(buf_size)) py_buf.seek(0) out = Image.open(py_buf, formats=("TIFF", )) if _debug_dump_file: out.save("/tmp/game.bmp") open("/tmp/native.xbm", "wb").write(py_buf.getbuffer()) return out