Jump to content

Simple Image Processing


ValorSolo
 Share

Recommended Posts

I've been working on a few apps that do various manipulations to images.

Searching for methods in the forums wasn't as clear cut as I had expected.

Most of the techniques I found were in the help section.

So after getting my own methods worked out, I figured I would share them here.

I only have a couple of manipulations to share at this time, but plan to add more in the future.

Maybe even make a UDF when there's enough code to justify doing so.

I've just started coding with Autoit,so these could probably be optimized.

Here's my method for applying a gradient mask to an image.

Obviously, with the brush set to 1 pixel width, this will only work on an image of a certain height (or width for horizontal gradient), but can be adjusted by increasing either the brush width or not multiplying $i*2.

$bg = _GDIPlus_ImageLoadFromFile (@ScriptDir & "\toolbar.png")
    $bgx = _GDIPlus_ImageGetWidth ($bg)
    $bgy = _GDIPlus_ImageGetHeight ($bg)
    $mask = _GDIPlus_ImageGetGraphicsContext ($bg)
    For $i = 1 To Abs($bgy)
        $brushRGB = "0x80" & Hex(255-($i*2), 2) & Hex(255-($i*2), 2) & Hex(255-($i*2), 2)
        $hPen  = _GDIPlus_PenCreate($brushRGB, 1)
        _GDIPlus_GraphicsDrawLine($mask, 1, 1+$i, $bgx, 1+$i, $hPen   )
        _GDIPlus_PenDispose ($hPen)
    Next
    _GDIPlus_ImageSaveToFile ($bg, $themeDir & "\images\toolbar.png")
    
    _GDIPlus_GraphicsDispose($mask)
    _GDIPlus_ImageDispose ($bg)

And for cropping.

$hImage   = _GDIPlus_ImageLoadFromFile($image)
    $x = _GDIPlus_ImageGetWidth ($hImage)
    $y = _GDIPlus_ImageGetHeight ($hImage)

; toolbar   
$toolbar = _GDIPlus_BitmapCloneArea ($hImage, 0, 17, $x, 120, $GDIP_PXF32ARGB  )
    _GDIPlus_ImageSaveToFile ($toolbar, @ScriptDir & "\toolbar.png")
    
; frame 
$frame = _GDIPlus_BitmapCloneArea ($hImage, 0, 0, $x, 102, $GDIP_PXF32ARGB )
    _GDIPlus_ImageSaveToFile ($frame, $themeDir & "\images\frame.png")

    _WinAPI_DeleteObject($frame)
    _WinAPI_DeleteObject($toolbar)
    _GDIPlus_ImageDispose ($hImage)

Another variation of gradient mask, this one has a glass effect.

$bg = _GDIPlus_ImageLoadFromFile (@ScriptDir & "\toolbar.png")
    $bgx = _GDIPlus_ImageGetWidth ($bg)
    $bgy = _GDIPlus_ImageGetHeight ($bg)
    $mask = _GDIPlus_ImageGetGraphicsContext ($bg)
    For $i = 1 To 50
            $brushRGB = "0x80" & Hex(255-$i, 2) & Hex(255-$i, 2) & Hex(255-$i, 2)
            $hPen  = _GDIPlus_PenCreate($brushRGB, 1)
            _GDIPlus_GraphicsDrawLine($mask, 1, 1+$i, $bgx, 1+$i, $hPen   )
            _GDIPlus_PenDispose ($hPen)
    Next
    For $j = 1 To 70
            $brushRGB = "0x80" & Hex(128-$j, 2) & Hex(128-$j, 2) & Hex(128-$j, 2)
            $hPen  = _GDIPlus_PenCreate($brushRGB, 1)
            _GDIPlus_GraphicsDrawLine($mask, 1, 51+$j, $bgx, 51+$j, $hPen   )
            _GDIPlus_PenDispose ($hPen)
    Next
    _GDIPlus_ImageSaveToFile ($bg, $themeDir & "\images\toolbar.png")
    
    _GDIPlus_GraphicsDispose($mask)
    _GDIPlus_ImageDispose ($bg)

A solid mask. Brush width is set to the width of the image.

$brushRGB = "0x80FF0000"
                
    $bg = _GDIPlus_ImageLoadFromFile ("bg.png")
    $x = _GDIPlus_ImageGetWidth ($bg)
    $y = _GDIPlus_ImageGetHeight ($bg)
    $mask = _GDIPlus_ImageGetGraphicsContext ($bg)
    $hPen = _GDIPlus_PenCreate($brushRGB, $x)
    _GDIPlus_GraphicsDrawRect($mask, 1, 1,$x,$y, $hPen)
    _GDIPlus_ImageSaveToFile ($bg, "toolbar.jpg")
    _GDIPlus_PenDispose         ($hPen)
    _GDIPlus_BrushDispose       ($hBrush1)
    _GDIPlus_GraphicsDispose    ($mask)
    _GDIPlus_ImageDispose       ($bg)

More to come............

Link to comment
Share on other sites

Think the first example should be like this: (You don't need to recreate and destroy the pen every loop)

$bg = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\toolbar.png")
$bgx = _GDIPlus_ImageGetWidth($bg)
$bgy = _GDIPlus_ImageGetHeight($bg)
$mask = _GDIPlus_ImageGetGraphicsContext($bg)

$hPen = _GDIPlus_PenCreate(0x00000000, 1)
For $i = 1 To Abs($bgy)
    $brushRGB = "0x80" & Hex(255 - ($i * 2), 2) & Hex(255 - ($i * 2), 2) & Hex(255 - ($i * 2), 2)
    _GDIPlus_PenSetColor($hPen, $brushRGB)
    _GDIPlus_GraphicsDrawLine($mask, 1, 1 + $i, $bgx, 1 + $i, $hPen)
Next
_GDIPlus_ImageSaveToFile($bg, @ScriptDir & "\out.png")

_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($mask)
_GDIPlus_ImageDispose($bg)
Link to comment
Share on other sites

Why not this

#include <GDIPlus.au3>

_GDIPlus_Startup()

$bg = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\fond.jpg")
$bgx = _GDIPlus_ImageGetWidth($bg)
$bgy = _GDIPlus_ImageGetHeight($bg)
; ---
$mask = _GDIPlus_ImageGetGraphicsContext($bg)
$hPen = _GDIPlus_PenCreate(0x00000000, 1)
; ---

$mult = 1
; Width of the shade = 255 / $mult

For $i = 0 To 255 Step $mult
    $RGBA = "0x" & Hex(255 - $i, 2) & "FFAACC"
    _GDIPlus_PenSetColor($hPen, $RGBA)
    ; ---
    _GDIPlus_GraphicsDrawLine($mask, 0, $i / $mult, $bgx, $i / $mult, $hPen) ; top
    _GDIPlus_GraphicsDrawLine($mask, 0, $bgy - ($i / $mult), $bgx, $bgy - ($i / $mult), $hPen) ; bottom
    ; ---
    _GDIPlus_GraphicsDrawLine($mask, $i / $mult, 0, $i / $mult, $bgy, $hPen) ; left
    _GDIPlus_GraphicsDrawLine($mask, $bgx - ($i / $mult), 0, $bgx - ($i / $mult), $bgy, $hPen) ; right
Next

; ---

_GDIPlus_ImageSaveToFile ($bg, @ScriptDir & "\out.jpg")
; ---
_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose($mask)
_GDIPlus_ImageDispose ($bg)

_GDIPlus_Shutdown()
Link to comment
Share on other sites

Why not this

#include <GDIPlus.au3>

_GDIPlus_Startup()

$bg = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\fond.jpg")
$bgx = _GDIPlus_ImageGetWidth($bg)
$bgy = _GDIPlus_ImageGetHeight($bg)
; ---
$mask = _GDIPlus_ImageGetGraphicsContext($bg)
$hPen = _GDIPlus_PenCreate(0x00000000, 1)
; ---

$mult = 1
; Width of the shade = 255 / $mult

For $i = 0 To 255 Step $mult
    $RGBA = "0x" & Hex(255 - $i, 2) & "FFAACC"
    _GDIPlus_PenSetColor($hPen, $RGBA)
    ; ---
    _GDIPlus_GraphicsDrawLine($mask, 0, $i / $mult, $bgx, $i / $mult, $hPen) ; top
    _GDIPlus_GraphicsDrawLine($mask, 0, $bgy - ($i / $mult), $bgx, $bgy - ($i / $mult), $hPen) ; bottom
    ; ---
    _GDIPlus_GraphicsDrawLine($mask, $i / $mult, 0, $i / $mult, $bgy, $hPen) ; left
    _GDIPlus_GraphicsDrawLine($mask, $bgx - ($i / $mult), 0, $bgx - ($i / $mult), $bgy, $hPen) ; right
Next

; ---

_GDIPlus_ImageSaveToFile ($bg, @ScriptDir & "\out.jpg")
; ---
_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose($mask)
_GDIPlus_ImageDispose ($bg)

_GDIPlus_Shutdown()

This isn't working for me. The result is a solid colored image.

I tried using it like this.

$hPen = _GDIPlus_PenCreate(0x00000000, 1)
$mult = 1
For $i = 0 To 255 Step $mult
    $RGBA = "0x" & Hex(255 - $i, 2) & "000000"
    _GDIPlus_PenSetColor($hPen, $RGBA)
    _GDIPlus_GraphicsDrawLine($mask, 0, $i / $mult, $bgx, $i / $mult, $hPen) ; top
Next
_GDIPlus_PenDispose ($hPen)
Link to comment
Share on other sites

Here's another example that does basically the same as the gradient in my first post, but builds the gradient by adjusting the alpha, not the color.

$bg = _GDIPlus_ImageLoadFromFile ("bg.png")
    $x = _GDIPlus_ImageGetWidth ($bg)
    $y = _GDIPlus_ImageGetHeight ($bg)
    $premask = _GDIPlus_ImageGetGraphicsContext ($bg)
    For $i = 1 To Abs($y)
; for a stronger gradient use
;       $ri = $i*2
;       If $ri > 255 Then $ri = 255
;       $brushRGB = "0x" & Hex(0+$ri,2) & "000000"
; instead of
        $brushRGB = "0x" & Hex(0+$i,2) & "000000"
; ----
        $hPen  = _GDIPlus_PenCreate($brushRGB, 1)
        _GDIPlus_GraphicsDrawLine($premask, 0, 1+$i, $x, 1+$i, $hPen   )
        _GDIPlus_PenDispose ($hPen)
    Next
    _GDIPlus_ImageSaveToFile ($bg, "toolbar.jpg")
    _GDIPlus_ImageSaveToFile ($bg, "toolbar.png")
    _GDIPlus_GraphicsDispose    ($premask)
    _GDIPlus_ImageDispose       ($bg)

I think this is what matwachich's code demonstrates.

Link to comment
Share on other sites

I just noticed that I can't edit my posts. That sucks.

I was going to add the example from my last post in the first post.

And also my example for using PixelGetColor on an image you are working with.

The easiest way I've found to do this is to display the image in the gui.

I've reserved a space in my apps gui for momentarily displaying each image I need a color from.

And then I use this:

GUICtrlCreatePic(@ScriptDir & "\frame.jpg", 480, 0, 6, 102)
If Not WinActive($CTTM,"") Then WinActivate($CTTM,"")
WinWaitActive($CTTM,"")
    $picPos = WinGetPos($CTTM)

$tbC = PixelGetColor($picPos[0]+484, $picPos[1]+110, $CTTM)
    $rgb =  _ColorGetRGB( "0x" & Hex($tbC, 6))

The positioning is a little tricky, so you can use MouseMove($picPos[0]+484, $picPos[1]+110,0) to help you set it correctly.

Also to get better control over GUICtrlCreatePic, check out Yashied's Icons.au3

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...