python - 在while循环中完成while循环后脚本冻结(oops)
问题描述
所以我试图获取图像中每个像素的 rgb 值,在它获取第一行的所有值之后,有什么想法吗?
脚本:
image = input("image:")
im = Image.open(image)
pix = im.load()
width, height = im.size
x = 0
y = 0
#for each pixel in the Y
while (y < height):
# for each pixel in the X
while (x < width):
print pix[x,y]
x = x + 1
y = y + 1
解决方案
初始化 x 和 y 值的方式是问题所在。X 应在第二个 while 循环之前立即初始化回零,以便为下一行的宽度重新开始计数。就像是:
x = 0
y = 0
#for each pixel in the Y
while (y < height):
# for each pixel in the X
x = 0 #start counting again for the next row
while (x < width):
print pix[x,y]
x = x + 1
y = y + 1
您的循环冻结,因为在第一行的末尾, x=width 并且您忘记将其重置为零以进行第一个 while 循环的第二次迭代。
推荐阅读
- javascript - 带有 laravel 的甜蜜警报
- java - JDK-13 Surefire 插件不支持的类文件主要版本
- python - 如何在pycharm python上从3到6按钮相乘和求和
- html - 我想在链接按钮上停止页面重新加载点击下载
- python - 当我的主数据库是 Mongo 时,Django 访问 Admin 问题
- javascript - 如何使用选项 jQuery 隐藏 select2
- node.js - 如何在正则表达式中使用 request.query
- javascript - 如何在一个对象或元组中将两个列表放在一起?
- java - Ignite ClusterNode.consistendId() 返回的值与配置中定义的值不同
- oauth-2.0 - v2 API 中的 LinkedIn 公司提要/更新