本帖最后由 joody 于 2025-9-7 11:51 编辑
把输入的文本转为图片,不是文本生图。
用豆包写的,可以选字体,字号,颜色。格式是png,背景透明的。
默认保存图片在ps1所在位置,并且直接打开。
文本转png.ps1.txt
(6.46 KB, 下载次数: 5)
附件是ps1代码,去除后边的.txt即可
下面有源码,自己记得编码转换为ansi。
[PowerShell] 纯文本查看 复制代码 <#
.SYNOPSIS
将文本转换为PNG图片的工具,默认字号24,生成后自动打开
.DESCRIPTION
使用System.Drawing库将输入的文本转换为PNG图片,支持自定义字体、字号和文本颜色,保存到脚本所在目录并自动打开
#>
# 加载System.Drawing assembly
Add-Type -AssemblyName System.Drawing
# 获取当前脚本所在的目录路径
$scriptPath = $MyInvocation.MyCommand.Definition
$scriptDirectory = Split-Path -Parent $scriptPath
Write-Host "脚本所在位置:$scriptDirectory"
# 提示用户输入文本
$text = Read-Host "请输入要转换为图片的文本"
# 如果用户未输入文本,使用默认文本
if ([string]::IsNullOrWhiteSpace($text)) {
$text = "默认文本"
Write-Host "未输入文本,将使用默认文本: $text"
}
# 检测文本主要语言,用于默认字体选择
$hasChinese = $text -match '[\u4e00-\u9fa5]'
$hasEnglish = $text -match '[a-zA-Z]'
# 显示字体选项
Write-Host "`n请选择字体:"
Write-Host "1. 微软雅黑 (默认中文)`n2. Arial (默认英文)`n3. 宋体`n4. 黑体`n5. 楷体`n6. Times New Roman"
# 设置默认字体选项
if ($hasChinese -and !$hasEnglish) {
$defaultFontChoice = 1
} elseif ($hasEnglish -and !$hasChinese) {
$defaultFontChoice = 2
} else {
# 混合文本默认使用微软雅黑
$defaultFontChoice = 1
}
Write-Host "推荐默认字体选项: $defaultFontChoice"
# 获取用户选择的字体
do {
$fontChoiceInput = Read-Host "请输入选项(1-6,直接回车使用默认选项 $defaultFontChoice)"
if ([string]::IsNullOrWhiteSpace($fontChoiceInput)) {
$fontChoice = $defaultFontChoice
break
}
if ($fontChoiceInput -match '^\d+$') {
$num = [int]$fontChoiceInput
if ($num -ge 1 -and $num -le 6) {
$fontChoice = $num
break
}
}
Write-Host "无效的选项,请重新输入(1-6)"
} while ($true)
# 根据选择设置字体
switch ($fontChoice) {
1 { $fontFamilyName = "微软雅黑" }
2 { $fontFamilyName = "Arial" }
3 { $fontFamilyName = "宋体" }
4 { $fontFamilyName = "黑体" }
5 { $fontFamilyName = "楷体" }
6 { $fontFamilyName = "Times New Roman" }
}
# 显示字号选项,默认设置为24(选项6)
Write-Host "`n请选择字号:"
Write-Host "1. 9`n2. 12`n3. 14`n4. 16`n5. 18`n6. 24 (默认)`n7. 36`n8. 48`n9. 72`n10. 120`n11. 手动输入"
# 获取用户选择的字号,默认值设为6(对应24号字体)
do {
$fontSizeChoiceInput = Read-Host "请输入选项(1-11,直接回车使用默认选项6)"
if ([string]::IsNullOrWhiteSpace($fontSizeChoiceInput)) {
$fontSizeChoice = 6 # 默认选择24号字体
break
}
if ($fontSizeChoiceInput -match '^\d+$') {
$num = [int]$fontSizeChoiceInput
if ($num -ge 1 -and $num -le 11) {
$fontSizeChoice = $num
break
}
}
Write-Host "无效的选项,请重新输入(1-11)"
} while ($true)
# 根据选择设置字号
switch ([int]$fontSizeChoice) {
1 { $fontSize = 9 }
2 { $fontSize = 12 }
3 { $fontSize = 14 }
4 { $fontSize = 16 }
5 { $fontSize = 18 }
6 { $fontSize = 24 } # 默认字号
7 { $fontSize = 36 }
8 { $fontSize = 48 }
9 { $fontSize = 72 }
10 { $fontSize = 120 }
11 {
do {
$userInput = Read-Host "请输入字号"
if ($userInput -match '^\d+$' -and $userInput -gt 0) {
$fontSize = [int]$userInput
break
}
Write-Host "无效的字号,请输入正整数"
} while ($true)
}
}
# 显示颜色选项
Write-Host "`n请选择文本颜色:"
Write-Host "1. 黑色`n2. 白色`n3. 红色`n4. 绿色`n5. 蓝色`n6. 黄色`n7. 紫色`n8. 灰色"
# 获取用户选择的颜色
do {
$colorChoice = Read-Host "请输入选项(1-8)"
if ($colorChoice -match '^\d+$') {
$num = [int]$colorChoice
if ($num -ge 1 -and $num -le 8) {
break
}
}
Write-Host "无效的选项,请重新输入(1-8)"
} while ($true)
# 根据选择设置颜色
switch ([int]$colorChoice) {
1 { $color = [System.Drawing.Color]::Black }
2 { $color = [System.Drawing.Color]::White }
3 { $color = [System.Drawing.Color]::Red }
4 { $color = [System.Drawing.Color]::Green }
5 { $color = [System.Drawing.Color]::Blue }
6 { $color = [System.Drawing.Color]::Yellow }
7 { $color = [System.Drawing.Color]::Purple }
8 { $color = [System.Drawing.Color]::Gray }
}
# 创建字体和画笔
try {
$fontFamily = New-Object System.Drawing.FontFamily($fontFamilyName)
$font = New-Object System.Drawing.Font($fontFamily, $fontSize, [System.Drawing.FontStyle]::Regular)
}
catch {
Write-Host "警告:无法加载字体 '$fontFamilyName',将使用默认字体"
$fontFamily = [System.Drawing.FontFamily]::GenericSansSerif
$font = New-Object System.Drawing.Font($fontFamily, $fontSize, [System.Drawing.FontStyle]::Regular)
}
$brush = New-Object System.Drawing.SolidBrush($color)
# 测量文本大小以确定图像尺寸
$graphics = [System.Drawing.Graphics]::FromImage((New-Object System.Drawing.Bitmap(1, 1)))
$textSize = $graphics.MeasureString($text, $font)
# 添加一些边距
$margin = 0
$imageWidth = [int]$textSize.Width + $margin * 2
$imageHeight = [int]$textSize.Height + $margin * 2
# 创建位图和图形对象
$bitmap = New-Object System.Drawing.Bitmap($imageWidth, $imageHeight)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
# 设置文本渲染质量
$graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAlias
# 设置背景为透明
$bitmap = New-Object System.Drawing.Bitmap($imageWidth, $imageHeight, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.Clear([System.Drawing.Color]::Transparent) # 使用透明背景
# 绘制文本(居中显示)
$textX = ($imageWidth - $textSize.Width) / 2
$textY = ($imageHeight - $textSize.Height) / 2
$graphics.DrawString($text, $font, $brush, $textX, $textY)
# 处理文件名(移除无效字符)
$invalidChars = [System.IO.Path]::GetInvalidFileNameChars()
$fileName = $text -replace "[$([regex]::Escape(-join $invalidChars))]", ""
if ([string]::IsNullOrWhiteSpace($fileName)) {
$fileName = "text_image"
}
# 图片保存路径设置为脚本所在目录
$filePath = Join-Path -Path $scriptDirectory -ChildPath "$fileName.png"
# 保存图像并自动打开
try {
$bitmap.Save($filePath, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Host "`n图片已成功保存到:$filePath"
# 生成后直接打开图片
Start-Process -FilePath $filePath
Write-Host "正在打开图片..."
} catch {
Write-Host "`n操作时出错:$_"
} finally {
# 释放资源
$graphics.Dispose()
$bitmap.Dispose()
$brush.Dispose()
$font.Dispose()
}
写完搜索了一下52,有个可以类似的,而且可以预览的工具,只是没有找到透明背景的选项。
https://www.52pojie.cn/forum.php?mod=viewthread&tid=966989&highlight=%CE%C4%B1%BE%D7%AA%CD%BC%C6%AC,
找到了官网 https://www.sttmedia.com/downloads/TextImagesEn.zip
|