Board logo

标题: [原创代码] PowerShell实现txt笔记转图片 [打印本页]

作者: went    时间: 2023-9-28 00:08     标题: PowerShell实现txt笔记转图片

编码自动检测,可自由设置字体,颜色
转换当前文件夹下所有txt,转换完成的Png放到png文件夹

txt2png.ps1
  1. cls
  2. [void][System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
  3. #判断文件编码的函数
  4. function Get-FileCoder($file_path){
  5. $bytes = [System.IO.File]::ReadAllBytes($file_path)
  6. if($bytes[0] -eq 0xff -and $bytes[1] -eq 0xfe){ return 'utf-16_le' }
  7. if($bytes[0] -eq 0xfe -and $bytes[1] -eq 0xff){ return 'utf-16_be' }
  8. if($bytes[0] -eq 0xef -and $bytes[1] -eq 0xbb -and $bytes[2] -eq 0xbf){ return 'utf-8_bom' }
  9. $index = 0; $bu8 = $false
  10. while($index -lt $bytes.Count){
  11. $one = 0
  12. for($i = 0; $i -lt 8; $i++){
  13. if(($bytes[$index] -band (0x80 -shr $i)) -eq 0){ break; }
  14. ++$one
  15. }
  16. if($one -eq 0){
  17. ++$index
  18. } else {
  19. if($one -eq 1){ return 'ansi' }
  20. $bu8 = $true
  21. for($i = 0; $i -lt $one-1; $i++){
  22. ++$index
  23. if(($bytes[$index] -band 0x80) -ne 0x80){ return 'ansi' }
  24. }
  25. ++$index
  26. }
  27. }
  28. if($bu8){return 'utf-8'} else { return 'ansi'}
  29. }
  30. #字体
  31. $font_size = 20
  32. $font_family = 'Courier New'
  33. $font = New-Object 'System.Drawing.Font'(([System.Drawing.FontFamily]::Families | Where-Object { $_.Name -eq $font_family } | Select-Object -First 1),$font_size)
  34. #画刷
  35. $black_brush = New-Object 'System.Drawing.SolidBrush'([System.Drawing.Color]::Black)
  36. $white_brush = New-Object 'System.Drawing.SolidBrush'([System.Drawing.Color]::White)
  37. #遍历txt
  38. [void][System.IO.Directory]::CreateDirectory('png')
  39. Get-ChildItem '*.txt' | foreach {
  40.     Write-Host $_.FullName
  41.     #读取文字
  42.     $cp = Get-FileCoder -file_path $_.FullName
  43.     Write-Host $cp
  44.     $lines = $null
  45.     if($cp -eq 'utf-8'){
  46. $lines = Get-Content -Encoding UTF8 -Path $_.FullName
  47.     } elseif ( $cp -eq 'ansi'){
  48. $lines = Get-Content -Encoding Default -Path $_.FullName
  49.     } elseif ( $cp -eq 'utf-16_le'){
  50. $lines = Get-Content -Encoding Unicode -Path $_.FullName
  51.     } elseif ( $cp -eq 'utf-16_be'){
  52. $lines = Get-Content -Encoding BigEndianUnicode -Path $_.FullName
  53.     } elseif ( $cp -eq 'utf-8_bom'){
  54. $lines = Get-Content -Encoding UTF8 -Path $_.FullName
  55.     }
  56.     #计算宽高
  57.     $max_c = 0
  58.     $lines | foreach {
  59.         $len = ([System.Text.UnicodeEncoding]::Unicode.GetBytes($_)|Where-Object{ $_ -ne 0 }).Count
  60.         if($max_c -lt $len){ $max_c = $len}
  61.     }
  62.     $w = $max_c*$font_size+20
  63.     $h = $lines.Count*2*$font_size+20
  64.     #创建图片和画布
  65.     $bmp = New-Object 'System.Drawing.Bitmap'($w,$h)
  66.     $g = [System.Drawing.Graphics]::FromImage($bmp)
  67.     #画白色背景
  68.     $g.FillRectangle($white_brush,0,0,$w,$h)
  69.     #画文字
  70.     $y0 = 5
  71.     $x0 = 5
  72.     $lines | foreach {
  73.         $g.DrawString($_,$font,$black_brush,$x0,$y0)
  74.         $y0 += $font_size*2
  75.     }
  76.     #保存图片
  77.     [void]$g.Save()
  78.     $bmp.Save('.\png\' + $_.BaseName + '.png',[System.Drawing.Imaging.ImageFormat]::Png)
  79.     '.\png\' + $_.BaseName + '.png'
  80.     '------------------------'
  81. }
  82. pause
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2