发布网友 发布时间:2024-09-25 19:48
共1个回答
热心网友 时间:2024-10-04 10:58
本篇文章给大家谈谈python照片对比大概多久,以及Python 图片对比对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、python 不同时间拍摄的图片如何对比差异2、python如何进行图像比对3、学会python大概要多久?4、.py文件用jupyter打开运行图片需要多长时间5、使用Python 制作对比图片相似度的程序python 不同时间拍摄的图片如何对比差异比较不同
使用PIL(Pillow library)库
安装 pip install pillow,然后直接用其中的ImageChops函数
from PIL import Imagefrom PIL import ImageChops
def compare_images(path_one, path_two, diff_save_location):
? ?"""
? ?比较图片,如果有不同则生成展示不同的图片
? ?@参数一: path_one: 第一张图片的路径
? ?@参数二: path_two: 第二张图片的路径
? ?@参数三: diff_save_location: 不同图的保存路径
? ?"""
? ?image_one = Image.open(path_one)
? ?image_two = Image.open(path_two)
? ?diff = ImageChops.difference(image_one, image_two)
? ?if diff.getbbox() is None: ? ? ? ?# 图片间没有任何不同则直接退出
? ? ? ?return
? ?else:
? ? ? ?diff.save(diff_save_location)
if __name__ == '__main__':
? ?compare_images('/path/to/瀑布.jpg', ? ? ? ? ? ? ? ? ? '/path/to/瀑布改.jpg', ? ? ? ? ? ? ? ? ? '/path/to/不同.jpg')
结果,底部的不同被显示出来了
python如何进行图像比对
import?Image
import?ImageChops
im1?=?Image.open('1.jpg')
im2?=?Image.open('2.jpg')
diff?=?ImageChops.difference(im1,?im2).getbbox()
print?a?+?b?+?'is:?'?+?str(diff)
学会python大概要多久?一周或者一个月。
如果完全靠自己自学,又是从零基础开始学习Python的情况下,按照每个人的学习和理解能力的不同,我认为大致上需要半年到一年半左右的时间。
当然了,Python学习起来还是比较简单的,如果有其他编程语言经验,入门Python还是非常快的,花1-2个月左右的时间学完基础,就可以自己编写一些小的程序练练手了,5-6个月的时间就可以上手做项目了。
从一定程度上来说,一些零基础的初学者想要利用两个月的时间掌握好Python是不太可能的,学习完Python后想要应聘相对应的工作岗位,即便是选择最快的学习方式也是很难实现的,无法快速实现就业。
.py文件用jupyter打开运行图片需要多长时间30秒到40秒左右,但是有时候程序打不开。
jupyter程序的文件是。ipynb格式,如果要引用。py文件时,首先是要导入,可以通过下面方法,如有一个pythonDemo。py文件,想使用这个文件中的函数,需要使用,importpythonDemopython文件是当作一个模块,import文件名,就是导入该模块。
如果修改了。py文件,通过上面importpythonDemo是无法对修改的内容生效,这时需要重新装载模块,方法如下:fromimpimportreload,reload(pythonDemo)。
使用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)
python照片对比大概多久的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Python 图片对比、python照片对比大概多久的信息别忘了在本站进行查找喔。