Jump to content
Sign in to follow this  
ishldgetoutmore

Dumb Script Question

Recommended Posts

ishldgetoutmore

I've got a script I cobbled together from parts other people here have supplied. Coming from a background where I've never done any real scripting (I've never considered myself a coder of even novice ability), reading up on GDIPlus and its powerful utility is a big eye-opener.

Anyway the script is as follows:

; #SPECIALIZED IMAGE CONVERSION SCRIPT# =========================================================================================
; Name ..........: SpecImageConvert.au3
; AutoIt Version : 3.3.6
; Language ......: English
; Description ...: Queries the user to select files and then crops them, converts them to b&w, and publishes them to PDF format.
; Author(s) .....: Roland Volz
; Thanks To .....: danwilli for the crop, UEZ for the inversion, Authenticity for GDIP.au3 and the tutorial.
; Modified.......:
; Remarks .......:
; Related .......: 
; Link ..........: http://www.autoitscript.com/forum/index.php?showtopic=114533&view=findpost&p=800948
;                  http://www.autoitscript.com/forum/index.php?showtopic=115392&st=0&p=806028&#entry806028
; Example .......: No
; ===============================================================================================================================

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <Misc.au3>

;Image Crop Settings
Global $ImageCropPosX = 0, $ImageCropPosY = 48, $ImageCropWidth = 880, $ImageCropHeight = 642

;Text Box Settings For Image AFTER Crop
Global $TextBoxWidth = 100, $TextBoxHeight = 17, $TextBoxPosX = 400, $TextBoxPosY = 625, $TextColor = 0xFFFFFFFF, $TextBGcolor = 0xFF000000, $TextFont = "Arial", $TextSize = 10, $TextStyle = 1 ;1 = Bold

Global $hImageContext, $hBackImage

; Query user for files to modify. All files must be PNG format.
$Message = "Hold down Ctrl or Shift to choose multiple files."
$Select = FileOpenDialog($Message, @WorkingDir & "\", "Images (*.png)", 1 + 4 )

; If no files are selected, return an error. Otherwise, split the selections into separate elements of array $Pic. $Pic[0] is the
; number of elements in the array, $Pic[1] is the directory containing the files.
If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
    $Pic = StringSplit($Select, "|")
EndIf

; Initialize the GDIPlus tools.
_GDIPlus_Startup()
$hBrush = _GDIPlus_BrushCreateSolid($TextColor)
$hBrush2 = _GDIPlus_BrushCreateSolid($TextBGcolor)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate($TextFont)
$hFont = _GDIPlus_FontCreate($hFamily, $TextSize, $TextStyle)
$tLayout = _GDIPlus_RectFCreate($TextBoxPosX, $TextBoxPosY, $TextBoxWidth, $TextBoxHeight)
$scale_factor = 1.0


; Take each file, crop it, add the title, and save it as a JPG file. If a JPG file already exists with the specified name, then it
; ignores that one and moves on to the next item in the files list.
For $a = 2 To UBound($Pic,1) - 1
    $PicNewName = StringReplace($Pic[$a], '.png', '.jpg')
    If Not FileExists($Pic[1] & '\' & $PicNewName) Then
        Local $tNegMatrix, $pNegMatrix
        $dll = DllOpen("user32.dll")
        $hImage = _GDIPlus_ImageLoadFromFile($Pic[$a])
        $hClone = _GDIPlus_BitmapCloneArea($hImage, $ImageCropPosX, $ImageCropPosY, $ImageCropWidth, $ImageCropHeight)
        $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hClone)
        $hImageContext = _GDIPlus_ImageGetGraphicsContext($hClone)
        $tNegMatrix = _GDIPlus_ColorMatrixCreateNegative()
        $pNegMatrix = DllStructGetPtr($tNegMatrix)
        $hIA = _GDIPlus_ImageAttributesCreate()
        $iX = _GDIPlus_ImageGetWidth($hClone) * $scale_factor
        $iY = _GDIPlus_ImageGetHeight($hClone) * $scale_factor
        _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pNegMatrix)
        _GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $ImageCropWidth, $iX, $iY, 0, 0, $iX, $iY, $hIA)
        _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBackImage, 0, 0, $iX, $iY, 0, 0, $iX, $iY)
        $tNegMatrix = 0
        _GDIPlus_GraphicsFillRect($hImageContext, $TextBoxPosX, $TextBoxPosY + 1, $TextBoxWidth, $TextBoxHeight, $hBrush2)
        _GDIPlus_GraphicsDrawStringEx($hImageContext, $PicNewName, $hFont, $tLayout, $hFormat, $hBrush)
        _GDIPlus_ImageSaveToFile($hClone, $Pic[1] & '\' & $PicNewName)
        _GDIPlus_GraphicsDispose($hImageContext)
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_BitmapDispose($hImage)
        _GDIPlus_BitmapDispose($hClone)
        _WinAPI_DeleteObject($hClone)
    EndIf
Next
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_FontDispose($hFont)
_GDIPlus_Shutdown()

I'm trying to perform a series of specific operations on images like the one below:

Posted Image

And turn them into something like this (minus the black bars, of course :mellow::

Posted Image

I think the problem is probably where I try to turn the graphic into the negative, but the end result is nothing. Since I'm not a coder and never took classes, I have to ask, what's the proper way to troubleshoot this? Is there a utility I'm missing which shows you graphically the various elements of the script as it's running?

Share this post


Link to post
Share on other sites
PsaltyDS

Start out by adding error checking to your function calls. For example, _GDIPlus_ImageLoadFromFile() returns -1 and sets @error if it fails. So you could rewrite that:

$hImage = _GDIPlus_ImageLoadFromFile($Pic[$a])
        If @error Then
            MsgBox(16, "Error", "Image load failed from:  " & $Pic[$a])
            ContinueLoop
        EndIf

Or you can just output values to the console in SciTE while the script is running to see how it's going:

$hImage = _GDIPlus_ImageLoadFromFile($Pic[$a])
        ConsoleWrite("Debug: Loaded image; $Pic[" & $a & "] = " & $Pic[$a] & "; $hImage = " & $hImage & "; @error = " & @error & @LF)

You can also run the script in SciTE with this at the top of the script:

#AutoIt3Wrapper_Run_Debug_Mode=Y

This will output some debugging info in the console pane of the SciTE window while the script runs.

:mellow:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
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
Sign in to follow this  

×