跳至主要內容
常用的js代码片段
function getSelectedText() {
	if (window.getSelection) { // This technique is the most likely to be standardized.         
		// getSelection() returns a Selection object, which we do not document.        
		return window.getSelection()
			.toString();
	} else if (document.getSelection) {
		// This is an older, simpler technique that returns a string        
		return document.getSelection();
	} else if (document.selection) {
		// This is the IE-specific technique.         
		// We do not document the IE selection property or TextRange objects.         
		return document.selection.createRange()
			.text;
	}
}

爱喝水的木子...大约 2 分钟codejavascriptcode
docker安装es及kibana
os:ubuntu22.04-lts

安装docker

sudo apt install docker.io
# 配置docker加速
{
    "registry-mirrors": [
        "https://dockerproxy.com",
        "https://mirror.baidubce.com",
        "https://docker.m.daocloud.io",
        "https://docker.nju.edu.cn",
        "https://docker.mirrors.sjtug.sjtu.edu.cn",
        "https://registry.docker-cn.com",
        "https://mirror.ccs.tencentyun.com",
        "http://hub-mirror.c.163.com"
    ]
}

爱喝水的木子...大约 1 分钟codeelasticsearchkibana
高版本labelme格式转换为低版本格式
import os
import json
deatil_data=r''
save_path=""
for i in os.listdir(deatil_data):
    if not i.endswith('json'):
        continue
    temp_data=os.path.join(deatil_data,i)
    print(temp_data)
    with open(temp_data,"r",encoding='utf8')as f:
        data=json.loads(f.read())
    template_data = {
        "version": "3.16.7",
        "flags": {},
        "shapes": [],
        "lineColor": [
            0,
            255,
            0,
            128
        ],
        "fillColor": [
            255,
            0,
            0,
            128
        ],
        "imagePath": data['imagePath'],
        "imageHeight": data['imageHeight'],
        "imageWidth": data['imageWidth'],
        "imageData": data['imageData'],
    } 
    shapes=[]
    for j in data['shapes']:
        dict_point={
                "label":j['label'],
                "line_color":None,
                "fill_color":None,
                "points":j['points'],
                "shape_type":j['shape_type'],
                "flags":{}
            }

        shapes.append(dict_point)
    template_data['shapes']=shapes
    with open(temp_data,"w",encoding='utf-8')as ff:
        ff.write(json.dumps(template_data,ensure_ascii=False,indent=4))


爱喝水的木子...小于 1 分钟codepythonlabelme
关于m4a文件转成wav文件

m4a文件转为16bit,单声道,采样率为48kHz

for i in *.m4a; do ffmpeg -i "$i" -acodec pcm_s16le -ac 1 -ar 48000 "${i%}.wav"; done
-acodec pcm_s16le:16bit
-ac 1:单声道
-ar 48000:48kHZ

爱喝水的木子...小于 1 分钟codeffmpegpythoncode
压缩图片
import os
from PIL import Image


# 获取图片文件大小
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 拼接输出文件地址
def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile


# 压缩图片方法(配置即可用_1007)
def compress_image(infile, outfile='', mb=600, step=10, quality=80):
    # 判断该张图片是否压缩
    if os.path.exists(outfile):
        return
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        return infile
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)


# 扩展压缩
a = r"C:\Users\DM\Desktop\图片过长\新建文件夹\1"
b = r"C:\Users\DM\Desktop\图片过长\新建文件夹\2"
for i in os.listdir(a):
    in_file = os.path.join(a, i)
    out_file = os.path.join(b, i)
    print(in_file, out_file)
    compress_image(in_file, out_file)

爱喝水的木子...小于 1 分钟codepython压缩图片
图片大小压缩
from PIL import Image
import os
from multiprocessing.pool import ThreadPool

# 获取图片文件大小
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 拼接输出文件地址
def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile


# 压缩文件到指定大小
def compress_image(infile, outfile='', mb=500, step=10, quality=80):
    # 判断该张图片是否压缩
    if os.path.exists(outfile):
        return
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        shutil.copyfile(infile,outfile)
        return f"cp:{infile}>{outfile}"
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)


# 默认600k,但是可能不会到600K,有的压缩不到
path_a = r"文件路径"
path_b = r"文件路径"
# 使用线程池提高速度
s = ThreadPool(12)
for i in os.listdir(path_a):
    infile = os.path.join(path_a, i)
    outfile = os.path.join(path_b, i)
    # if os.path.exists(outfile):
    #     continue
    print(infile)
    s.apply_async(compress_image, args=(infile, outfile))
    # compress_image(infile,outfile)
s.close()
s.join()


爱喝水的木子...小于 1 分钟codepython图片压缩
多张图片合成一张
import cv2 as cv
import numpy as np
# 读入图片
src = cv.imread('test.jpg')
# 调用cv.putText()添加文字
text = "Your are so beautiful!"
AddText = src.copy()
cv.putText(AddText, text, (200, 100), cv.FONT_HERSHEY_COMPLEX, 2.0, (100, 200, 200), 5)
# 将原图片和添加文字后的图片拼接起来
res = np.hstack([src, AddText])
# 显示拼接后的图片
cv.imshow('text', res)
cv.waitKey()
cv.destroyAllWindows()

爱喝水的木子...小于 1 分钟codepython
逛github遇到喜欢的项目但是没时间去看的解决方法

逛GitHub遇到喜欢的项目但是暂时又不想看可以clone

flask demo

from flask import *
import os
import uuid

from pip import main

app = Flask(__name__)


# post请求提交的数据都在json中
@app.route('/github', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        github_url = request.form.get('url', '')
        github_name = request.form.get('name', '')
        with open("download.txt", "a") as f:
            f.write(github_url + "\t" + github_name + "\n")
        if not github_name:
            github_name = uuid.uuid1().hex
        call = "git clone {} {}".format(
            github_url,
            os.path.join("/data/github", github_name.replace(" ", "_")))
        print(call)
        os.system(call)
        return jsonify({'msg': 'ok', "status": "created", "name": github_name})
    else:
        return jsonify({'msg': 'error'})


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

爱喝水的木子...小于 1 分钟codegithubsyncpython