shell列出目录树形结构
#安装tree
brew install tree
#使用tree命令
tree
#深层结构
tree -L 2
python 初始化数组
res = ["" for _ in range(10)]
res[0]="abc"
python svg转png/ico
import cairosvg
import io
from PIL import Image
def svg_to_png(svg_content, output_path, width=None, height=None):
"""
将SVG代码转换为PNG图片
Args:
svg_content: SVG代码字符串
output_path: 输出PNG文件路径
width: 可选,输出图片宽度
height: 可选,输出图片高度
"""
# 将SVG转换为PNG
if width and height:
png_data = cairosvg.svg2png(bytestring=svg_content.encode('utf-8'),
output_width=width,
output_height=height)
else:
png_data = cairosvg.svg2png(bytestring=svg_content.encode('utf-8'))
# 保存PNG文件
with open(output_path, 'wb') as png_file:
png_file.write(png_data)
def png_to_ico(png_path, ico_path, sizes=None, optimize=True):
"""
将PNG图片转换为ICO图标
Args:
png_path: PNG文件路径
ico_path: 输出ICO文件路径
sizes: 图标尺寸列表,默认为常用尺寸
optimize: 是否优化图像质量
"""
if sizes is None:
sizes = [16, 24, 32, 48, 64, 128, 256] # 常用ICO尺寸
try:
# 打开原始PNG图像
original_img = Image.open(png_path)
# 确保图像模式为RGBA(支持透明度)
if original_img.mode != 'RGBA':
original_img = original_img.convert('RGBA')
images = []
for size in sizes:
try:
# 调整图像尺寸,保持宽高比
img_resized = original_img.resize(
(size, size),
Image.Resampling.LANCZOS # 高质量重采样
)
images.append(img_resized)
except Exception as e:
print(f"Warning: Failed to resize to {size}x{size}: {e}")
continue
if not images:
raise ValueError("No valid icons were generated")
# 保存为ICO文件(包含所有尺寸)
images[0].save(
ico_path,
format='ICO',
sizes=[(img.width, img.height) for img in images],
append_images=images[1:] if len(images) > 1 else None
)
return True
except Exception as e:
print(f"Error converting PNG to ICO: {e}")
return False