繁体   English   中英

如何将浮点数附加到python中的列表

[英]how to append float numbers to a list in python

我正在尝试将一个图像与另一个文件的所有图像进行比较,获得差异百分比并打印最小差异百分比的文件名....如果我尝试将输出差异附加到列表中...我得到一个错误提示“浮点值不能被迭代” ....这是我到目前为止所做的..

from itertools import izip
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
import math
res = 0
def take_and_save_picture(im_save):
  '''Take a picture and save it

  Args:
    im_save: filepath where the image should be stored
  '''
  camera_port = 0
  ramp_frames = 30
  cap = cv2.VideoCapture(camera_port)
  def get_image():
   retval, im = cap.read()
   return im

  for i in xrange(ramp_frames):
   temp = get_image()

  print("Taking image...")
  # Take the actual image we want to keep
  camera_capture = get_image()

  #im_save_tmp = im_save + '.jpg'
  im_save_tmp = im_save 

  # A nice feature of the imwrite method is that it will automatically choose the
  # correct format based on the file extension you provide. Convenient!
  cv2.imwrite(im_save_tmp, camera_capture)

  # You'll want to release the camera, otherwise you won't be able to create a new
  # capture object until your script exits
  # del(cap)

  img1 = cv2.imread(im_save_tmp, 0)

  edges = cv2.Canny(img1, 100, 200)
  cv2.imwrite(im_save, edges)
  cv2.waitKey(0)
  cv2.destroyAllWindows()


def re(path1,path2):



    #path1 = raw_input("Enter the path1:")
    #path2 = raw_input("Enter the path2:")
 i2= Image.open(path2)   
 listing = os.listdir(path1)    
 for file in listing:
   i1 = Image.open(path1 + file)    
   assert i1.mode == i2.mode, "Different kinds of images."
   assert i1.size == i2.size, "Different sizes."

   pairs = izip(i1.getdata(), i2.getdata())
   if len(i1.getbands()) == 1:
    # for gray-scale jpegs
     dif = sum(abs(p1-p2) for p1,p2 in pairs)
   else:
     dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

   ncomponents = i1.size[0] * i1.size[1] * 3
   res = (dif / 255.0 * 100) / ncomponents
   print "Difference (percentage):", res


def main():
  capture_img = "/Users/Me/Documents/python programs/New/pro.png"
  #img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
  take_and_save_picture(capture_img)
  path1 = "/Users/Me/Documents/python programs/New/numbers1/"    
  path2 = "/Users/Me/Documents/python programs/New/pro.png"
  re(path1,path2)

if __name__ == '__main__':
  main()

输出是差异

Difference (percentage): 2.52484809028
Difference (percentage): 2.64822048611
Difference (percentage): 2.64822048611
Difference (percentage): 3.55436197917

我在“ res”中获得的值必须存储在列表中,并且应该找到并打印最小值。...请给我一些代码... python全新...谢谢...

您的代码必须是这样的:

#######
list_dif = []

def re(path1,path2):
    #path1 = raw_input("Enter the path1:")
    #path2 = raw_input("Enter the path2:")
    i2= Image.open(path2)   
    listing = os.listdir(path1)    
    for file in listing:
        i1 = Image.open(path1 + file)    
        assert i1.mode == i2.mode, "Different kinds of images."
        assert i1.size == i2.size, "Different sizes."

        pairs = izip(i1.getdata(), i2.getdata())
        if len(i1.getbands()) == 1:
            # for gray-scale jpegs
            dif = sum(abs(p1-p2) for p1,p2 in pairs)
        else:
            dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

        ncomponents = i1.size[0] * i1.size[1] * 3

        #######
        for n in range(ncomponents): 
            res = (dif / 255.0 * 100) / (ncomponents + 1)
            list_dif.append(res)

        print "Difference (percentage):", list_dif

像这样吗

def re(path1,path2):
    #path1 = raw_input("Enter the path1:")
    #path2 = raw_input("Enter the path2:")
    i2= Image.open(path2)   
    listing = os.listdir(path1)  
    res = []  
    for file in listing:
        i1 = Image.open(path1 + file)    
        assert i1.mode == i2.mode, "Different kinds of images."
        assert i1.size == i2.size, "Different sizes."

        pairs = izip(i1.getdata(), i2.getdata())
        if len(i1.getbands()) == 1:
            # for gray-scale jpegs
            dif = sum(abs(p1-p2) for p1,p2 in pairs)
        else:
            dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

        ncomponents = i1.size[0] * i1.size[1] * 3
        res.append((dif / 255.0 * 100) / ncomponents)
        print "Difference (percentage):", res
    minimum = min(res)  # Find minimum value in res
    print(minimum)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM