async def METHOD_NAME(self, options: scrypted_sdk.ImageOptions = None) -> bytearray:
    pilImage: PILImage = await self.toImageInternal(options)
    if not options or not options.get('format', None):
        def format():
            return pilImage.pilImage.tobytes()
        return await to_thread(format)
    elif options['format'] == 'rgb':
        def format():
            rgbx = pilImage.pilImage
            if rgbx.mode != 'RGBA':
                return rgbx.tobytes()
            rgb = rgbx.convert('RGB')
            try:
                return rgb.tobytes()
            finally:
                rgb.close()
        return await to_thread(format)
    elif options['format'] == 'gray':
        def format():
            if pilImage.pilImage.mode == 'L':
                return pilImage.pilImage.tobytes()
            l = pilImage.pilImage.convert('L')
            try:
                return l.tobytes()
            finally:
                l.close()
        return await to_thread(format)
    def save():
        bytesArray = io.BytesIO()
        if pilImage.pilImage.mode == 'RGBA':
            rgb = pilImage.pilImage.convert('RGB')
            try:
                rgb.save(bytesArray, format='JPEG')
            finally:
                rgb.close()
        else:
            pilImage.pilImage.save(bytesArray, format='JPEG')
        # pilImage.pilImage.save(bytesArray, format=options['format'])
        return bytesArray.getvalue()
    return await to_thread(lambda: save())