Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从GCS将字体加载到ImageFont_Python_Google Cloud Storage_Python Imaging Library - Fatal编程技术网

Python 从GCS将字体加载到ImageFont

Python 从GCS将字体加载到ImageFont,python,google-cloud-storage,python-imaging-library,Python,Google Cloud Storage,Python Imaging Library,我在内存中加载字体时遇到问题,直接从GCS加载,而不创建临时文件 目标是将其加载到: from PIL import Image, ImageFont, ImageDraw BUCKET_NAME = 'bucket_name' gs_path = 'path_in_bucket/object.otf' font_file = load_font_from_gcs(gs_path) font = ImageFont.truetype(font_file, 18) 我尝试使用以下两个功能下载:

我在内存中加载字体时遇到问题,直接从GCS加载,而不创建临时文件

目标是将其加载到:

from PIL import Image, ImageFont, ImageDraw
BUCKET_NAME = 'bucket_name'
gs_path = 'path_in_bucket/object.otf'
font_file = load_font_from_gcs(gs_path)
font = ImageFont.truetype(font_file, 18)
我尝试使用以下两个功能下载:

from google.cloud import storage

storage_client = storage.Client()

def load_font_from_gcs(gs_path):
    font_file = download_blob_as_string(gs_path)
    return font_file

def download_blob_as_string(source_blob_name, bucket_name=BUCKET_NAME):
    """Downloads a blob from the bucket as string."""

    storage_client = storage.Client()

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    return blob.download_as_string()
然而,我不断遇到编码/解码错误或类型错误,例如

文件 “/anaconda3/envs/ml36/lib/python3.6/site packages/PIL/ImageFont.py”, 第161行,在init 字体、大小、索引、编码、布局\u引擎=布局\u引擎类型错误:参数1必须是不带空字节的编码字符串,而不是 字节


将其包装在BytesIO中在我的Mac上工作:

from io import BytesIO
...
...
font_file = load_font_from_gcs(gs_path)
font = ImageFont.truetype(BytesIO(font_file), 18)

download\u as\u string()
函数,顾名思义,返回字节(请参阅)。也许你的问题是相关的?我知道它返回字节,问题是
PIL/ImageFont.py
应该处理字节。我认为逻辑上有一个缺陷,因为if平台!=win32,我们不在这行应该是
self.font=core.getfont(“”,大小,索引,编码,self.font\u字节,布局\u引擎)的任何地方使用字节。
Awesome!两个字节的对象之间有区别吗?我不知道,对不起!根据经验,我只知道
BytesIO
在需要文件的地方工作得很好!