quinta-feira, 9 de novembro de 2017

Aplicando Efeito de Negativo

No post anterior, ilustramos algumas formas de converter uma imagem colorida para tons de cinza.

Agora, demonstraremos como é simples aplicar um efeito de negativo a uma imagem. O truque que vamos usar é o de efetuar um XOR (OU Exclusivo) sobre cada canal RGB do pixel com o valor 255 e salvar a imagem correspondente.

Sem mais delongas, vamos ao código:
Function Negativo([System.Drawing.Image]$Img) {
    Write-Host -ForegroundColor Green "Convertendo a imagem para o seu negativo..."
    Write-Host -ForegroundColor Green "Será salvo na mesma pasta um arquivo NomeOriginal_Negativo"
    [Int32]$p=0
    Foreach($y in (0..($Img.Height-1))) {
        $p++
        If ($p -gt (($Img.Height)*0.05)) { 
            Write-Host -NoNewLine '.'
            $p = 0
        }
        Foreach($x in (0..($Img.Width-1))) {
            $ColorPixel = $Img.GetPixel($x,$y)
            ## NEGATIVO
            $R = ($ColorPixel.R) -bxor 255
            $G = ($ColorPixel.G) -bxor 255
            $B = ($ColorPixel.B) -bxor 255

            ## Set Negative Pixel
            $A = $ColorPixel.A
            $Img.SetPixel($x,$y,[System.Drawing.Color]::FromArgb([Int32]$A,[Int32]$R,[Int32]$G,[Int32]$B))
        } ## ForEach $x
    } ## ForEach $y
} ## EndFunction Negativo

Function MaxRGB([Int32]$R, [Int32]$G, [Int32]$B) {
    Return ([math]::Max([math]::Max($R,$G),$B))
}

Function MinRGB([Int32]$R, [Int32]$G, [Int32]$B) {
    Return ([math]::Min([math]::Min($R,$G),$B))
}

$Arquivo = New-Object System.Windows.Forms.OpenFileDialog
$Arquivo.filter = "Imagens (*.PNG;*.BMP;*.JPG;*.GIF)|*.PNG;*.BMP;*.JPG;*.GIF"
$Arquivo.ShowDialog() | Out-Null
$Ext = ($Arquivo.filename).Substring(($Arquivo.filename).Length-3,3)
$ArquivoSemExt = ($Arquivo.filename).Substring(0,($Arquivo.filename).Length-4)
$Img = [System.Drawing.Image]::FromFile($Arquivo.filename)

Clear-Host
Write-Host "##############################################"
Write-Host "Arquivo de Imagem:"$Arquivo.filename
Write-Host "##############################################"

Negativo $Img

Write-Host ""
Write-Host -ForegroundColor Yellow "Conversão concluída!"
$Img.Save($ArquivoSemExt+"_Negativo."+$Ext)
$Img.Dispose()

Abaixo, alguns resultados.

IMAGEM ORIGINAL 1


EFEITO NEGATIVO


IMAGEM ORIGINAL 2


EFEITO NEGATIVO


IMAGEM ORIGINAL 3


EFEITO NEGATIVO

Nenhum comentário:

Postar um comentário