🚲 🚗 ✈️ 🚀

🚀 修改文件名

功能,打印当前目录下所有文件的文件名,并将文件名中的 ‘.’ 和 ‘-‘ 之间的字符替换成 ‘__’ (包括’.’ 和 ‘-‘ )。

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
import os
import re

# 获取指定目录下的所有文件名
def get_file_names(directory):
# 遍历目录下的所有文件,并将文件名添加到列表中
file_names = []
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
file_names.append(filename)
return file_names

# 打印所有文件名
def print_file_names(file_names):
for filename in file_names:
print(filename)

# 修改文件名
def modify_file_name(directory, old_name, new_name):
# 获取文件路径
old_path = os.path.join(directory, old_name)
new_path = os.path.join(directory, new_name)
# 重命名文件
os.rename(old_path, new_path)

# 将要遍历的目录路径替换为您所需的目录
directory = './'
# 定义正则表达式,用于匹配括号内的字符串
regex = re.compile(r'\.(.*?)\-')
# 获取所有文件名
file_names = get_file_names(directory)
# 打印所有文件名
print_file_names(file_names)

# 循环遍历并修改文件名
for i in range(len(file_names)):
# 获取原始文件名
old_name = file_names[i]
# 查找括号内的字符串
match = regex.search(old_name)
# 移除不需要的字符
new_name = old_name.replace(match.group(), '_')
# 修改文件名
modify_file_name(directory, old_name, new_name)
print(new_name)

🚀 png转jpg并修改文件名为数字序列

文件夹下的所有文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PIL import Image
import os

path = './featureimages'
files = os.listdir(path)
i = 1 // 如果重命名失败,说明文件夹内有同名的文件,可以先将这个值设置大一点,之后再调回1

# 将文件夹下的所有文件按照从 1 开始的数字命名,后缀不变
for file in files:
old_name = os.path.join(path, file)
extension = os.path.splitext(file)[1]
new_name = os.path.join(path, str(i) + extension)
print(new_name)
os.rename(old_name, new_name)
i += 1

# 将文件夹内的 png 图片转换为 jpg
for filename in os.listdir(path):
if filename.endswith('.png'):
img = Image.open(os.path.join(path, filename))
img = img.convert('RGB')
new_filename = os.path.splitext(filename)[0] + '.jpg'
img.save(os.path.join(path, new_filename))
os.remove(os.path.join(path, filename))

🚀 xlsx文件挑选特定范围的值

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
import os
import pandas as pd

title1 = 'T_P_SLC'
title2 = 'T_R_SLC'

threshold1 = 130
threshold2 = 60

xlsx_file_path = "./Xlsx/"
out_path_xlsx = './OutPut/'

# 删除输出文件夹内所有的文件
for filename in os.listdir(out_path_xlsx):
file_path = os.path.join(out_path_xlsx, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
print(f'rm {file_path}')
except Exception as e:
print(f'Failed to delete {file_path}. Reason: {e}')

# 浏览并处理目标文件夹内的文件
for filename in os.listdir(xlsx_file_path):
file_path = os.path.join(xlsx_file_path, filename)
out_file_path = os.path.join(out_path_xlsx, filename)
print(file_path)
print(out_file_path)

# 读取Excel文件
df = pd.read_excel(file_path)

# 将文本格式转换为整数
df[title1] = df[title1].astype(int)
df[title2] = df[title2].astype(int)

# 筛选大于阈值的行
df_filtered = df[(df[title1] > threshold1) | (df[title2] > threshold2)]

# 保存筛选后的数据到新的Excel文件
df_filtered.to_excel(out_file_path, index=False)

🚀 统计Dat文件中各个数值的数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import collections
import matplotlib.pyplot as plt

with open('a.dat', 'rb') as f:
data = f.read()

counter = collections.Counter(data)
x = list(counter.keys())
y = list(counter.values())

for key in sorted(counter.keys()):
print(f'{key} : {counter[key]}')

plt.bar(x, y)
plt.show()

🚀 将txt文件以空格进行分隔并批量转换为xlsx文件

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
import os
import pandas as pd

out_path_txt = './Out/Txt/'
out_path_xlsx = './Out/Xlsx/'

# 删除 ./Out/Xlsx/ 文件夹内所有的文件
for filename in os.listdir(out_path_xlsx):
file_path = os.path.join(out_path_xlsx, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
print(f'rm {file_path}')
except Exception as e:
print(f'Failed to delete {file_path}. Reason: {e}')

# 浏览并处理目标文件夹内的文件
for filename in os.listdir(out_path_txt):
file_path = os.path.join(out_path_txt, filename)
outPut_path = os.path.join(out_path_xlsx, filename)[0:-3] + 'xlsx'
print(f'create {outPut_path}')
# 读取CSV文件
df = pd.read_csv(file_path, sep=" ", header=0)
# 将DataFrame保存为xlsx文件
df.to_excel(outPut_path, index=False, engine='openpyxl')

🚀 将图片转换为c文件

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
from PIL import Image  
import os

# 定义一个函数,将RGB888格式转换为RGB565格式
def rgb888_to_rgb565(r, g, b, Little_Endian=0):
# 将8位的R,G,B值分别右移3,2,3位,得到5位的R,G,B值
r = r >> 3
g = g >> 2
b = b >> 3
# 将5位的R,G,B值拼接成16位的RGB565值,高位在前,低位在后
rgb565 = (r << 11) | (g << 5) | b
# 小端模式需要调换高低位
if Little_Endian != 0:
rgb565 = (rgb565 >> 8) | ((rgb565 & 0xFF) << 8)
# 返回RGB565值
return rgb565

# 将图像文件以RGB565的格式写入到.c文件
def img_to_cFile(img_name, cFile_name):
# 打开图像文件并转换为RGB模式
img = Image.open(img_name).convert("RGB")
# 获取图像的宽度和高度
width, height = img.size
with open(cFile_name, "w") as f:
f.write(f"// width = {width}, height = {height}\n")
f.write(f"const unsigned short img_{os.path.basename(cFile_name)[:-2]}[] = {{")
for y in range(height):
for x in range(width):
# 获取当前像素的RGB值
r, g, b = img.getpixel((x, y))

# 将RGB值转换为RGB565格式
rgb565 = rgb888_to_rgb565(r, g, b, 1)

# 每16个数据添加一个换行
if (x % 16 == 0):
f.write("\n ")

# 将RGB565格式写入文件
if x == width - 1 and y == height - 1: # 最后一个数据不用加逗号
f.write("0x{:04x}\n".format(rgb565))
else:
f.write("0x{:04x}, ".format(rgb565))
f.write("};\n")

# 删除文件夹内所有的文件
def rm_path_file(path_name):
for filename in os.listdir(path_name):
file_path = os.path.join(path_name, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
print(f'rm {file_path}')
except Exception as e:
print(f'Failed to delete {file_path}. Reason: {e}')

# 定义文件夹路径
img_path = './img/' # 放置图片的目录
cFile_path = './cFile/' # 生成.c文件的目录


if __name__ == '__main__':
rm_path_file(cFile_path) # 删除c文件夹内原有的文件
# 获取文件夹内的所有文件
files = os.listdir(img_path)
# 打印每个文件名
for file in files:
img_name = os.path.join(img_path, file)
# 检查是否为文件,排除子文件夹
if os.path.isfile(img_name):
cFile_name = os.path.splitext(file)[0] + '.c'
cFile_name = os.path.join(cFile_path, cFile_name)
img_to_cFile(img_name, cFile_name)
print(cFile_name)


🚀 将图片转换为webp格式

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
# 首先需要确保已经安装了Pillow库,如果还没有安装,可以使用pip命令安装:
# pip install Pillow

import os
from PIL import Image

def convert_to_webp(input_folder, output_folder):
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)

# 遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
# 构建输入和输出文件的完整路径
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.webp")

# 打开图片并转换为webp格式
with Image.open(input_path) as img:
print('open img')
img.save(output_path, 'webp')

print(f"Converted {filename} to webp")

# 使用示例
convert_to_webp('./', './out/')