在Colab中访问提取的zip文件 [英] Accessing extracted zip file in Colab

查看:88
本文介绍了在Colab中访问提取的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些案例研究.我正在尝试使用Google Colab中的PIL库,并且无法获取 ImageFont 来读取我最初的压缩文件.代码:

Some case study here. I am trying to play with PIL library in Google Colab, and can't get ImageFont to read my originally zipped file. The code:

import requests, zipfile, io
r3 = requests.get('https://sources.archlinux.org/other/community/ttf-roboto/ttf-roboto-hinted-2.138.zip') 
z3 = zipfile.ZipFile(io.BytesIO(r3.content))
z3.extractall()

到目前为止,如果我使用ls浏览目录,它将显示以下内容:

So far so good, and if I browse my directory with ls, it shows me the elements:

ls

显示:

LICENSE                          RobotoCondensed-Regular.ttf
__MACOSX/                        Roboto-Italic.ttf
Roboto-BlackItalic.ttf           Roboto-LightItalic.ttf
Roboto-Black.ttf                 Roboto-Light.ttf
Roboto-BoldItalic.ttf            Roboto-MediumItalic.ttf
Roboto-Bold.ttf                  Roboto-Medium.ttf
RobotoCondensed-BoldItalic.ttf   Roboto-Regular.ttf
RobotoCondensed-Bold.ttf         Roboto-ThinItalic.ttf
RobotoCondensed-Italic.ttf       Roboto-Thin.ttf
RobotoCondensed-LightItalic.ttf  sample_data/
RobotoCondensed-Light.ttf

现在让我们导入ImageFont

Now let's import ImageFont

from PIL import ImageFont

我如何读取文件?如果我尝试这样做:

How do I read the file? If I try this:

# how do I make it work if I read it from the extracted files?
font = ImageFont.truetype(open("Roboto-BlackItalic.ttf"), 72)

它失败并显示错误:

'utf-8'编解码器无法解码位置7的字节0x80:无效的起始字节

'utf-8' codec can't decode byte 0x80 in position 7: invalid start byte

我知道我可以将直接链接传递到请求,并且它将起作用:

I know that I can pass a direct link to requests, and it will work:

# it works if we pass a direct link to requests like this:
req = requests.get("https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf?raw=true")
font = ImageFont.truetype(io.BytesIO(req.content), 72)

但是如何从本地内存中读取文件?

But how do I read the file from the local memory?

完整图像,便于查看:

Full image for easier review:

推荐答案

您只需将文件名提供给 ImageFont.truetype(),因此请更改此行:

You only need to provide the filename to ImageFont.truetype(), so change this line:

font = ImageFont.truetype(open("Roboto-BlackItalic.ttf"), 72)

font = ImageFont.truetype("Roboto-BlackItalic.ttf", 72)

这篇关于在Colab中访问提取的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆