Deye Posted Wednesday at 08:16 PM Posted Wednesday at 08:16 PM Are there any ways as to how can I distinguish the real names from the ones that arent valid on progress Func _ChangeImageColors($sInputImagePath, $sOutputImagePath, $aColorMap) _GDIPlus_Startup() Local $hImage = _GDIPlus_ImageLoadFromFile($sInputImagePath) If @error Then Return False Local $hGraphics = _GDIPlus_GraphicsCreateFromImage($hImage) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) For $i = 0 To $iWidth - 1 For $j = 0 To $iHeight - 1 Local $iPixelColor = _GDIPlus_BitmapGetPixel($hImage, $i, $j) Local $newColor = $aColorMap[0] _GDIPlus_GraphicsDrawRectangle($hGraphics, $i, $j, 1, 1, $newColor) Next Next _GDIPlus_ImageSaveToFile($hImage, $sOutputImagePath) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() Return True EndFunc ;==>_ChangeImageColors SOLVE-SMART 1
argumentum Posted Wednesday at 08:34 PM Posted Wednesday at 08:34 PM (edited) ..since _GDIPlus_ImageSaveToFile() may exist, at times I make my functions f_GDIPlus_ImageSaveToFile(). That prefix lets me know is my function. @Deye - Great idea. @argumentum - I know. 28 minutes ago, Deye said: Are there any ways as to how can I distinguish the real names from the ones that arent valid on progress ...I may have no clue of what is the question 😅 Edited Wednesday at 08:46 PM by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Deye Posted yesterday at 10:56 AM Author Posted yesterday at 10:56 AM SOrry I meant : on progress ? these functions are not valid functions _GDIPlus_GraphicsCreateFromImage($hImage) _GDIPlus_GraphicsDrawRectangle($hGraphics, $i, $j, 1, 1, $newColor) The goal is to load the image in grayscale, which enhances the ability to capture any text present. This optimizes OCR recognition, as it performs best with specific adjustments like image sharpening. Additionally, An added benefit of the process is to allow renaming the image file for better organization and accessibility. expandcollapse popup#include <GUIConstantsEx.au3> #include <File.au3> #include <GDIPlus.au3> ; Declare GDI+ Constants Global Const $GDIP_ILMREADWRITE = 0x00000000 ; Image Locking Mode: Read/Write ;~ Global Const $GDIP_PXF32ARGB = 0x26200A00 ; Pixel Format: 32bpp ARGB ;~ Global Const $GDIP_PXF32ARGB = 0x26200A00 ; Pixel Format: 32bpp ARGB ; Initialize GDI+ _GDIPlus_Startup() Global $imgPath, $imgList, $currentIndex Global $folderPath = "C:\Users\user\Pictures\" ; Image folder path $imgList = _FileListToArrayRec($folderPath, "*.jpg;*.png;*.gif", 1, 0, $FLTAR_SORT) ;~ _ArrayDisplay($imgList) If @error Or $imgList[0] = 0 Then MsgBox(0, "Error", "No images found in the specified folder.") Exit EndIf $currentIndex = 1 $imgPath = $folderPath & $imgList[$currentIndex] ; Create GUI GUICreate("Image Viewer", 800, 650) ; Create controls Global $imgControl = GUICtrlCreatePic("", 10, 10, 780, 500) Global $txtName = GUICtrlCreateInput($imgList[$currentIndex], 10, 520, 780, 30) ;~ MsgBox (0 ,"", $imgList[$currentIndex] ) GUICtrlSetFont($txtName, 12, 700) Global $btnPrev = GUICtrlCreateButton("Prev", 200, 560, 100, 40) Global $btnNext = GUICtrlCreateButton("Next", 320, 560, 100, 40) Global $btnRename = GUICtrlCreateButton("Rename", 440, 560, 100, 40) GUISetState(@SW_SHOW) _ReloadImage() ; Load & enhance the image when the GUI first shows While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE _GDIPlus_Shutdown() Exit Case $btnRename _RenameFile() Case $btnPrev If $currentIndex > 1 Then $currentIndex -= 1 _ReloadImage() EndIf Case $btnNext If $currentIndex < $imgList[0] Then $currentIndex += 1 _ReloadImage() EndIf EndSwitch WEnd Func _ReloadImage() $imgPath = $folderPath & $imgList[$currentIndex] If FileExists($imgPath) Then ; Enhance the image $hProcessedImage = _dstinctiveGreyForText($imgPath) If Not IsPtr($hProcessedImage) Then MsgBox(0, "Error", "Image processing failed.") Else _GDIPlus_ImageSaveToFile($hProcessedImage, "temp_processed.jpg") GUICtrlSetImage($imgControl, "temp_processed.jpg") ; Set the processed image in the GUI GUISetState(@SW_SHOW) GUICtrlSetData($txtName, StringRegExpReplace($imgList[$currentIndex], "\\..*$", "")) EndIf Else MsgBox(0, "Error", "Image not found: " & $imgPath) EndIf EndFunc ;==>_ReloadImage Func _RenameFile() Local $newName = StringRegExpReplace(GUICtrlRead($txtName), '[\W]+', ' ') If $newName <> "" Then Local $ext = StringRegExpReplace($imgList[$currentIndex], "^.*\.(\w+)$", "$1") Local $newFullPath = $folderPath & $newName & "." & $ext If FileMove($imgPath, $newFullPath, 1) Then $imgList[$currentIndex] = $newName & "." & $ext $imgPath = $newFullPath _ReloadImage() Else MsgBox(0, "Error", "Failed to rename file.") EndIf Else MsgBox(0, "Error", "File name cannot be empty.") EndIf EndFunc ;==>_RenameFile Func _dstinctiveGreyForText($sImagePath) If Not FileExists($sImagePath) Then Return SetError(1, 0, 0) EndIf ; Initialize GDI+ Local $hBitmap = _GDIPlus_BitmapCreateFromFile($sImagePath) If $hBitmap = 0 Then Return SetError(2, 0, 0) Local $iWidth = _GDIPlus_ImageGetWidth($hBitmap) Local $iHeight = _GDIPlus_ImageGetHeight($hBitmap) ; Create a new bitmap for processing Local $hNewBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $GDIP_PXF32ARGB) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hNewBitmap) ; Draw original image onto new bitmap _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight) ; Lock bits to manipulate pixels Local $tBitmapData = _GDIPlus_BitmapLockBits($hNewBitmap, 0, 0, $iWidth, $iHeight, $GDIP_ILMREADWRITE, $GDIP_PXF32ARGB) Local $pScan0 = DllStructGetData($tBitmapData, "Scan0") If $pScan0 Then Local $iStride = DllStructGetData($tBitmapData, "Stride") Local $iTotalBytes = Abs($iStride) * $iHeight Local $tPixels = DllStructCreate("byte[" & $iTotalBytes & "]", $pScan0) ; Process each pixel For $i = 0 To $iTotalBytes - 4 Step 4 Local $b = DllStructGetData($tPixels, 1, $i + 1) Local $g = DllStructGetData($tPixels, 1, $i + 2) Local $r = DllStructGetData($tPixels, 1, $i + 3) ; Convert to grayscale using luminosity formula Local $gray = ($r * 0.3 + $g * 0.59 + $b * 0.11) ; Increase contrast (thresholding effect) If $gray > 128 Then $gray = 255 Else $gray = 0 EndIf ; Set new pixel values DllStructSetData($tPixels, 1, $gray, $i + 1) ; Blue DllStructSetData($tPixels, 1, $gray, $i + 2) ; Green DllStructSetData($tPixels, 1, $gray, $i + 3) ; Red Next ; Unlock bits _GDIPlus_BitmapUnlockBits($hNewBitmap, $tBitmapData) EndIf ; Cleanup _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) Return $hNewBitmap EndFunc ;==>_dstinctiveGreyForText
argumentum Posted 10 hours ago Posted 10 hours ago 15 hours ago, Deye said: these functions are not valid functions _GDIPlus_GraphicsCreateFromImage($hImage) _GDIPlus_GraphicsDrawRectangle($hGraphics, $i, $j, 1, 1, $newColor) ..then how to know why/when/how the AI shows nonsense as a solution ?, ..run it ?, ..then disregard it, as it was hallucinating an answer. My 2 cents Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
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