吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1797|回复: 8
收起左侧

[其他原创] powershell把输入的文本转为图片

  [复制链接]
joody 发表于 2025-9-6 21:18
本帖最后由 joody 于 2025-9-7 11:51 编辑

把输入的文本转为图片,不是文本生图。

用豆包写的,可以选字体,字号,颜色。格式是png,背景透明的。

默认保存图片在ps1所在位置,并且直接打开。
文本转png.ps1.txt (6.46 KB, 下载次数: 5)
附件是ps1代码,去除后边的.txt即可


2025-09-06_211710.png 2025-09-07_114932.jpg

下面有源码,自己记得编码转换为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

免费评分

参与人数 3吾爱币 +8 热心值 +3 收起 理由
only2025 + 1 加油
laozhang4201 + 1 + 1 热心回复!
hrh123 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ROSE1688 发表于 2025-9-7 08:50
最终效果是什么样子的?
JikeCoolTech 发表于 2025-9-7 09:06
三滑稽甲苯 发表于 2025-9-7 09:37
daoye9988 发表于 2025-9-7 09:40
期待文本生成图片的
moyc 发表于 2025-9-7 10:15
大佬,出错:操作时出错:使用“2”个参数调用“Save”时发生异常:“值不能为 null。
参数名: stream”
 楼主| joody 发表于 2025-9-7 12:01
JikeCoolTech 发表于 2025-9-7 09:06
求大佬出个打包版的吧

写完搜索了一下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
 楼主| joody 发表于 2025-9-7 12:02
moyc 发表于 2025-9-7 10:15
大佬,出错:操作时出错:使用“2”个参数调用“Save”时发生异常:“值不能为 null。
参数名: stream”

怀疑你保存的时候没有改编码,你下载附件试一试
zzz123uiop 发表于 2025-9-25 08:48
挺好用,不错不错
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-4-17 09:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表