a method to make some handy tools with python

In my working of computer, there are a lot of simple jobs that are frequently repeated. I tried to find a way to make these job been processed easily.

Method 1: The method is writing a script to do the job, and executing the script by utools extension utools-ScriptRunner.

Method 2: writing a script to do the job, giving the script with extension .pyy, and associating files with pyy extension to python.exe to quick lanuch the script. The reason to do these is I want to always open .py files with vscode. Then double clicking the .pyy file or searching the .pyy file in everything pressing enter on the expected item, will both trigger the script.

Example:

# csv2tiff.pyy
import re
import clipboard
import os
import cv2
import numpy as np

# clipboard.copy("abc")  # now the clipboard content will be string "abc"
# text = clipboard.paste()  # text will have the content of clipboard

text = clipboard.paste()
lsA = text.splitlines()
# lsB = []
# for line in lsA:
#     line = re.sub(r"\\", "/", line)
#     theMatch = re.search("^\"*(.*?)\"*$", line)
#     # print(theMatch)
#     line = f'"{theMatch.groups()[0]}"'
#     lsB.append(line)
# text = "\n".join(lsB)
# clipboard.copy(text)

# print(lsA)
for idx, ff in enumerate(lsA):
    print(f"idx {idx:6d} / {len(lsA)} : {ff}")
answer = input("are you sure to convert these files?\n`ctrl + C` to exit.")

for idx, ff in enumerate(lsA):
    print(f"idx {idx:6d} / {len(lsA)} : {ff}")
    img = np.loadtxt(ff, delimiter=",")
    img = img.astype(np.float32)
    cv2.imwrite(ff + ".tiff", img)

os.system("pause")

reference

wenjinghuan999/utools-ScriptRunner: utools插件,可将监视目录下的所有python脚本文件自动加入关键字列表: https://github.com/wenjinghuan999/utools-ScriptRunner

posted on 2024-07-24 23:39  yusisc  阅读(3)  评论(0编辑  收藏  举报

导航