【问题标题】:Why is this code producing a different value in a VM?为什么这段代码会在 VM 中产生不同的值?
【发布时间】:2015-10-06 05:38:43
【问题描述】:

基本上我有这些 tif 图像,我需要递归并读取像素数据以确定图像中的像素是否是融化的冰。这是通过脚本中设置的阈值确定的。这被配置为能够显示年总熔化值和每个月。它在我自己的机器上运行良好,但我需要在 Linux VM 上远程运行它。它有效,但它产生的总数正好比它应该和蜜蜂产生的数量大 71146。

这是完成大部分处理并最终导致我相信的问题的 sn-p。

for file in os.listdir(current): 
    if os.path.exists(file):
        if file.endswith(".tif"): 
            fname = os.path.splitext(file)[0]
            day = fname[4:7] 
            im = Image.open(file)
            for x in range(0,60):
                for y in range(0,109):
                    p = round(im.getpixel((x,y)), 4) 
                    if p >= threshold:
                        combined = str(x) + "-" + str(y) 
                        if combined not in coords: 
                            melt += 1
                            coords.append( combined )
            totalmelt.append( melt ) 

然后将 totalmelt 相加得到年值:

total = sum(totalmelt)

阈值之前已设置如下:

threshold = float(-0.0158)

我觉得我错过了一些明显的东西。自从我玩 Python 以来已经有一段时间了……我现在从 C++ 过来。感谢您提供的任何解决方案!

【问题讨论】:

  • 分解出一个函数以获取单个图像的熔体。它应该清楚问题是什么。数据的局部性使代码更容易理解。

标签: python python-3.x virtual-machine


【解决方案1】:

您需要在内部循环之前将melt 重置为0

melt = 0
for x in range(0,60):
    for y in range(0,109):
       ...
       melt += 1
totalmelt.append(melt)

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多