-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageRepairTool.ps1
More file actions
806 lines (711 loc) · 26.2 KB
/
Copy pathImageRepairTool.ps1
File metadata and controls
806 lines (711 loc) · 26.2 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
param(
[Parameter(Position = 0)]
[Alias("Input", "In", "InputDir")]
[string]$Source = ".\resource",
[Alias("Out", "OutputDir")]
[string]$Output = "",
[ValidateSet("simple", "debug", "full")]
[string]$OutputProfile = "simple",
[ValidateSet("ppt", "svg", "both")]
[string]$Target = "ppt",
[ValidateSet("dual", "mimic", "guide")]
[string]$TemplateMode = "dual",
[switch]$ReferenceSlides,
[switch]$NoReferenceSlides,
[switch]$DebugPreview,
[string]$Placeholder = "__AUTO__",
[switch]$AutoPlaceholder,
[switch]$NoSampledStyle,
[double]$GuideWidth = 0.0,
[string]$GuideColor = "#2563eb",
[string]$FontFace = "Microsoft YaHei",
[int]$DarkThreshold = 132,
[int]$ColoredThreshold = 168,
[int]$LineGap = 6,
[int]$VerticalGap = 1,
[int]$LineSuppressionLength = 0,
[int]$MaxBoxes = 260,
[ValidateSet("auto", "off", "tesseract")]
[string]$OcrMode = "auto",
[ValidateSet("box", "image", "both")]
[string]$OcrStrategy = "box",
[string]$Tesseract = "",
[string]$OcrLang = "chi_sim+eng",
[int]$OcrPsm = 6,
[string]$OcrBoxPsm = "13,7",
[int]$OcrScale = 3,
[double]$OcrMinConfidence = 45.0,
[int]$OcrMaxBoxes = 60,
[string]$FallbackGlyph = ([char]0x25A1),
[ValidateSet("both", "embedded", "trace")]
[string]$SvgMode = "both",
[switch]$SvgPreview,
[int]$SvgColors = 64,
[int]$SvgMaxSize = 1400,
[double]$SvgMinArea = 1.0,
[int]$SvgMaxPathsPerColor = 4000,
[string]$Magick = "",
[switch]$Open,
[switch]$Help,
[switch]$Check
)
$ErrorActionPreference = "Stop"
$script:EffectiveDebugPreview = $DebugPreview -or ($OutputProfile -in @("debug", "full"))
$script:EffectiveReferenceSlides = $ReferenceSlides -or (-not $NoReferenceSlides)
$ImageExtensions = @(".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff")
$MagickCandidates = @(
"D:\ImageMagick-7.1.2-Q16-HDRI\magick.exe",
"C:\Program Files\ImageMagick-7.1.2-Q16-HDRI\magick.exe",
"C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick.exe",
"C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe"
)
function Write-Usage {
Write-Host @"
AI Image Repair Tool
Simple use:
Drag an image or folder onto RepairImage.cmd
.\ImageRepairTool.ps1 "image.png"
.\ImageRepairTool.ps1 -Input "image.png"
.\ImageRepairTool.ps1 -Input ".\resource" -Output ".\output"
Default PPT output includes an original reference slide and an editable slide.
Common modes:
-Check Check dependencies only.
-Open Open the output folder when finished.
-OutputProfile simple Write only final files and START_HERE.txt. Default.
-OutputProfile debug Keep debug previews and intermediate files.
-OutputProfile full Keep every generated artifact.
-Target ppt|svg|both Generate editable PPTX, SVG, or both.
Useful only when tuning:
-NoReferenceSlides Skip the original-image reference slides.
-OcrMode off Disable OCR and use placeholders.
-OcrMode tesseract Force OCR even on dense images.
-OcrMaxBoxes 60 Auto-skip OCR on dense images above this count.
-LineGap 10 -VerticalGap 2 Merge nearby characters more aggressively.
-LineSuppressionLength 40 Separate text from long box/arrow/table lines.
-DebugPreview Save detected text-box previews.
"@
}
if ($Help) {
Write-Usage
exit 0
}
function Get-InputImageFiles {
param([string]$InputDir)
return @(Get-ChildItem -LiteralPath $InputDir -File |
Where-Object { $ImageExtensions -contains $_.Extension.ToLowerInvariant() } |
Sort-Object Name)
}
function Get-ImageInputSummary {
param([string]$Path)
$resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop
$item = Get-Item -LiteralPath $resolved.Path
$supported = ($ImageExtensions -join ", ")
if ($item.PSIsContainer) {
$images = Get-InputImageFiles -InputDir $item.FullName
if ($images.Count -eq 0) {
throw "No supported image files found in input folder: $($item.FullName). Supported extensions: $supported"
}
return @{
Kind = "Directory"
Path = $item.FullName
ImageCount = $images.Count
Images = $images
}
}
$extension = $item.Extension.ToLowerInvariant()
if ($ImageExtensions -notcontains $extension) {
throw "Input file is not a supported image: $($item.FullName). Supported extensions: $supported"
}
return @{
Kind = "File"
Path = $item.FullName
ImageCount = 1
Images = @($item)
}
}
function Resolve-Python {
$candidates = @(
"D:\conda\miniconda3\python.exe",
"python"
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) {
return (Resolve-Path $candidate).Path
}
$command = Get-Command $candidate -ErrorAction SilentlyContinue
if ($null -ne $command) {
return $command.Source
}
}
throw "Python was not found. Install Python with NumPy."
}
function Resolve-Node {
$command = Get-Command node -ErrorAction SilentlyContinue
if ($null -eq $command) {
throw "Node.js was not found. It is required for PPTX generation."
}
return $command.Source
}
function Resolve-Magick {
param([string]$Requested)
$candidates = @()
if ($Requested -ne "") {
$candidates += $Requested
}
$candidates += "magick"
$candidates += $MagickCandidates
foreach ($candidate in $candidates) {
if ($candidate -eq "") {
continue
}
if (Test-Path $candidate) {
return (Resolve-Path $candidate).Path
}
$command = Get-Command $candidate -ErrorAction SilentlyContinue
if ($null -ne $command) {
return $command.Source
}
}
throw "ImageMagick 'magick' was not found. Install ImageMagick or pass -Magick."
}
function Resolve-Tesseract {
param([string]$Requested)
$candidates = @()
if ($Requested -ne "") {
$candidates += $Requested
}
$candidates += @(
"tesseract",
"D:\Tesseract-OCR\tesseract.exe",
"C:\Program Files\Tesseract-OCR\tesseract.exe",
"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
)
foreach ($candidate in $candidates) {
if ($candidate -eq "") {
continue
}
if (Test-Path $candidate) {
return (Resolve-Path $candidate).Path
}
$command = Get-Command $candidate -ErrorAction SilentlyContinue
if ($null -ne $command) {
return $command.Source
}
}
return $null
}
function Write-CheckLine {
param(
[ValidateSet("OK", "WARN", "FAIL")]
[string]$Status,
[string]$Name,
[string]$Detail
)
$color = "Green"
if ($Status -eq "WARN") {
$color = "Yellow"
}
elseif ($Status -eq "FAIL") {
$color = "Red"
}
Write-Host ("[{0}] {1}: {2}" -f $Status, $Name, $Detail) -ForegroundColor $color
}
function Invoke-PreflightCheck {
$failures = 0
$warnings = 0
Write-Host "AI Image Repair Tool preflight"
Write-Host "Target: $Target"
try {
$summary = Get-ImageInputSummary -Path $Source
Write-CheckLine -Status "OK" -Name "Input" -Detail "$($summary.ImageCount) image(s) in $($summary.Path)"
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "Input" -Detail $_.Exception.Message
}
try {
$python = Resolve-Python
$version = (& $python -c "import sys; print('.'.join(map(str, sys.version_info[:3])))" 2>&1)
if ($LASTEXITCODE -ne 0) {
throw ($version -join "`n")
}
Write-CheckLine -Status "OK" -Name "Python" -Detail "$python ($version)"
$numpyVersion = (& $python -c "import numpy; print(numpy.__version__)" 2>&1)
if ($LASTEXITCODE -ne 0) {
throw "NumPy import failed: $($numpyVersion -join "`n")"
}
Write-CheckLine -Status "OK" -Name "NumPy" -Detail $numpyVersion
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "Python/NumPy" -Detail $_.Exception.Message
}
try {
$magickPath = Resolve-Magick -Requested $Magick
$magickVersion = (& $magickPath -version 2>&1 | Select-Object -First 1)
if ($LASTEXITCODE -ne 0) {
throw ($magickVersion -join "`n")
}
Write-CheckLine -Status "OK" -Name "ImageMagick" -Detail "$magickPath ($magickVersion)"
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "ImageMagick" -Detail $_.Exception.Message
}
if ($Target -eq "ppt" -or $Target -eq "both") {
try {
$node = Resolve-Node
$nodeVersion = (& $node --version 2>&1)
if ($LASTEXITCODE -ne 0) {
throw ($nodeVersion -join "`n")
}
Write-CheckLine -Status "OK" -Name "Node.js" -Detail "$node ($nodeVersion)"
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "Node.js" -Detail $_.Exception.Message
}
try {
Push-Location $PSScriptRoot
try {
$packageCheck = (& $node -e "import('pptxgenjs').then(() => process.exit(0)).catch((error) => { console.error(error.message); process.exit(1); })" 2>&1)
}
finally {
Pop-Location
}
if ($LASTEXITCODE -ne 0) {
throw "pptxgenjs was not found. Run npm install before generating PPTX. $($packageCheck -join ' ')"
}
Write-CheckLine -Status "OK" -Name "PPTX package" -Detail "pptxgenjs is installed."
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "PPTX package" -Detail $_.Exception.Message
}
if ($OcrMode -ne "off") {
$tesseractPath = Resolve-Tesseract -Requested $Tesseract
if ($null -eq $tesseractPath) {
if ($OcrMode -eq "tesseract") {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "Tesseract" -Detail "OCR mode is 'tesseract' but tesseract was not found."
}
else {
$warnings += 1
Write-CheckLine -Status "WARN" -Name "Tesseract" -Detail "Not found. The tool will use quiet placeholders unless you install Tesseract or pass -OcrMode off."
}
}
else {
$tesseractVersion = (& $tesseractPath --version 2>&1 | Select-Object -First 1)
Write-CheckLine -Status "OK" -Name "Tesseract" -Detail "$tesseractPath ($tesseractVersion)"
}
}
else {
Write-CheckLine -Status "OK" -Name "Tesseract" -Detail "Skipped because -OcrMode off is set."
}
}
try {
if ([string]::IsNullOrWhiteSpace($Output)) {
$defaultResultsRoot = Join-Path $PSScriptRoot "results"
$outputPath = Resolve-Path -Path (New-Item -ItemType Directory -Force -Path $defaultResultsRoot)
Write-CheckLine -Status "OK" -Name "Output" -Detail "Writable default output root: $outputPath (auto-assigned per run)"
}
else {
$outputPath = Resolve-Path -Path (New-Item -ItemType Directory -Force -Path $Output)
Write-CheckLine -Status "OK" -Name "Output" -Detail "Writable output root: $outputPath"
}
}
catch {
$failures += 1
Write-CheckLine -Status "FAIL" -Name "Output" -Detail $_.Exception.Message
}
if ($failures -gt 0) {
Write-Host "Preflight failed with $failures failure(s) and $warnings warning(s)."
return 1
}
Write-Host "Preflight passed with $warnings warning(s)."
return 0
}
function Resolve-ImageInput {
param([string]$Path)
$summary = Get-ImageInputSummary -Path $Path
if ($summary.Kind -eq "Directory") {
$label = ConvertTo-SafePathName -Name (Split-Path -Leaf $summary.Path)
return @{
InputDir = $summary.Path
TempDir = $null
ImageCount = $summary.ImageCount
Label = $label
SourcePath = $summary.Path
}
}
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("image_repair_tool_input_" + [Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
$sourceItem = $summary.Images[0]
Copy-Item -LiteralPath $sourceItem.FullName -Destination (Join-Path $tempDir $sourceItem.Name) -Force
return @{
InputDir = $tempDir
TempDir = $tempDir
ImageCount = 1
Label = (ConvertTo-SafePathName -Name $sourceItem.BaseName)
SourcePath = $sourceItem.FullName
}
}
function ConvertTo-SafePathName {
param([string]$Name)
$safe = [regex]::Replace([string]$Name, '[<>:"/\\|?*\x00-\x1f]', "_").Trim(" ._")
if ([string]::IsNullOrWhiteSpace($safe)) {
return "images"
}
if ($safe.Length -gt 60) {
return $safe.Substring(0, 60)
}
return $safe
}
function New-DefaultOutputPath {
param([hashtable]$InputInfo)
$stamp = Get-Date -Format "yyyyMMdd_HHmmss"
$label = ConvertTo-SafePathName -Name ([string]$InputInfo.Label)
return Join-Path $PSScriptRoot ("results\{0}_{1}" -f $stamp, $label)
}
function Invoke-PptWorkflow {
param(
[string]$InputDir,
[string]$OutputDir
)
$scriptPath = Join-Path $PSScriptRoot "convert_images_to_editable_ppt.ps1"
if (!(Test-Path $scriptPath)) {
throw "PowerPoint workflow script was not found: $scriptPath"
}
$effectivePlaceholder = $Placeholder
if ($AutoPlaceholder) {
$effectivePlaceholder = "__AUTO__"
}
$effectiveGuideWidth = $GuideWidth
if ($TemplateMode -eq "guide" -and $effectiveGuideWidth -le 0) {
$effectiveGuideWidth = 1.0
}
$params = @{
InputDir = $InputDir
OutputDir = $OutputDir
TemplateMode = $TemplateMode
Placeholder = $effectivePlaceholder
DarkThreshold = $DarkThreshold
ColoredThreshold = $ColoredThreshold
LineGap = $LineGap
VerticalGap = $VerticalGap
LineSuppressionLength = $LineSuppressionLength
MaxBoxes = $MaxBoxes
GuideColor = $GuideColor
GuideWidth = $effectiveGuideWidth
FontFace = $FontFace
OcrMode = $OcrMode
OcrStrategy = $OcrStrategy
Tesseract = $Tesseract
OcrLang = $OcrLang
OcrPsm = $OcrPsm
OcrBoxPsm = $OcrBoxPsm
OcrScale = $OcrScale
OcrMinConfidence = $OcrMinConfidence
OcrMaxBoxes = $OcrMaxBoxes
FallbackGlyph = $FallbackGlyph
Magick = $Magick
}
if ($script:EffectiveReferenceSlides) {
$params.ReferenceSlides = $true
}
if ($script:EffectiveDebugPreview) {
$params.DebugPreview = $true
}
if ($NoSampledStyle) {
$params.NoSampledStyle = $true
}
$workflowOutput = New-Object System.Collections.Generic.List[string]
& $scriptPath @params 2>&1 | ForEach-Object {
$line = [string]$_
$workflowOutput.Add($line) | Out-Null
if ($OutputProfile -ne "simple" -or $line -match "^(OCR:|OCR |Processing |Warning:|ERROR:)" -or $line -match "text box\(es\)") {
Write-Host $line
}
}
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
if ($OutputProfile -eq "simple") {
Write-Host "Detailed log:"
$workflowOutput | ForEach-Object { Write-Host $_ }
}
exit $exitCode
}
}
function Assert-ChildPath {
param(
[string]$Parent,
[string]$Child
)
$parentPath = (Resolve-Path -LiteralPath $Parent).Path
$childPath = (Resolve-Path -LiteralPath $Child).Path
if ($childPath -eq $parentPath) {
throw "Refusing to treat root path as child: $childPath"
}
if (-not $childPath.StartsWith($parentPath + [System.IO.Path]::DirectorySeparatorChar)) {
throw "Path is outside expected output root: $childPath"
}
}
function Copy-ResultFile {
param(
[string]$Source,
[string]$Destination
)
$parent = Split-Path -Parent $Destination
New-Item -ItemType Directory -Force -Path $parent | Out-Null
Copy-Item -LiteralPath $Source -Destination $Destination -Force
return (Resolve-Path -LiteralPath $Destination).Path
}
function Publish-PptResults {
param(
[string]$PptOutput,
[string]$OutputRoot,
[hashtable]$InputInfo
)
$primaryFiles = @()
$debugFiles = @()
$combined = Join-Path $PptOutput "combined_editable_text_layer.pptx"
$perImageDir = Join-Path $PptOutput "per_image"
$summaryPath = Join-Path $PptOutput "ppt_summary.json"
$pptSummary = $null
if (Test-Path -LiteralPath $summaryPath) {
try {
$pptSummary = Get-Content -Raw -LiteralPath $summaryPath | ConvertFrom-Json
}
catch {
Write-Host "Warning: could not read PPT summary: $($_.Exception.Message)"
}
}
if ($OutputProfile -eq "simple") {
if ($InputInfo.ImageCount -eq 1 -and (Test-Path $perImageDir)) {
$single = @(Get-ChildItem -LiteralPath $perImageDir -Filter "*.pptx" -File | Sort-Object Name | Select-Object -First 1)
if ($single.Count -gt 0) {
$primaryFiles += Copy-ResultFile -Source $single[0].FullName -Destination (Join-Path $OutputRoot "editable_template.pptx")
}
}
elseif (Test-Path $combined) {
$primaryFiles += Copy-ResultFile -Source $combined -Destination (Join-Path $OutputRoot "combined_editable_text_layer.pptx")
if (Test-Path $perImageDir) {
$editableDir = Join-Path $OutputRoot "editable_pptx"
New-Item -ItemType Directory -Force -Path $editableDir | Out-Null
Get-ChildItem -LiteralPath $perImageDir -Filter "*.pptx" -File |
Sort-Object Name |
ForEach-Object {
$primaryFiles += Copy-ResultFile -Source $_.FullName -Destination (Join-Path $editableDir $_.Name)
}
}
}
$debugDir = Join-Path $PptOutput "debug"
if ($script:EffectiveDebugPreview -and (Test-Path $debugDir)) {
$reviewDir = Join-Path $OutputRoot "review"
New-Item -ItemType Directory -Force -Path $reviewDir | Out-Null
Get-ChildItem -LiteralPath $debugDir -File |
Sort-Object Name |
ForEach-Object {
$debugFiles += Copy-ResultFile -Source $_.FullName -Destination (Join-Path $reviewDir $_.Name)
}
}
Assert-ChildPath -Parent $OutputRoot -Child $PptOutput
Remove-Item -LiteralPath $PptOutput -Recurse -Force
}
else {
if (Test-Path $combined) {
$primaryFiles += (Resolve-Path -LiteralPath $combined).Path
}
if (Test-Path $perImageDir) {
$primaryFiles += @(Get-ChildItem -LiteralPath $perImageDir -Filter "*.pptx" -File | Sort-Object Name | ForEach-Object { $_.FullName })
}
$debugDir = Join-Path $PptOutput "debug"
if (Test-Path $debugDir) {
$debugFiles += @(Get-ChildItem -LiteralPath $debugDir -File | Sort-Object Name | ForEach-Object { $_.FullName })
}
}
return @{
PrimaryFiles = $primaryFiles
DebugFiles = $debugFiles
Summary = $pptSummary
}
}
function Format-CommandPath {
param([string]$Path)
return '"' + ([string]$Path).Replace('"', '\"') + '"'
}
function New-SuggestedOutputPath {
param(
[string]$OutputRoot,
[string]$Suffix
)
$parent = Split-Path -Parent $OutputRoot
$leaf = Split-Path -Leaf $OutputRoot
return Join-Path $parent ($leaf + $Suffix)
}
function Write-StartHere {
param(
[string]$OutputRoot,
[array]$PrimaryFiles,
[array]$DebugFiles,
[array]$SvgPaths,
[object]$PptSummary,
[hashtable]$InputInfo
)
$lines = @(
"AI Image Repair Tool results",
"",
"1. Open first:"
)
if ($PrimaryFiles.Count -eq 0 -and $SvgPaths.Count -eq 0) {
$lines += " No final files were recorded. Check the console output above."
}
foreach ($file in $PrimaryFiles) {
$lines += " $file"
}
foreach ($path in $SvgPaths) {
$lines += " $path"
}
if ($PrimaryFiles.Count -gt 0) {
$lines += ""
$lines += "2. Edit in PowerPoint:"
if ($script:EffectiveReferenceSlides) {
$lines += " Each image has a reference slide first, then an editable slide."
}
else {
$lines += " Each slide is an editable repair page."
}
$lines += " Click recognized text, placeholder text, or blue guide boxes and type the corrected text."
$lines += " Selection Pane layers use names like 01_original_reference, 02_cleaned_text_removed, and 03_text_001."
$lines += " If there is no 02_cleaned_text_removed layer and the original text is still visible, dense-image guide mode is active."
}
if ($null -ne $PptSummary -and $null -ne $PptSummary.images) {
$lines += ""
$lines += "3. What this run detected:"
foreach ($image in @($PptSummary.images)) {
$mode = [string]$image.templateMode
if ([string]::IsNullOrWhiteSpace($mode)) {
$mode = $TemplateMode
}
$ocrNote = ""
if ($null -ne $image.ocrSkipped -and -not [string]::IsNullOrWhiteSpace([string]$image.ocrSkipped)) {
$ocrNote = ", OCR skipped"
}
$lines += " $($image.name): $($image.textBoxes) text box(es), mode=$mode$ocrNote"
}
}
if ($DebugFiles.Count -gt 0) {
$lines += ""
$lines += "Debug previews:"
foreach ($file in $DebugFiles) {
$lines += " $file"
}
}
$lines += ""
$lines += "4. If the result needs tuning:"
$inputPath = [string]$InputInfo.SourcePath
if ([string]::IsNullOrWhiteSpace($inputPath)) {
$inputPath = [string]$Source
}
$debugOutput = New-SuggestedOutputPath -OutputRoot $OutputRoot -Suffix "_debug"
$ocrOutput = New-SuggestedOutputPath -OutputRoot $OutputRoot -Suffix "_ocr"
$lines += " See detection boxes:"
$lines += " .\ImageRepairTool.ps1 " + (Format-CommandPath -Path $inputPath) + " -Output " + (Format-CommandPath -Path $debugOutput) + " -OutputProfile debug"
$lines += " Force OCR on dense images:"
$lines += " .\ImageRepairTool.ps1 " + (Format-CommandPath -Path $inputPath) + " -Output " + (Format-CommandPath -Path $ocrOutput) + " -OcrMode tesseract"
$lines += " Run .\ImageRepairTool.ps1 -Help for more options."
$startHere = Join-Path $OutputRoot "START_HERE.txt"
Set-Content -LiteralPath $startHere -Value $lines -Encoding UTF8
return (Resolve-Path -LiteralPath $startHere).Path
}
function Invoke-SvgWorkflow {
param(
[string]$InputDir,
[string]$OutputDir
)
$scriptPath = Join-Path $PSScriptRoot "convert_images_to_svg.ps1"
if (!(Test-Path $scriptPath)) {
throw "SVG workflow script was not found: $scriptPath"
}
$params = @{
InputDir = $InputDir
OutputDir = $OutputDir
Mode = $SvgMode
Colors = $SvgColors
MaxSize = $SvgMaxSize
MinArea = $SvgMinArea
MaxPathsPerColor = $SvgMaxPathsPerColor
}
if ($SvgPreview) {
$params.Preview = $true
}
if ($Magick -ne "") {
$params.Magick = $Magick
}
$workflowOutput = New-Object System.Collections.Generic.List[string]
& $scriptPath @params 2>&1 | ForEach-Object {
$line = [string]$_
$workflowOutput.Add($line) | Out-Null
if ($OutputProfile -ne "simple" -or $line -match "^(Input:|Images:|== |embedded:|traced:|preview:|Warning:|ERROR:)") {
Write-Host $line
}
}
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
if ($OutputProfile -eq "simple") {
Write-Host "Detailed log:"
$workflowOutput | ForEach-Object { Write-Host $_ }
}
exit $exitCode
}
}
if ($Check) {
exit (Invoke-PreflightCheck)
}
$inputInfo = Resolve-ImageInput -Path $Source
if ([string]::IsNullOrWhiteSpace($Output)) {
$Output = New-DefaultOutputPath -InputInfo $inputInfo
}
$outputPath = Resolve-Path -Path (New-Item -ItemType Directory -Force -Path $Output)
$primaryFiles = @()
$debugFiles = @()
$svgPaths = @()
$pptSummary = $null
try {
if ($Target -eq "ppt" -or $Target -eq "both") {
$pptOutput = Join-Path $outputPath "ppt"
New-Item -ItemType Directory -Force -Path $pptOutput | Out-Null
Write-Host "Creating editable PowerPoint output..."
Invoke-PptWorkflow -InputDir $inputInfo.InputDir -OutputDir $pptOutput
$pptResults = Publish-PptResults -PptOutput $pptOutput -OutputRoot $outputPath -InputInfo $inputInfo
$primaryFiles += $pptResults.PrimaryFiles
$debugFiles += $pptResults.DebugFiles
$pptSummary = $pptResults.Summary
Write-Host "PPT output ready."
}
if ($Target -eq "svg" -or $Target -eq "both") {
$svgOutput = Join-Path $outputPath "svg"
New-Item -ItemType Directory -Force -Path $svgOutput | Out-Null
Write-Host "Creating SVG output..."
Invoke-SvgWorkflow -InputDir $inputInfo.InputDir -OutputDir $svgOutput
$svgPaths += (Resolve-Path -LiteralPath $svgOutput).Path
Write-Host "SVG output: $svgOutput"
}
$startHere = Write-StartHere -OutputRoot $outputPath -PrimaryFiles $primaryFiles -DebugFiles $debugFiles -SvgPaths $svgPaths -PptSummary $pptSummary -InputInfo $inputInfo
Write-Host "Start here: $startHere"
foreach ($file in $primaryFiles) {
Write-Host "Result: $file"
}
foreach ($path in $svgPaths) {
Write-Host "Result: $path"
}
Write-Host "Done. Output root: $outputPath"
if ($Open) {
Start-Process -FilePath "explorer.exe" -ArgumentList "`"$outputPath`""
}
}
finally {
if ($null -ne $inputInfo.TempDir -and (Test-Path $inputInfo.TempDir)) {
Remove-Item -LiteralPath $inputInfo.TempDir -Recurse -Force
}
}