Jump to content

How to convert image to Grayscale and save ?


Tukata
 Share

Recommended Posts

  • Moderators

Tukata,

someone can write me the script

Have you ever heard of the old saying: "Give a man a fish, you feed him for a day; give a man a net and you feed him forever"?

Well, in this forum we try to be net makers and repairers, not fishmongers. Give it a try yourself first and come back if you run into problems. You know where we are! :D

M23

P.S. Did you even try "Search" - I did and there a loads of posts out there. :huggles:

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I am tryig this script and can not find my mistake.

#include <FreeImage.au3>
_FreeImage_LoadDLL(@ScriptDir&"\FreeImage.dll")
_FreeImage_Initialise()
$sFile = @ScriptDir & "\flagirl.bmp"
$FIF = _FreeImage_GetFIFFromFilenameU($sFile)
$hImage = _FreeImage_LoadU($FIF, $sFile)
_FreeImage_ConvertToGreyscale($hImage)
$sFileName = @ScriptDir & "\New.bmp"
_FreeImage_SaveU($FIF, $hImage, $sFileName)
_FreeImage_Unload($hImage)
_FreeImage_DeInitialise()

Please help

tukata

Link to comment
Share on other sites

#Include <FreeImage.au3>

$sFile = 'Test.jpg'

_FreeImage_LoadDLL(@ScriptDir & '\FreeImage.dll')
_FreeImage_Initialise()

$FIF = _FreeImage_GetFileTypeU($sFile)
If $FIF = $FIF_UNKNOWN Then
    $FIF = _FreeImage_GetFIFFromFilenameU($sFile)
EndIf
$hImage = _FreeImage_LoadU($FIF, $sFile)
$hGrey = _FreeImage_ConvertToGreyscale($hImage)
_FreeImage_SaveU($FIF, $hGrey, @ScriptDir & '\Test_Grey.jpg')
_FreeImage_Unload($hImage)
_FreeImage_Unload($hGrey)

_FreeImage_DeInitialise()

Link to comment
Share on other sites

Hi, I found the following script in this forum somewhere ....

#Include <GDIPlus.au3>

_GDIPlus_Startup()

$img = FileOpenDialog("Select an image", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.gif)", 1)

$hImage1 = _GDIPlus_ImageLoadFromFile($img)
$hImage2 = _GDIPlus_ImageGreyscale($hImage1)
$ext = StringMid($img, StringLen($img) -2)
 $sCLSID = _GDIPlus_EncodersGetCLSID ($ext)
_GDIPlus_ImageSaveToFileEx($hImage2, StringMid($img, 1, StringLen($img) - 4) & "_grey." & $ext, $sCLSID)
$iwx = _GDIPlus_ImageGetWidth($hImage2)
$iwy = _GDIPlus_ImageGetHeight($hImage2)
$hGUI = GUICreate("Convert image to greyscale by Siao", $iwx, $iwy)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iwx, $iwy)

While GUIGetMsg() <> -3 * Sleep(50)
WEnd

_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

_GDIPlus_ShutDown()
Exit

;; _GDIPlus_ImageGreyscale()
;;    Creates a greyscale copy of Image object and returns its handle. To destroy it, use _GDIPlus_ImageDispose() or _GDIPlus_BitmapDispose()
Func _GDIPlus_ImageGreyscale(Const ByRef $hImage)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage), $hGraphics, $hGraphics2, $hBitmap
;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;greyscale values:
    $x = DllStructSetData($tColorMatrix, 1, 0.30, 1) * DllStructSetData($tColorMatrix, 1, 0.30, 2) * DllStructSetData($tColorMatrix, 1, 0.30, 3) * _
         DllStructSetData($tColorMatrix, 2, 0.59, 1) * DllStructSetData($tColorMatrix, 2, 0.59, 2) * DllStructSetData($tColorMatrix, 2, 0.59, 3) * _
         DllStructSetData($tColorMatrix, 3, 0.11, 1) * DllStructSetData($tColorMatrix, 3, 0.11, 2) * DllStructSetData($tColorMatrix, 3, 0.11, 3) * _
         DllStructSetData($tColorMatrix, 4, 1.00, 4) * DllStructSetData($tColorMatrix, 5, 1.00, 5)
;;create an image attributes object and update its color matrix
    $hImgAttrib = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
;;copy image
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hGraphics2 = _GDIPlus_ImageGetGraphicsContext($hBitmap)
;;draw original into copy with attributes
    _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics2, $hImage, 0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
;;clean up
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hGraphics2)
    _GDIPlus_ImageAttributesDispose($hImgAttrib)

    Return $hBitmap
EndFunc

;;;;; other GDIPlus wrappers ;;;;;

#cs
_GDIPlus_ImageAttributesSetColorMatrix()
    Sets ColorMatrix of ImageAttributes object
    Parameters:
        $hImgAttrib = ImageAttributes object
        $iColorAdjustType = can be:
            ColorAdjustTypeDefault                =  0
            ColorAdjustTypeBitmap                 =  1
            ColorAdjustTypeBrush                  =  2
            ColorAdjustTypePen                    =  3
            ColorAdjustTypeText                   =  4
            ColorAdjustTypeCount                  =  5
            ColorAdjustTypeAny (Reserved)         =  6
        $pColorMatrix = pointer to ColorMatrix structure
        $pGrayMatrix = pointer to GreyMatrix structure
        $iColorMatrixFlags = can be:
            ColorMatrixFlagsDefault               =  0
            ColorMatrixFlagsSkipGrays             =  1
            ColorMatrixFlagsAltGray               =  2
    Return value: True/False
#ce
Func _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, $iColorAdjustType, $pColorMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr",$hImgAttrib, "int",$iColorAdjustType, _
                                            "int",$fEnable, "ptr",$pColorMatrix, "ptr",$pGrayMatrix, "int",$iColorMatrixFlags)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc

;;Creates ImageAttributes object
Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    Return SetError($aResult[0], 0, $aResult[1])
EndFunc

;;Deletes ImageAttributes object
Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc

;; _GDIPlus_GraphicsDrawImageRectRectEx()
;; Same as _GDIPlus_GraphicsDrawImageRectRect(), but adds 1 optional parameter - $hImgAttrib (handle to ImageAttributes object)
Func _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX, $iDstY, $iDstWidth, $iDstHeight, $iUnit = 2, $hImgAttrib = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc

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