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)

本回答被提问者和网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值