OVH Cloud OVH Cloud

[PS] Filtre : Out-Image

5 réponses
Avatar
Jean
Je profite du décalage horaire et du gain de sommeil pris de l'autre
côté de la frontière pour soumettre le filtre PowerShell Out-Image à
vos neurones reposés.

Celui-ci transforme un texte en image.

"PowerShell`nIs`nPowerfull"|Out-Image -fg 'Navy' -bg 'Thistle'

°crée une image (out-image.png dans le répertoire courant)
contenant le texte "Powershell is powefull"

Get-Process|Out-String|Out-Image -filename "z:\process.gif"

°crée z:\process.gif, une image contenant la liste des processus

! Ce message s'auto-détruira dans les 5 minutes *UTC* !

Je poste le code en réponse à ce message.

Amicalement,

--
Jean - JMST
Belgium

5 réponses

Avatar
Jean
Je poste le code en réponse à ce message


#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#/2006===================================Version 0==========#
#============ Creates an image from input (string) ============#
#
# To use :
#
# -filename parameter contains path of the image to create.
# Extension (bmp,jpg,png,gif, ...) defines the format
# By default an "out-image.png" image is created in
# current directory.
#
# -fname parameter defines the font name to use in image.
# For console outputs it's better to use a fixed font.
#
# -fsize parameter is the font size in pixel
#
# -fgcolor parameter is text's foreground color
#
# -bgcolor parameter is the image's background color
#
# -fstyle parameter is an enum containing font styles
# ie: -fstyle "bold","italic"
#
# -pixformat parameter us image's pixel format
#
#
# To test :
#
# "PowerShell`nIs`nPowerfull"|Out-Image -fg 'Navy' -bg 'Thistle'
# °creates an image (out-image.png in the current directory)
# containing text "Powershell is powefull"
#
# Get-Process|Out-String|Out-Image -filename "z:process.gif"
# °creates z:process.gif, an image containing process list
#
#
#=================Tested on PowerShell 1 RC2===================#

filter Out-Image(`
$FILENAME="out-image.png",`
$FNAME="Courier New",`
[Single]$FSIZE,`
$FGCOLOR="White",`
$BGCOLOR="Black",`
$FSTYLE="Regular",`
$PIXFORMAT="Format16bppRgb555"`
)
{

#### Loads [system.Drawing] if not already done ####

$assnames=(
[appdomain]::CurrentDomain.GetAssemblies()|`
%{$_.GetName().Name}
)
if(!($assnames -contains "System.Drawing"))
{
[void][System.Reflection.Assembly]::`
LoadWithPartialName("System.Drawing")
}

#### Functions used later ####

function message($varname){
write-host "`nVALUES ALLOWED FOR $varname ARE :`n" `
-BackgroundColor 'yellow' `
-ForegroundColor 'red'
}

function valuesof($enum){
## outputs enumaration values ##
[enum]::GetValues($enum)
}

#### Tests variables conformity ####

$script:stop=$false
$fonts=New-Object "Drawing.Text.InstalledFontCollection"
if(!($fonts.Families -contains $FNAME))
{
message('$FNAME')
$fonts.Families|%{$_.Name}
$script:stop=$true
}

trap [InvalidCastException]
{
message($curvar[1])
valuesof($curvar[0])
$script:stop=$true
continue
}

$curvar=[Drawing.KnownColor],'$FGCOLOR'
[void][Drawing.KnownColor]$FGCOLOR

$curvar=[Drawing.KnownColor],'$BGCOLOR'
[void][Drawing.KnownColor]$BGCOLOR

$curvar=[Drawing.FontStyle],'$FSTYLE'
[void][Drawing.FontStyle]$FSTYLE

$curvar=[Drawing.Imaging.Pixelformat],'$PIXFORMAT'
[void][Drawing.Imaging.Pixelformat]$PIXFORMAT

if($script:stop -eq $true){return}

#### Builds the image from input string ####

## Transforms colors parameters ##
$FGCOLOR=[Drawing.Color]::FromName($FGCOLOR)
$BGCOLOR=[Drawing.Color]::FromName($BGCOLOR)
## Builds a font object with font parameters##
$font=new-object `
"Drawing.Font" `
($FNAME,$FSIZE,$FSTYLE,[Drawing.GraphicsUnit]::Pixel)
##Creates a as little as possible bitmap##
$bitmap=new-object `
"Drawing.Bitmap" `
(1,1)
##Gets graphics from bitmap##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
##Gets string from pipeline##
$text=$_
##Measures the string physical size relative ##
##to font attributes ##
$stringsize=$graphic.MeasureString($text,$font,1000)
##Creates a bitmap sized to the string physical size##
$bitmap=new-object `
"Drawing.Bitmap" `
(`
$stringsize.Width,`
$stringsize.Height,`
[Drawing.Imaging.PixelFormat]$PIXFORMAT`
)
##Gets graphics from bitmap##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
##Create a brush and set its color to $BGCOLOR ##
$brush=New-Object `
"Drawing.SolidBrush" `
($BGCOLOR)
##Paints graphics with $BGCOLOR to cover bitmap's##
##background ##
$graphic.FillRectangle(`
$brush,`
0,`
0,`
$stringsize.Width,`
$stringsize.Height`
)
##Changes brush color to $FGCOLOR##
$brush.Color=$FGCOLOR
##Paints the string with font and $FGCOLOR##
$graphic.DrawString($text,$font,$brush,0.0,0.0)
##Saves the bitmap to $FILENAME##
$bitmap.Save($FILENAME)
}#end filter Out-Image

#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
le filtre PowerShell Out-Image


J'ai un peu complété le code.
Maintenant le filtre retourne le bitmap.

L'usage change un petit peu.

[void]("Windows`nPowerShell"|Out-Image 'image.png' -bg 'Tan')
°crée une image (image.png dans le répertoire courant)
contenant le texte "Windows Powershell"

Get-Process|Out-String|Out-Image -file 'z:process.gif' -no
°crée z:process.gif, une image contenant la liste des processus

$img=(dir|Out-String|Out-Image -fstyle "bold")
$img.Width,$img.Height
$img.Save('dirimage.jpg')
°Crée une image contenant le résultat de dir
, montre la largeur et la hauteur de l'image et la
sauve dans le fichier "dirimage.jpg"

$m=mem|Out-String
Out-Image "mem.bmp" -inputobject $m|Out-Null
°Crée l'image "mem.bmp" contenant le résultat de mem

Je poste le nouveau code en réponse à ce message.

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Je poste le nouveau code en réponse à ce message


#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#/2006===================================Version 0.1========#
#===== Creates and/or saves an image from input (string) ======#
#
# To use :
#
# *Returns an image's bitmap*
#
# -filepath parameter contains path where to save the image.
# Extension (bmp,jpg,png,gif, ...) defines the format
#
# -fname parameter defines the font name to use in image.
# For console outputs it's better to use a fixed font.
#
# -fsize parameter is the font size in pixel
#
# -fgcolor parameter is text's foreground color
#
# -bgcolor parameter is the image's background color
#
# -fstyle parameter is an enum containing font styles
# ie: -fstyle "bold","italic"
#
# -pixformat parameter is image's pixel format
#
# -inputobject defines an input object
#
# -nooutput if this switch is present, $bitmap isn't returned
# (same as cast [void], > $null or |Out-Null)
#
# To test :
#
# [void]("Windows`nPowerShell"|Out-Image 'image.png' -bg 'Tan')
# °creates an image (image.png in the current directory)
# containing text "Windows Powershell" on 2 lines
#
# Get-Process|Out-String|Out-Image -file 'z:process.gif' -no
# °creates z:process.gif, an image containing process list
#
# $img=(dir|Out-String|Out-Image -fstyle 'bold')
# $img.Width,$img.Height
# $img.Save('dirimage.jpg')
# °Creates an image containing dir result
# , shows image's width and height and save it
# to "dirimage.jpg"
#
# $m=mem|Out-String
# Out-Image "u:mem.bmp" -inputobject $m|Out-Null
# °Creates image "u:mem.bmp" containing mem result
#
#=================Tested on PowerShell 1 RC2===================#

filter Out-Image(
[string]$FILEPATH,
$FNAME="Courier New",
[single]$FSIZE,
$FGCOLOR="White",
$BGCOLOR="Black",
$FSTYLE="Regular",
$PIXFORMAT="Format16bppRgb555",
[PSObject]$INPUTOBJECT,
[switch]$NOOUTPUT
)
{

#### Loads [system.Drawing] if not already done ####

$asnames=(
[appdomain]::CurrentDomain.GetAssemblies()|`
%{$_.GetName().Name}
)
if(!($asnames -contains "System.Drawing"))
{
[void][System.Reflection.Assembly]::`
LoadWithPartialName("System.Drawing")
}

#### Functions used later ####

function message($varname){
write-host "`nVALUES ALLOWED FOR $varname ARE :`n" `
-BackgroundColor 'yellow' `
-ForegroundColor 'red'
}

function valuesof($enum){
## outputs enumeration values ##
[enum]::GetValues($enum)
}

#### Tests variant variables conformity ####

$script:stop=$false
$fonts=New-Object "Drawing.Text.InstalledFontCollection"
if(!($fonts.Families -contains $FNAME))
{
message('$FNAME')
$fonts.Families|%{$_.Name}
$script:stop=$true
}

trap [InvalidCastException]
{
message($curvar[1])
valuesof($curvar[0])
$script:stop=$true
continue
}

$curvar=[Drawing.KnownColor],'$FGCOLOR'
[void][Drawing.KnownColor]$FGCOLOR

$curvar=[Drawing.KnownColor],'$BGCOLOR'
[void][Drawing.KnownColor]$BGCOLOR

$curvar=[Drawing.FontStyle],'$FSTYLE'
[void][Drawing.FontStyle]$FSTYLE

$curvar=[Drawing.Imaging.Pixelformat],'$PIXFORMAT'
[void][Drawing.Imaging.Pixelformat]$PIXFORMAT

if($script:stop -eq $true){return}

#### Builds the image from input string ####

## Transforms colors parameters ##
$FGCOLOR=[Drawing.Color]::FromName($FGCOLOR)
$BGCOLOR=[Drawing.Color]::FromName($BGCOLOR)
## Builds a font object with font parameters##
$font=new-object `
"Drawing.Font" `
($FNAME,$FSIZE,$FSTYLE,[Drawing.GraphicsUnit]::Pixel)
##Creates a as little as possible bitmap##
$bitmap=new-object `
"Drawing.Bitmap" `
(1,1)
##Gets graphics from bitmap##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
##Gets string from pipeline or $inputobject##
if($INPUTOBJECT){$text=$INPUTOBJECT}else{$text=$_}
##Measures the string physical size relative ##
##to font attributes ##
$stringsize=$graphic.MeasureString($text,$font,10000)
##Creates a bitmap sized to the string physical size##
$bitmap=new-object `
"Drawing.Bitmap" `
(
$stringsize.Width,
$stringsize.Height,
[Drawing.Imaging.PixelFormat]$PIXFORMAT
)
##Gets graphics from bitmap##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
##Creates a brush and set its color to $BGCOLOR ##
$brush=New-Object `
"Drawing.SolidBrush" `
($BGCOLOR)
##Paints graphics with $BGCOLOR to cover bitmap's##
##background ##
$graphic.FillRectangle(
$brush,
0,
0,
$stringsize.Width,
$stringsize.Height
)
##Changes brush color to $FGCOLOR##
$brush.Color=$FGCOLOR
##Paints the string with font and $FGCOLOR##
$graphic.DrawString($text,$font,$brush,0.0,0.0)
##Saves the bitmap to $FILEPATH if $FILEPATH exists##
if($FILEPATH){$bitmap.Save($FILEPATH)}
##Returns image's bitmap if $NOOUTPUT isn't specified##
if(!$NOOUTPUT){return $bitmap}
}#end filter Out-Image

#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
le filtre PowerShell Out-Image


Voici la version 1.
Un petit nettoyage.
Remplacement du dernier exemple par :

$img=Get-Date|Out-Image -fstyle 'bold'
$img.RotateFlip('Rotate90FlipXY')
$img.Width,$img.Height
$img.Save('date.jpg')
°Crée une image contenant la date et l'heure courante
, effectue une rotation de l'image, affiche la largeur
et la hauteur de l'image, et la sauve sous "date.jpg"

Je poste le code en réponse à ce message.

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Je poste le code en réponse à ce message


#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#/2006===================================Version 1.0========#
#============ Creates an image from input (string) ============#
#
# To use :
#
# *Returns an Drawing.Image Bitmap object*
#
# -filepath parameter contains path where to save the image.
# Extension (bmp,jpg,png,gif, ...) defines the format
#
# -fname parameter defines the font name to use in image.
# For console outputs it's better to use a fixed font.
#
# -fsize parameter is the font size in pixel
#
# -fgcolor parameter is text's foreground color
#
# -bgcolor parameter is the image's background color
#
# -fstyle parameter is an enum containing font styles
# ie: -fstyle "bold","italic"
#
# -pixformat parameter is image's pixel format
#
# -inputobject defines an input object
#
# -autoload loads system.drawing if not already loaded
# without asking the user to confirm
#
# -nooutput if this switch is present, image object isn't
# returned
# (+/- same as cast [void], > $null or |Out-Null)
#
# To test :
#
# [void]("Windows`nPowerShell"|Out-Image 'image.png' -bg 'Tan')
# °creates an image (image.png in the current directory)
# containing text "Windows Powershell" on 2 lines
#
# Get-Process|Out-String|Out-Image -file 'z:process.gif' -no
# °creates z:process.gif, an image containing process list
#
# $img=Get-Date|Out-Image -fstyle 'bold'
# $img.RotateFlip('Rotate90FlipXY')
# $img.Width,$img.Height
# $img.Save('date.jpg')
# °Creates an image containing current date, rotates
# image, shows image's width and height, and saves it
# to "date.jpg"
#
# $m=mem|Out-String
# Out-Image "u:mem.bmp" -inputobject $m|Out-Null
# °Creates image "u:mem.bmp" containing mem result
#
#=================Tested on PowerShell 1 RC2===================#

filter Out-Image(
[string]$FILEPATH,
$FNAME="Courier New",
[single]$FSIZE,
$FGCOLOR="White",
$BGCOLOR="Black",
$FSTYLE="Regular",
$PIXFORMAT="Format16bppRgb555",
[PSObject]$INPUTOBJECT,
[switch]$AUTOLOAD,
[switch]$NOOUTPUT
)
{

#### Functions used later ####

function has-assembly($name){
##Verify of $name assembly is loaded##
[appdomain]::CurrentDomain.GetAssemblies()|
%{
if(
($_.GlobalAssemblyCache) `
-and `
($_.GetName().Name -eq $name)
)
{return $true}
}
}

function message($varname){
write-host "`nOut-Image * VALUES ALLOWED FOR " `
"$varname ARE :`n" `
-BackgroundColor 'yellow' `
-ForegroundColor 'red'
}

function valuesof($enum){
## outputs enumeration values ##
[enum]::GetValues($enum)
}

function load-assembly($name){
[void][System.Reflection.Assembly]::`
LoadWithPartialName($name)
}
#### Loads [system.Drawing] if not already done ####

if(!(has-assembly("System.Drawing")))
{
if($AUTOLOAD){load-assembly("System.Drawing")}
elseif((
$host.UI.PromptForChoice(
"Load System.Drawing ?",
"",
[Management.Automation.Host.ChoiceDescription[]]`
("&No","&Yes"),
$false
)
))
{
load-assembly("System.Drawing")
}
else
{
write-host 'Out-Image needs System.Drawing' `
-BackgroundColor 'yellow' `
-ForegroundColor 'red'
return
}
}

#### Tests variant variables conformity ####

$script:stop=$false
$fonts=New-Object "Drawing.Text.InstalledFontCollection"
if(!($fonts.Families -contains $FNAME))
{
message('$FNAME')
$fonts.Families|%{Write-Host $_.Name}
$script:stop=$true
}

trap [InvalidCastException]
{
message($curvar[1])
$select='.'
if($curvar[1] -eq '$PIXFORMAT'){$select='rgb'}
Write-Host (
valuesof($curvar[0])|
Select-String $select|
Out-String
)
$script:stop=$true
continue
}

$curvar=[Drawing.KnownColor],'$FGCOLOR'
[void][Drawing.KnownColor]$FGCOLOR

$curvar=[Drawing.KnownColor],'$BGCOLOR'
[void][Drawing.KnownColor]$BGCOLOR

$curvar=[Drawing.FontStyle],'$FSTYLE'
[void][Drawing.FontStyle]$FSTYLE

$curvar=[Drawing.Imaging.Pixelformat],'$PIXFORMAT'
[void][Drawing.Imaging.Pixelformat]$PIXFORMAT

if($script:stop -eq $true){return}

#### Builds the image from input string ####

## Builds a font object with font parameters##
$font=new-object `
"Drawing.Font" `
($FNAME,$FSIZE,$FSTYLE,[Drawing.GraphicsUnit]::Pixel)
## Creates a as little as possible bitmap ##
$bitmap=new-object `
"Drawing.Bitmap" `
(1,1)
## Gets graphics from bitmap ##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
## Gets string from pipeline or $inputobject ##
if($INPUTOBJECT){$text=$INPUTOBJECT}else{$text=$_}
## Measures the string physical size relative ##
## to font attributes ##
$stringsize=$graphic.MeasureString($text,$font)
##Creates a bitmap sized to the string physical size##
$bitmap=new-object `
"Drawing.Bitmap" `
(
$stringsize.Width,
$stringsize.Height,
[Drawing.Imaging.PixelFormat]$PIXFORMAT
)
## Gets graphics from bitmap ##
$graphic=[Drawing.Graphics]::FromImage($bitmap)
## Creates a brush and set its color to $BGCOLOR ##
$brush=New-Object `
"Drawing.SolidBrush" `
($BGCOLOR)
## Paints graphics with $BGCOLOR to cover bitmap's ##
## background ##
$graphic.FillRectangle(
$brush,
0,
0,
$stringsize.Width,
$stringsize.Height
)
## Changes brush color to $FGCOLOR ##
$brush.Color=$FGCOLOR
## Paints the string with font and $FGCOLOR ##
$graphic.DrawString($text,$font,$brush,0.0,0.0)
## Saves the bitmap to $FILEPATH if $FILEPATH exists ##
if($FILEPATH)
{$bitmap.Save($FILEPATH)}
## Returns image's bitmap if $NOOUTPUT isn't present ##
if(!$NOOUTPUT)
{return $bitmap}
}#end filter Out-Image

#---8<---Out-Image---PS Filter---Jean-JMST-Belgium---

#Amicalement,

--
Jean - JMST
Belgium