PoshCode Archive  Artifact [0ba0452e52]

Artifact 0ba0452e529b92d4830c272b10f1399818f789076bca320e5197f31450d8a12b:

  • File image2excel.ps1 — part of check-in [0df7d17fe9] at 2018-06-10 13:23:15 on branch trunk — Just for fun script for converting image files to excel. Very slow, try 50×50 bvfges first (user: AxCoder size: 1326)

# encoding: utf-8
# api: powershell
# title: image2excel
# description: Just for fun script for converting image files to excel. Very slow,  try 50×50 bvfges first
# version: 2.5
# type: script
# author: AxCoder
# license: CC0
# x-poshcode-id: 3274
# x-derived-from-id: 4047
# x-archived: 2016-10-06T11:27:20
# x-published: 2012-03-11T01:35:00
#
#
<#
    .Description
    image2excel converts image to excel file
#>
param (
    [parameter(Mandatory=$true,
               ValueFromPipeline=$true,
               HelpMessage=”Image file path”               
               )]
    [ValidateScript({Test-Path $_})]
    [String]
    $filename
)

function color2excel($color)
{
    $color.R * 256*256 + $color.G * 256 + $color.B
}
$image = New-Object System.Drawing.Bitmap $filename
$o = New-Object -ComObject Excel.Application

$wb = $o.Workbooks.Add()
$sh = $wb.ActiveSheet
$sh.Cells.ColumnWidth =0.25;
$sh.Cells.RowHeight = 2.5;
$o.Visible = $true
$total = $image.Width * $image.Height
foreach($x in 0..($image.Width-1))
{
    foreach($y in 0..($image.Height-1))
    {
        Write-Progress "Exporting..." "$x,$y" -PercentComplete  ((($x * $image.Height + $y) / $total ) * 100)
        $sh.Cells.Item($y+1, $x+1).Interior.Color = color2excel($image.GetPixel($x, $y))
    }
}