python测量线长

模块:

matplotlib
PIL
time
math
win32con
threading

代码:

mian.py:

from PIL import Image
import matplotlib.pyplot as plt
from image_show import show_your_image
from image_show import get_pixel
from image_show import get_line_long

#show_your_image(image,if_gray=) 展示图片






figure=plt.figure('get line long')
image= Image.open("line_try.jpg")
image_gray = image.convert('L')#图片变灰
#pixel_list=get_pixel(image=image_gray)
place_list=show_your_image(image=image, if_gray=True, figure=figure)
print(place_list)
line_len=get_line_long(place_list)
print(line_len)

#print(pixel_list)

image_show.py:

import matplotlib.pyplot as plt#该模块只用于展示图片
from PIL import Image
import time
from hot_key import Hotkey
import math

RUN=False #用来传递运行一次的参数
EXIT = False #用来传递退出的参数
hotkey = Hotkey()
hotkey.start()
hotkey.gethotkey()#查看热键是否被按 获得hotkey.hotkey1
x_place=0
y_place=0
place_list=[]


def show_your_image(image,figure,if_gray=False,):
    if if_gray==True:
        plt.imshow(image,cmap='gray') #cmap即colormap,颜色映射
    else:
        plt.imshow(image)
    plt.axis('off')  #关闭网格线

    figure.canvas.mpl_connect('motion_notify_event', call_back)
    plt.show()
    return place_list


def get_pixel(image,if_print='True'):
    '''是通过一列一列获取的 pixel_list[0]是第一列的像素'''

    time_read_pixel_start=time.time()
    image_width,image_height=image.size
    pixel_list=[]
    for i in range(image_width):
        column_list=[]
        for j in range(image_height):
            column_list.append(image.getpixel((i,j)))
        pixel_list.append(column_list)
    time_read_pixel_over=time.time()
    print('读取,解析像素用时:',time_read_pixel_over-time_read_pixel_start)
    return pixel_list

def show_new_image(image,pixel_list):
    image_new=Image.new('RGB',image.size,'white')
    for i in range(image.size[0]):
        for j in range(image.size[1]):
            image_new.putpixel((i, j), pixel_list[i][j])
    show_your_image(image=image_new,if_gray=True)

def get_line_long(place_list):
    len_list=[]
    for i in range(len(place_list)-1):
        x1=place_list[i][0]
        y1=place_list[i][1]
        x2=place_list[i+1][0]
        y2=place_list[i+1][1]
        len_x_to_y=math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
        len_x_to_y="%.3f" % (len_x_to_y)
        len_list.append(len_x_to_y)
    len_all=0
    for i in len_list:
        len_all+=float(i)
    print(len_list)
    return  len_all



def call_back(event):
    global x_place
    global y_place
    hotkey.gethotkey()

    try:
        x_place=event.xdata
        y_place=event.ydata
        if hotkey.hotkey1 == True:
            list1=[x_place,y_place]
            print(x_place,'   ',y_place)
            place_list.append(list1)
    except:
        pass


hot_key.py:

import win32con
import ctypes
import ctypes.wintypes
import threading
RUN=False #用来传递运行一次的参数
EXIT = False #用来传递退出的参数
user32 = ctypes.windll.user32  #加载user32.dll
id1=105 #注册热键的唯一id,用来区分热键
id2=106#id1:105为F9 id2:106为F10
class Hotkey(threading.Thread):  #创建一个Thread.threading的扩展类
    #hotkey=EXIT
    def gethotkey(self):
        global RUN
        #print('RUN=',RUN)
        self.hotkey1 = RUN
        RUN=False
    def run(self):
        global EXIT  #定义全局变量,这个可以在不同线程间共用。
        global RUN  #定义全局变量,这个可以在不同线程间共用。
        if not user32.RegisterHotKey(None, id1, 0, win32con.VK_F9):   # 注册快捷键F9并判断是否成功,该热键用于执行一次需要执行的内容。
            print("Unable to register id", id1) # 返回一个错误信息
        if not user32.RegisterHotKey(None, id2, 0, win32con.VK_F10):   # 注册快捷键F10并判断是否成功,该热键用于结束程序,且最好这么结束,否则影响下一次注册热键。
            print("Unable to register id", id2)
        #以下为检测热键是否被按下,并在最后释放快捷键
        try:
            msg = ctypes.wintypes.MSG()
            while True:
                if user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
                    if msg.message == win32con.WM_HOTKEY:
                        if msg.wParam == id1:
                            RUN = True
                        elif msg.wParam == id2:
                            EXIT=True
                            return
                    user32.TranslateMessage(ctypes.byref(msg))
                    user32.DispatchMessageA(ctypes.byref(msg))
        finally:
            user32.UnregisterHotKey(None, id1)#必须得释放热键,否则下次就会注册失败,所以当程序异常退出,没有释放热键,
            user32.UnregisterHotKey(None, id2)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值