大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
■ 问题由来
手边有一台相对比较古老的Tektronix的示波器TDS3054D示波器,四通道的。它可以通过联网获得波形的图片。对于记录观察到的波形相对比较方便。
▲ 示波器及其联网获得屏幕图片
在截取示波器波形窗口的过程中,由于上面出现红色的字体( HOME:TDS3054B AA (192.168.0.101)
)的影响,经常使得截取图片出现不完整,因此希望通过软件(PYTHON 程序)自动完成精细截取的过程。
▲ 截取示波器波形窗口
TDS3054B的显示模式包括两种:
- 普通显示模式: 示波器的主要参数和测量参数显示在波形窗口外边;
- 测量显示模式: 示波器的参数和波形测量参数显示在波形窗口内部;
这两种模式可以通过示波器前面板的“MEASURE”按钮, 和显示液晶屏下面的“MENU”窗口来进行切换。
▲ 两种显示模式|左:普通显示模式;右:测量参数显示模式
因此,需要对上述两种情况下粗略截取的屏幕图片进行精细截取。
01编程方法
1.基本方案
使用 TEASOFT 软件完成对示波器网页粗截取,将图片存储在Windows的 剪切板 。然后使用PYTHON程序完成对图片的精细裁剪。
2.PYTHON获取剪切板上的图片
根据 Grab Image From clipboard in Python with Pilow 中的介绍,使用PIL中的ImageGrab中的grabclipboard命令获得Clipboard中的图片数据。
from PIL import ImageGrab
img = ImageGrab.grabclipboard()
imgdata = img.getdata()
3.对图片进行精确裁剪
使用img.getdata()获取图片的RGB阵列,通过sum,reshape对图片转换成灰度阵列。
imggray = sum(array(img.getdata()), axis=1).reshape(img.size[::-1])/3
imgcolumn = sum(imggray, axis=1) / img.size[1]
imgrow = sum(imggray, axis=0) / img.size[0]
需要说明的是,img.size()给出的是图片的(列数、行数),但图片的灰度数据是按照“从上往下(按照列)进行扫描,再从左到右(按照行)进行扫描”。所有在reshape()中使用了img.size[::-1]来调整img.size的顺序。
下面给出了测试图片(左)对应的数据尺寸,以及对应的imgcolumn, imgrow的曲线。
对于普通显示模式的图片,只需要在粗剪切图上,确定最上面的空隙和最右边的空隙位置即可。
imgcolumn = sum(imggray, axis=1) / img.size[0]
imgrow = sum(imggray, axis=0) / img.size[1]
imgleftid = int(img.size[0] / 4)
imgtopid = int(img.size[1] / 4)
imgrightid = img.size[0] - imgleftid
imgbottomid = img.size[1] - imgtopid
imgleft = imgrow[0:imgleftid]
imgright = imgrow[imgrightid:]
imgtop = imgcolumn[0:imgtopid]
imgbottom = imgcolumn[imgbottomid:]
imgleftside = list(imgleft).index(min(imgleft)) - 32
imgrightside = list(imgright).index(min(imgright)) + imgrightid + 5
imgtopside = list(imgtop).index(min(imgtop)) - 30
imgbottomside = list(imgbottom).index(min(imgbottom)) + imgbottomid + 32
if imgtopside < 0: imgtopside = 0
if imgleftside < 0: imgleftside = 0
if imgrightside > img.size[0]: imgrightside = img.size[0]
if imgbottomside > img.size[1]: imgbottomside = img.size[1]
imgcrop = img.crop([imgleftside, imgtopside, imgrightside, imgbottomside])
imgcrop.show()
4.PYTHON拷贝图片到剪切板
from io import BytesIO
import win32clipboard
#------------------------------------------------------------
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
output = BytesIO()
imgcrop.convert('RGB').save(output, 'BMP')
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
5.嵌入CSDNPP
在原来的csdnpp命令中,增加命令参数的判断:
- ** csdnpp #arg** : 先调用oscimgcrop: 适用于普通显示模式
- csdnpp @arg: 先调用 oscimgcrop 4 : 适用于参数测量模式
#------------------------------------------------------------
if len(sys.argv) > 1:
if sys.argv[1][0] == '#':
sys.argv[1] = sys.argv[1][1:]
tspexecutepythoncmd('oscimgcrop')
time.sleep(2)
elif sys.argv[1][0] == '@':
sys.argv[1] = sys.argv[1][1:]
tspexecutepythoncmd('oscimgcrop 4')
time.sleep(2)
※ PYTHOH 命令程序
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY -- by Dr. ZhuoQing 2020-07-20
#
# Note:
#============================================================
from headm import *
from PIL import ImageGrab
from io import BytesIO
import win32clipboard
#------------------------------------------------------------
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
#------------------------------------------------------------
bottomsideappend = 32
if len(sys.argv) > 1:
bottomsideappend = int(sys.argv[1])
#------------------------------------------------------------
tspbeep(1800, 100)
img = ImageGrab.grabclipboard()
imgdata = img.getdata()
printf(img.size)
tspbeep(1200, 50)
#img.show()
imggray = sum(array(img.getdata()), axis=1).reshape(img.size[::-1])/3
imgcolumn = sum(imggray, axis=1) / img.size[0]
imgrow = sum(imggray, axis=0) / img.size[1]
imgleftid = int(img.size[0] / 4)
imgtopid = int(img.size[1] / 4)
imgrightid = img.size[0] - imgleftid
imgbottomid = img.size[1] - imgtopid
imgleft = imgrow[0:imgleftid]
imgright = imgrow[imgrightid:]
imgtop = imgcolumn[0:imgtopid]
imgbottom = imgcolumn[imgbottomid:]
imgleftside = list(imgleft).index(min(imgleft)) - 32
imgrightside = list(imgright).index(min(imgright)) + imgrightid + 5
imgtopside = list(imgtop).index(min(imgtop)) - 30
imgbottomside = list(imgbottom).index(min(imgbottom)) + imgbottomid + bottomsideappend
if imgtopside < 0: imgtopside = 0
if imgleftside < 0: imgleftside = 0
if imgrightside > img.size[0]: imgrightside = img.size[0]
if imgbottomside > img.size[1]: imgbottomside = img.size[1]
imgcrop = img.crop([imgleftside, imgtopside, imgrightside, imgbottomside])
output = BytesIO()
imgcrop.convert('RGB').save(output, 'BMP')
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
printf('\a')
#------------------------------------------------------------
# END OF FILE : TEST1.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# CSDNPP.PY -- by Dr. ZhuoQing 2020-02-04
#
# Note:
#============================================================
from head import *
csdn_window = '写文章-CSDN博客'
#------------------------------------------------------------
if len(sys.argv) > 1:
if sys.argv[1][0] == '#':
sys.argv[1] = sys.argv[1][1:]
tspexecutepythoncmd('oscimgcrop')
time.sleep(2)
elif sys.argv[1][0] == '@':
sys.argv[1] = sys.argv[1][1:]
tspexecutepythoncmd('oscimgcrop 4')
time.sleep(2)
tspfocuswindow(csdn_window)
#------------------------------------------------------------
tspsendwindowkey(csdn_window, 'v', control=1, noreturn=1)
time.sleep(1)
for i in range(10):
time.sleep(.25)
printf('\a')
readdata = tspread()
if readdata[7] != 0: continue
if readdata[8] != 0: continue
if readdata[9] != 0: continue
break
#------------------------------------------------------------
for i in range(5):
time.sleep(.25)
readdata = tspread()
if readdata[7] != 0: exit()
if readdata[8] != 0: exit()
if readdata[9] != 0: exit()
tspsendwindowkey(csdn_window, 'ac', control=1, noreturn=1)
# tspsendwindowkey(csdn_window, 'c', control=1)
if clipboard.paste().find('在这里插入图片描述') >= 0:
tspbeep(1500, 200)
time.sleep(0.25)
break
printf('\a')
#------------------------------------------------------------
#tspsendwindowkey(csdn_window, 'a', control=1)
#tspsendwindowkey(csdn_window, 'c', control=1)
picurllink = ''
pastestr = clipboard.paste().split('![在这里插入图片描述](')
if len(pastestr) < 2:
printf("Can not find the ![Insert picture] discriptor !\a")
exit()
for i in range(2):
tspsendwindowkey(csdn_window, '%c'%M1_END, vk=1)
tspsendwindowkey(csdn_window, 'v', control=1)
time.sleep(0.25)
printf('\a')
time.sleep(.25)
tspsendwindowkey(csdn_window, 'a', control=1)
tspsendwindowkey(csdn_window, 'c', control=1)
pastestr = clipboard.paste().split('![在这里插入图片描述](')
if len(pastestr) >= 2: break
if len(pastestr) < 2:
printf("Can not find the [Insert picture] discriptor !\a")
exit()
#------------------------------------------------------------
tspsendwindowkey(csdn_window, 'z', control=1, noreturn=1)
#------------------------------------------------------------
pastestr = pastestr[1].split(')')[0]
printf(pastestr)
pastestr = pastestr.split('?')[0]
#------------------------------------------------------------
widthstr = ''
#------------------------------------------------------------
if len(sys.argv) <= 1:
# insertstr = tspi nput().rstrip('\r').split(' ')
# insertstring = insertstr[0]
insertstr = ''
insertstring = ''
if len(insertstr) > 1:
if insertstring[-1].isdigit:
insertstring = ' '.join(insertstr[:-1])
widthstr = '=%sx'%insertstr[-1]
else: insertstring = ' '.join(insertstr)
else:
if len(sys.argv) == 2:
insertstring = sys.argv[1]
# if insertstring[0] == '=':
# insertstring = ''
# widthstr = sys.argv[1] + 'x'
if insertstring.isdigit() or insertstring[0] == '=':
insertstring = ''
widthstr = '=' + sys.argv[1] + 'x'
elif len(sys.argv) == 3:
if sys.argv[2].isdigit():
insertstring = sys.argv[1]
widthstr = '=%sx'%sys.argv[2]
else: insertstring = ' '.join(sys.argv[1:3])
else:
if sys.argv[-1].isdigit():
insertstring = ' '.join(sys.argv[1:-1])
widthstr = '=%sx'%sys.argv[-1]
else: insertstring = ' '.join(sys.argv[1:])
#------------------------------------------------------------
if len(insertstring) == 0:
insertstring = '![%s](%s#pic_center %s)\r\n\r\n'%(insertstring, pastestr, widthstr)
clipboard.copy(insertstring)
tspsendwindowkey(csdn_window, 'v', control=1, noreturn=1)
exit()
if insertstring[-1] == '.':
insertstring = '[**%s**](%s)'%(insertstring[0:-1], pastestr)
clipboard.copy(insertstring)
tspsendwindowkey(csdn_window, 'v', control=1, noreturn=1)
exit()
if len(insertstring) >= 2:
if insertstring[-1:] == '。':
insertstring = '[**%s**](%s)'%(insertstring[0:-1], pastestr)
clipboard.copy(insertstring)
tspsendwindowkey(csdn_window, 'v', control=1, noreturn=1)
exit()
#------------------------------------------------------------
pic_pos_str = 'pic_center'
if len(insertstring) >= 2:
if insertstring[0:2] == '[ ':
insertstring = insertstring[2:]
pic_pos_str = 'pic_left'
elif insertstring[0:2] == '[[':
insertstring = insertstring[2:]
pic_pos_str = 'pic_left'
elif insertstring[0:2] == '] ':
insertstring = insertstring[2:]
pic_pos_str = 'pic_right'
elif insertstring[0:2] == ']]':
insertstring = insertstring[2:]
pic_pos_str = 'pic_right'
elif insertstring[0:2] == '[]':
insertstring = insertstring[2:]
pic_pos_str = 'pic_center'
elif insertstring[0:2] == '][':
insertstring = insertstring[2:]
pic_pos_str = 'pic_center'
#------------------------------------------------------------
atid = insertstring.find('@')
if atid >= 0:
picurllink = insertstring[atid+1:]
insertstring = insertstring[:atid]
else:
atid = insertstring.find('https://')
if atid < 0: atid = insertstring.find('http://')
if atid >= 0:
picurllink = insertstring[atid:]
insertstring = insertstring[:atid]
#------------------------------------------------------------
insertsect = insertstring.split('||')
morestring = ""
if len(insertsect) > 1:
insertstring = insertsect[0]
for s in insertsect[1:]:
if pic_pos_str == 'pic_left':
morestring = morestring + '\r\n<sup>``%s``</sup>'%(s.strip(' '))
else:
morestring = morestring + '<center><sup>``%s``</sup></center>'%(s.strip(' '))
if len(insertstring) > 0:
insertstring = '▲ ' + insertstring
if pic_pos_str == 'pic_left':
insertstring = '![%s](%s#%s %s)\r\n<sup>``%s``</sup>%s\r\n'%(insertstring, pastestr, pic_pos_str, widthstr, insertstring, morestring)
else:
insertstring = '![%s](%s#%s %s)<center><sup>``%s``</sup></center>%s\r\n'%(insertstring, pastestr, pic_pos_str, widthstr, insertstring, morestring)
else:
insertstring = '![%s](%s#%s %s)\r\n'%(insertstring, pastestr, pic_pos_str, widthstr)
#------------------------------------------------------------
if len(picurllink) > 0:
insertstring = '[%s](%s)'%(insertstring, picurllink)
#------------------------------------------------------------
clipboard.copy(insertstring)
tspsendwindowkey(csdn_window, 'v', control=1,noreturn=1)
time.sleep(.2)
clipboard.copy('')
tspfocuswindow(csdn_window)
#------------------------------------------------------------
# END OF FILE : CSDNPP.PY
#============================================================
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/182908.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...