當前位置:編程學習大全網 - 編程語言 - 使用Python 制作對比圖片相似度的程序

使用Python 制作對比圖片相似度的程序

import media

def red_average(pic):

'''Return an integer that represents the average red of the picture.

'''

total=0

for pixel in pic:

total = total + media.get_red(pixel)

red_average = total / (media.get_width(pic)*media.get_height(pic))

return red_average

def green_average(pic):

'''Return an integer that represents the average green of the picture

'''

total = 0

for pixel in pic:

total = total + media.get_green(pixel)

green_average = total / (media.get_width(pic)*media.get_height(pic))

return green_average

def blue_average(pic):

'''Return an integer that represents the average blue of the picture

'''

total = 0

for pixel in pic:

total = total + media.get_blue(pixel)

blue_average = total / (media.get_width(pic)*media.get_height(pic))

return blue_average

def scale_red(pic, value):

'''Return the picture that the average of the red is value which has been set.

'''

averaged = red_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_red = min(255, int(factor * media.get_red(pixel)))

media.set_red(pixel,new_red)

return pic

def scale_green(pic, value):

'''Return the picture that the average of the green is value which has been set.

'''

averaged = green_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_green = min(255, int(factor * media.get_green(pixel)))

media.set_green(pixel,new_green)

return pic

def scale_blue(pic, value):

'''Return the picture that the average of the blue is value which has been set.

'''

averaged = blue_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_blue = min(255, int(factor * media.get_blue(pixel)))

media.set_blue(pixel,new_blue)

return pic

def expand_height(pic, factor):

'''Return a newpicture that has been vertically stretched by the factor which has been set.

'''

new_width = pic.get_width()

new_height = pic.get_height()*factor

newpic = media.create_pic(new_width, new_height, media.black)

for pixel in pic:

x = media.get_x(pixel)

y = media.get_y(pixel)

newpixel = media.get_pixel(newpic, x, y*factor)

for newpixel in newpic:

new_red = media.get_red(pixel)

new_green = media.get_green(pixel)

new_blue = media.get_blue(pixel)

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def expand_width(pic,factor):

'''Return a newpicture that has been horizontally stretched by the factor which has been set.

'''

new_width = pic.get_width() * factor

new_height = pic.get_height()

newpic = media.create_pic(new_width,new_height,media.black)

for newpixel in newpic:

x = media.get_x(newpixel)

y = media.get_y(newpixel)

pixel = media.get_pixel(pic,x / factor, y)

new_red = media.get_red(pixel)

new_green = media.get_green(pixel)

new_blue = media.get_blue(pixel)

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def reduce_height(pic, factor):

'''return a new pic that has been compressed vertically by the factor which has been set

'''

# Create a new, all-black pic with the appropriate new height and

# old width; (all colour components are zero).

new_width = pic.get_width

new_height = (pic.get_height() - 1) / factor + 1

newpic = media.create_pic(new_width, new_height, media.black)

# Iterate through all the pixels in the original (large) image, and copy

# a portion of each pixel's colour components into the correct

# pixel position in the smaller image.

for pixel in pic:

# Find the corresponding pixel in the new pic.

x = media.get_x(pixel)

y = media.get_y(pixel)

newpixel = media.get_pixel(newpic, x, y / factor)

# Add the appropriate fraction of this pixel's colour components

# to the components of the corresponding pixel in the new pic.

new_red = newpixel.get_red()+pixel.get_red()/factor

new_green = newpixel.get_green()+pixel.get_green()/factor

new_blue = newpixel.get_blue()+pixel.get_blue()/fctor

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def reduce_width(pic,factor):

'''Return a newpic that has been horizontally compressed by the factor which has been set.

'''

new_width = (media.get_width() - 1) / factor + 1

new_height = media.get_height()

newpic = media.create_pic(new_width, new_height, media.black)

for pixel in pic:

x = media.get_x(pixel)

y = media.get_y(pixel)

new_pixel = media.get_pixel(newpic, x / factor, y)

new_red = newpixel.get_red() + pixel.get_red() / factor

new_green = newpixel.get_green() + pixel.get() / factor

new_blue = newpixel.get_blue() + pixel.get()/factor

media.set_red(newpixel, new_red)

media.set_green(newpixel, new_green)

media.set_blue(newpixel, new_blue)

return newpic

def distance(pixel1, pixel2):

red1 = media.get_red(pixel1)

green1 = media.get_green(pixel1)

blue1 = media.get_blue(pixel1)

red2 = media.get_red(pixel2)

green2 = media.get_green(pixel2)

blue2 = media.get_blue(pixel2)

sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)

return sum

def simple_difference(pic1, pic2):

for pixel in pic1:

x = media.get_x(pixel)

y = media.get_y(pixel)

pixel2 = media.get_pixel(pic2, x, y)

sum = media.distance(pixel, pixel2)

return sum

def smart_difference(pic1,pic2):

height1 = media.get_height(pic1)

height2 = media.get_height(pic2)

factorh = float(height1 / height2)

if factorh >= 1:

height1 = media.reduce_height(pic1, factorh)

else:

height2 = media.reduce_height(pic2, 1 / factorh)

width1 = media.get_width(pic1)

width2 = media.get_width(pic2)

factorw = float(width1 / width2)

if factorw >= 1:

width1 = reduce_width(pic1, factorw)

else:

width2 = reduce_width(pic2, 1 / factorw)

red1 = red_average(pic1)

green1 = green_average(pic1)

blue1 = blue_average(pic1)

red2 = media.scale_red(pic2, red1)

green2 = media.scale_green(pic2, green1)

blue2 = media.scale_blue(pic2, blue1)

#if __name__ == '__main__':

#media.show(newpic)

  • 上一篇:作為壹個安卓開發,如何去開發AR?需要具體學什麽
  • 下一篇:5G通訊 讓半導體行業重新進入壹個新起點
  • copyright 2024編程學習大全網