Jump to content

ImageTOGreyscale


Go to solution Solved by Skeletor,

Recommended Posts

Not figure out yet ,  any help with this will be great

Thank you

#include <WinAPIShellEx.au3>
#include <GDIPlus.au3>

ShellExecute(ImageTOGreyscale("image.jpg"))

Func ImageTOGreyscale($img)
    _GDIPlus_Startup()
    Local $hIA = _GDIPlus_ImageAttributesCreate()     ;create an ImageAttribute object
    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateGrayScale()     ;create grayscale color matrix
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 4, True, $tColorMatrix, 1) ;set negative color matrix
    Local $hImage1 = _GDIPlus_ImageLoadFromFile(@ScriptFullPath & "\" & $img)

    $ext = StringRegExpReplace($img, "^.*\.", "")
    $file = StringRegExpReplace(@ScriptFullPath, "(^.*\\)(.*)", "\1") & "img_grey." & $ext

;~  ClipPut($file)
;~  MsgBox(0, '', $file)

    $sCLSID = _GDIPlus_EncodersGetCLSID($ext)
    
    MsgBox(0, 'creating img_grey.' & $ext, _GDIPlus_ImageSaveToFileEx($hImage1, $file, $sCLSID))

    _GDIPlus_ImageAttributesDispose($hIA)

    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_Shutdown()
    
    Return $file
EndFunc   ;==>ImageTOGreyscale

 

Link to comment
Share on other sites

  • Solution

I took some inspiration from this post. They used a screen capture, I modified the script to rather open an image.

Try this:
 

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
#include <GDIPlus.au3>

Example()

Func Example()
    _GDIPlus_Startup()

    Local $sInputFile = "image.jpg"
    Local $sOutputFile = "image_gray.jpg"
    Local $hImage = _GDIPlus_ImageLoadFromFile($sInputFile)
    If Not $hImage Then
        ConsoleWriteError("Failed to load image: " & $sInputFile & @CRLF)
        Return False
    EndIf

    Local Const $iWidth = _GDIPlus_ImageGetWidth($hImage)
    Local Const $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $hIA = _GDIPlus_ImageAttributesCreate()
    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix)

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDispose($hGraphics)

    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA)
    _GDIPlus_ImageSaveToFile($hBitmap, $sOutputFile)

    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

 

Edited by Skeletor

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

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...