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

时间:2016-03-02 10:23:23

标签: python loops image-processing

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

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来说是全新的...谢谢...

2 个答案:

答案 0 :(得分:1)

You're code must be like this:

#######
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

答案 1 :(得分:0)

这样的东西?

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)