python-2.7 - 了解 Python 中一行中的多个变量赋值

标签 python-2.7 python-imaging-library

我不确定如何正确地提出这个问题,因为我试图理解一些我还不理解的东西,因此我不知道要使用正确的术语,但我在一个关于 PIL 的 YouTube 教程 here

问题

有人可以解释一下最后一行的含义吗?我猜这是一种Python风格的声明变量,我还不熟悉,而且我不知道它叫什么,所以我无法查找它。

import Image

filename = "py.png"
image = Image.open(filename)
size = width, height = image.size

我尝试过的

我试图分解最后一行的逻辑,但它对我来说没有意义:

# assign value of width to size variable?     
size = width 
# assign value of image.size to height variable?
height = image.size 

我发现的问题是:

  • 宽度未定义。

  • image.size 表示图像尺寸,即 (512,512),因此这似乎不是 height< 的合适值

我确信这很简单,只是我还没明白。

最佳答案

解决方案

我想我通过运行以下命令找到了答案:

>>> size = width, height = 320, 240;
>>> size
(320, 240)
>>> width
320
>>> height
240

所以原帖中的代码本质上说的是:

>>> size = width, height = 512, 512;
>>> size
(512, 512)
>>> width
512
>>> height
512

这意味着:

“变量size是由两个值(widthheight)组成的元组,它们的值为512512 分别”。

它的机制还没有真正被理解,但目前已经足够好了。

关于python-2.7 - 了解 Python 中一行中的多个变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587972/

相关文章:

python - 在随机网络x图中设置不同的节点颜色

python - 有没有办法在Python中的图像上写阿拉伯文字

python - PIL 对象中的属性错误

python - 可 pickle 图像对象

python - 在 python 中对一个大文件进行异或运算

python - 如何从读取的 csv 中重新计算每个类(class)的人数

python - 在不缩放的情况下改进图像的 OCR(使用 PIL、pixbuf)?

python - 使用 Python 的 PIL,如何增强图像的对比度/饱和度?

Python ggplot 格式轴编号作为百分比不起作用

python - AttributeError: 'Series' 对象没有属性 'items'