[英]Opening a GIF picture with PIL in Python
I am trying to open a GIF file but it is coming up with a syntax error i do not understand.我正在尝试打开一个 GIF 文件,但它出现了一个我不明白的语法错误。 Does the image have to change to PNG / JPEG because i am trying to keep transparency which would be lost if i have to change the file type.
图像是否必须更改为 PNG/JPEG,因为我试图保持透明度,如果我必须更改文件类型,这将丢失。
The Syntax Error i don't understand我不明白的语法错误
I have tried to keep the image and open it with Turtle by using screen.addshape.我试图保留图像并使用 screen.addshape 用 Turtle 打开它。 That works for the image however i have had a problem with resizing the image so i am having to use the PIL library to open the images and then resize them
这适用于图像,但是我在调整图像大小时遇到问题,因此我不得不使用 PIL 库打开图像,然后调整它们的大小
Image.open("your.gif")
should work. Image.open("your.gif")
应该可以工作。 However, the path is not the string you think it is.但是,路径不是您认为的字符串。
\\
is the escape character. \\
是转义字符。 To type a \\
, you will need to use \\\\
in stead of \\
.要键入
\\
,您需要使用\\\\
代替\\
。
Thus Image.open("C:\\Your\\Path\\to a funny\\image.gif")
becomes Image.open("C:\\\\Your\\\\Path\\\\to a funny\\\\image.gif")
or Image.open(r"C:\\Your\\Path\\to a funny\\image.gif")
(a raw string).因此
Image.open("C:\\Your\\Path\\to a funny\\image.gif")
Image.open("C:\\\\Your\\\\Path\\\\to a funny\\\\image.gif")
Image.open("C:\\Your\\Path\\to a funny\\image.gif")
变成了Image.open("C:\\\\Your\\\\Path\\\\to a funny\\\\image.gif")
或Image.open(r"C:\\Your\\Path\\to a funny\\image.gif")
(原始字符串)。
其导致问题的文件路径的字符串编码请参阅: “Unicode 错误”unicodeescape”编解码器无法解码字节...无法在 Python 3 中打开文本文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.