截取示波器网络图片
■ 问题由来
手边有一台相对比较古老的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()
- 1
- 2
- 3
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]
- 1
- 2
- 3
需要说明的是,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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
※ 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
#============================================================
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
#!/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
#============================================================
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/107464726
- 点赞
- 收藏
- 关注作者
评论(0)