笔记是越记越多,记录也越来越混乱,重复的记录,错误的代码也记录上,时间一久就忘记了当时是怎样一个场景面临的问题。回过头来曾经一笔一笔记录的代码,最开始从
#这个API可以提供交互面板选择元素
*createmarkpanel comps 1 "Select entity"
#可以获取到选择的ID
hm_getmark comps 1
开始记录,再到现在记录的关于图像识别的代码,还记得以前同事给我讲冒泡排序的时候,为了理解下来将每一行代码都写上注释。真是见证了半路出家的自己一步一步的成长。
在整理过程中,遇到以前记录有意思的函数。
proc map {lambda list} {
set result {}
foreach item $list {
lappend result [apply $lambda $item]
}
return $result
}
#匿名函数,模拟lambda函数
map {x {return [string length $x]:$x}} {a bb ccc dddd}
#1:a 2:bb 3:ccc 4:dddd
map {x {expr {$x**2 + 3*$x - 2}}} {-4 -3 -2 -1 0 1 2 3 4}
#2 -2 -4 -4 -2 2 8 16 26
#range函数,模拟python 的range函数功能
proc range {{start 0} {end 10} {step 1}} {
set res [list]
for {set i $start} {$i < $end} {incr i $step} {
lappend res $i
}
return $res
}
set a [range 3 18 2]
#另一种写法
# Range函数
proc myRange {args} {
set start 0
set stop [lindex $args 0]
set step 1
if {[llength $args] == 2} {
set start [lindex $args 0]
set stop [lindex $args 1]
} elseif {[llength $args] == 3} {
set start [lindex $args 0]
set stop [lindex $args 1]
set step [lindex $args 2]
}
set result {}
set value $start
while {$value < $stop} {
lappend result $value
set value [expr {$value + $step}]
}
return $result
}
set result1 [myRange 5]
set result2 [myRange 1 10]
set result3 [myRange 1 10 2]
# 将单个图片白色背景替换为透明,用于解决Hyperview无法截取透明背景图片
from PIL import Image
def make_image_transparent(image_path, output_path):
img = Image.open(image_path)
img = img.convert("RGBA")
datas = img.getdata()
new_data = []
for item in datas:
if item[0] in list(range(200, 256)):
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
img.putdata(new_data)
img.save(output_path, "PNG")
# 使用函数
make_image_transparent("ALLInfo_1692689722_64e4653a-5eef-5eef-5eef-005eef_0_morph.png", "ALLInfo_1692689722_64e4653a-5eef-5eef-5eef-005eef_0_morphoutput.png")
# 将文件夹内的白色背景图片替换为透明
import os
from PIL import Image
def make_image_transparent(image_path, output_path):
img = Image.open(image_path)
img = img.convert("RGBA")
datas = img.getdata()
new_data = []
for item in datas:
if item[0] in list(range(200, 256)):
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
img.putdata(new_data)
img.save(output_path, "PNG")
def process_directory(directory):
for filename in os.listdir(directory):
if filename.endswith(".png"):
base_filename = os.path.splitext(filename)[0]
output_filename = f"{base_filename}_Output.png"
make_image_transparent(os.path.join(directory, filename), os.path.join(directory, output_filename))
# 使用函数
process_directory("output")