jguinch Posted November 4, 2020 Posted November 4, 2020 Today, I had to automate the crop of several image files, by detecting the area used by the picture. The goal is to remove the white borders of the image. I searched on the forum, but didn't find what I need. Please, make your suggestions if you have an easy way... So, here is the function : expandcollapse popup#include <GDIPlus.au3> _GDIPlus_Startup() _ImageAutoCrop(@ScriptDir & "\source.png", @ScriptDir & "\destination.png") Func _ImageAutoCrop($sSrcImage, $sDstImage = "") If Not FileExists($sSrcImage) Then Return SetError(1, 0, 0) If $sDstImage = "" Then $sDstImage = $sSrcImage Local $hImage = _GDIPlus_ImageLoadFromFile($sSrcImage) Local $CoordsY = _GetImageCropSize($hImage) If @error Then Return SetError(2, 0, 0) Local $hClone = _GDIPlus_ImageClone($hImage) _GDIPlus_ImageRotateFlip($hClone, 1) Local $CoordsX = _GetImageCropSize($hClone) _GDIPlus_ImageDispose($hClone) Local $hCrop = _GDIPlus_BitmapCloneArea($hImage, $CoordsX[0], $CoordsY[0], $CoordsX[1], $CoordsY[1]) _GDIPlus_ImageDispose($hImage) Local $bRes = _GDIPlus_ImageSaveToFile($hCrop, $sDstImage) _GDIPlus_ImageDispose($hCrop) Return $bRes EndFunc Func _GetImageCropSize($hImage) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) Local $hClone = _GDIPlus_ImageClone($hImage) Local $tBitmapData = _GDIPlus_BitmapLockBits($hClone, 0, 0, $iWidth, $iHeight) Local $iScan0 = DllStructGetData($tBitmapData , "Scan0") Local $v_BufferA = DllStructCreate("byte[" & $iHeight * $iWidth * 4 & "]", $iScan0) ; Create DLL structure for all pixels Local $AllPixels = StringRegExpReplace(DllStructGetData($v_BufferA, 1), "^0x", "") Local $aPixels = StringRegExp($AllPixels, ".{" & 8 * $iWidth & "}", 3) Local $iYStart, $iYEnd For $i = 0 To UBound($aPixels) - 1 If Not $iYStart And StringRegExp($aPixels[$i], "[^F]") Then $iYStart = $i ExitLoop EndIf Next If Not $iYStart Then SetError(1, 0, 0) For $i = UBound($aPixels) - 1 To 0 Step -1 If Not $iYEnd And StringRegExp($aPixels[$i], "[^F]") Then $iYEnd = $i ExitLoop EndIf Next _GDIPlus_BitmapUnlockBits($hClone, $tBitmapData ) ; releases the locked region _GDIPlus_ImageDispose($hClone) Local $aReturn = [$iYStart, $iYEnd - $iYStart] Return $aReturn EndFunc Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now