TrueSR Posted November 4, 2008 Share Posted November 4, 2008 (edited) Hey all,I made a little script to cut png's into pieces, but it doesn't work like it should.Here is an example and my script :A png :What I supposed to get after my script has run, is 4 png's :The original, the left side, the right side and the center.What I get :The left side :This is correct, this is exactly what i need.The center :It starts from the left, while it should display the center of the image, the size is correct.The Right side is exactly the same as the left side.Script :expandcollapse popup#include <GDIPlus.au3> ; Load PNG file as GDI bitmap _GDIPlus_Startup() $gdi_pngSrc = @ScriptDir&"\back.png" $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ; Get Left, Right & Center from back.png $gdi_hLeftImage = _ImageCut($gdi_hImage, 0, 0, 40, $gdi_imageHeight) $gdi_hRightImage = _ImageCut($gdi_hImage, $gdi_imageWidth-40, 0, 40, $gdi_imageHeight) $gdi_hCenterImage = _ImageCut($gdi_hImage, 40, 0, $gdi_imageWidth-80, $gdi_imageHeight) _GDIPlus_ImageSaveToFile($gdi_hLeftImage, @ScriptDir&"\LEFT_1.png") _GDIPlus_ImageSaveToFile($gdi_hRightImage, @ScriptDir&"\RIGHT_1.png") _GDIPlus_ImageSaveToFile($gdi_hCenterImage, @ScriptDir&"\CENTER_1.png") ; Release resources _GDIPlus_ImageDispose($gdi_hLeftImage) _GDIPlus_ImageDispose($gdi_hRightImage) _GDIPlus_ImageDispose($gdi_hCenterImage) _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Func _ImageCut($hImage, $iX, $iY, $iWidth, $iHeight) Local $iImageWidth, $iImageHeight, $hOldGC, $hNewGC, $hOldBmp, $hNewBmp ; Extract image width and height from PNG $iImageWidth = _GDIPlus_ImageGetWidth($hImage) $iImageHeight = _GDIPlus_ImageGetHeight($hImage) ; Create New image $hOldGC = _GDIPlus_ImageGetGraphicsContext($hImage); Get GC from $hImage $hOldBmp = _GDIPlus_BitmapCreateFromGraphics($iImageWidth, $iImageHeight, $hOldGC); Create Bmp from the old GC $hNewBmp = _GDIPlus_BitmapCloneArea($hOldBmp, $iX, $iY, $iWidth, $iHeight, $GDIP_PXF32ARGB); Get Piece of Bmp $hNewGC = _GDIPlus_ImageGetGraphicsContext($hNewBmp); Get GC from new created Bmp _GDIPlus_GraphicsDrawImage($hNewGC, $hImage, 0, 0); Draw the image, i don't really know why you have to use $hImage _GDIPlus_GraphicsDispose($hOldGC) _GDIPlus_GraphicsDispose($hNewGC) _GDIPlus_BitmapDispose($hOldBmp) Return $hNewBmp EndFuncIt might be a small bug in my script that I just don't see, sorry if it is.Thanks in advance. Edited November 4, 2008 by TrueSR Link to comment Share on other sites More sharing options...
Malkey Posted November 5, 2008 Share Posted November 5, 2008 Hey all, I made a little script to cut png's into pieces, but it doesn't work like it should. Here is an example and my script : A png : What I supposed to get after my script has run, is 4 png's : The original, the left side, the right side and the center. What I get : The left side : This is correct, this is exactly what i need. The center : It starts from the left, while it should display the center of the image, the size is correct. The Right side is exactly the same as the left side. Script : expandcollapse popup#include <GDIPlus.au3> ; Load PNG file as GDI bitmap _GDIPlus_Startup() $gdi_pngSrc = @ScriptDir&"\back.png" $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ; Get Left, Right & Center from back.png $gdi_hLeftImage = _ImageCut($gdi_hImage, 0, 0, 40, $gdi_imageHeight) $gdi_hRightImage = _ImageCut($gdi_hImage, $gdi_imageWidth-40, 0, 40, $gdi_imageHeight) $gdi_hCenterImage = _ImageCut($gdi_hImage, 40, 0, $gdi_imageWidth-80, $gdi_imageHeight) _GDIPlus_ImageSaveToFile($gdi_hLeftImage, @ScriptDir&"\LEFT_1.png") _GDIPlus_ImageSaveToFile($gdi_hRightImage, @ScriptDir&"\RIGHT_1.png") _GDIPlus_ImageSaveToFile($gdi_hCenterImage, @ScriptDir&"\CENTER_1.png") ; Release resources _GDIPlus_ImageDispose($gdi_hLeftImage) _GDIPlus_ImageDispose($gdi_hRightImage) _GDIPlus_ImageDispose($gdi_hCenterImage) _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Func _ImageCut($hImage, $iX, $iY, $iWidth, $iHeight) Local $iImageWidth, $iImageHeight, $hOldGC, $hNewGC, $hOldBmp, $hNewBmp ; Extract image width and height from PNG $iImageWidth = _GDIPlus_ImageGetWidth($hImage) $iImageHeight = _GDIPlus_ImageGetHeight($hImage) ; Create New image $hOldGC = _GDIPlus_ImageGetGraphicsContext($hImage); Get GC from $hImage $hOldBmp = _GDIPlus_BitmapCreateFromGraphics($iImageWidth, $iImageHeight, $hOldGC); Create Bmp from the old GC $hNewBmp = _GDIPlus_BitmapCloneArea($hOldBmp, $iX, $iY, $iWidth, $iHeight, $GDIP_PXF32ARGB); Get Piece of Bmp $hNewGC = _GDIPlus_ImageGetGraphicsContext($hNewBmp); Get GC from new created Bmp _GDIPlus_GraphicsDrawImage($hNewGC, $hImage, 0, 0); Draw the image, i don't really know why you have to use $hImage _GDIPlus_GraphicsDispose($hOldGC) _GDIPlus_GraphicsDispose($hNewGC) _GDIPlus_BitmapDispose($hOldBmp) Return $hNewBmp EndFunc It might be a small bug in my script that I just don't see, sorry if it is. Thanks in advance.Try this:- expandcollapse popup#include <GDIPlus.au3> ; Load PNG file as GDI bitmap _GDIPlus_Startup() $gdi_pngSrc = @ScriptDir & "\back.png" $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ; Get Left, Right & Center from back.png $gdi_hLeftImage = _ImageCut($gdi_hImage, 0, 0, 40, $gdi_imageHeight) $gdi_hRightImage = _ImageCut($gdi_hImage, $gdi_imageWidth - 40, 0, 40, $gdi_imageHeight) $gdi_hCenterImage = _ImageCut($gdi_hImage, 40, 0, $gdi_imageWidth - 80, $gdi_imageHeight) _GDIPlus_ImageSaveToFile($gdi_hLeftImage, @ScriptDir & "\LEFT_1.png") _GDIPlus_ImageSaveToFile($gdi_hRightImage, @ScriptDir & "\RIGHT_1.png") _GDIPlus_ImageSaveToFile($gdi_hCenterImage, @ScriptDir & "\CENTER_1.png") ; Release resources _WinAPI_DeleteObject($gdi_hLeftImage) _WinAPI_DeleteObject($gdi_hRightImage) _WinAPI_DeleteObject($gdi_hCenterImage) _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Func _ImageCut($hImage, $iX, $iY, $iWidth, $iHeight) Local $iImageWidth, $iImageHeight, $hOldGC, $hNewGC, $hOldBmp, $hNewBmp $iImageWidth = _GDIPlus_ImageGetWidth($hImage) $iImageHeight = _GDIPlus_ImageGetHeight($hImage) ; Create New image $hNewBmp = _GDIPlus_BitmapCloneArea($hImage, $iX, $iY, $iWidth, $iHeight, $GDIP_PXF32ARGB); Get Piece of Bmp $hNewGC = _GDIPlus_ImageGetGraphicsContext($hNewBmp); Get GC from new created Bmp _GDIPlus_GraphicsDispose($hNewGC) Return $hNewBmp EndFunc ;==>_ImageCut Link to comment Share on other sites More sharing options...
Malkey Posted November 5, 2008 Share Posted November 5, 2008 I just realized the script can be further condensed. #include <GDIPlus.au3> ; Load PNG file as GDI bitmap _GDIPlus_Startup() $gdi_pngSrc = @ScriptDir & "\back.png" $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ; Get Left, Right & Center from back.png $gdi_hLeftImage = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, 0, 40, $gdi_imageHeight, $GDIP_PXF32ARGB) $gdi_hRightImage = _GDIPlus_BitmapCloneArea($gdi_hImage, $gdi_imageWidth - 40, 0, 40, $gdi_imageHeight, $GDIP_PXF32ARGB) $gdi_hCenterImage = _GDIPlus_BitmapCloneArea($gdi_hImage, 40, 0, $gdi_imageWidth - 80, $gdi_imageHeight, $GDIP_PXF32ARGB) _GDIPlus_ImageSaveToFile($gdi_hLeftImage, @ScriptDir & "\LEFT_1.png") _GDIPlus_ImageSaveToFile($gdi_hRightImage, @ScriptDir & "\RIGHT_1.png") _GDIPlus_ImageSaveToFile($gdi_hCenterImage, @ScriptDir & "\CENTER_1.png") ; Release resources _WinAPI_DeleteObject($gdi_hLeftImage) _WinAPI_DeleteObject($gdi_hRightImage) _WinAPI_DeleteObject($gdi_hCenterImage) _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Link to comment Share on other sites More sharing options...
TrueSR Posted November 5, 2008 Author Share Posted November 5, 2008 (edited) I just realized the script can be further condensed. #include <GDIPlus.au3> ; Load PNG file as GDI bitmap _GDIPlus_Startup() $gdi_pngSrc = @ScriptDir & "\back.png" $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ; Get Left, Right & Center from back.png $gdi_hLeftImage = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, 0, 40, $gdi_imageHeight, $GDIP_PXF32ARGB) $gdi_hRightImage = _GDIPlus_BitmapCloneArea($gdi_hImage, $gdi_imageWidth - 40, 0, 40, $gdi_imageHeight, $GDIP_PXF32ARGB) $gdi_hCenterImage = _GDIPlus_BitmapCloneArea($gdi_hImage, 40, 0, $gdi_imageWidth - 80, $gdi_imageHeight, $GDIP_PXF32ARGB) _GDIPlus_ImageSaveToFile($gdi_hLeftImage, @ScriptDir & "\LEFT_1.png") _GDIPlus_ImageSaveToFile($gdi_hRightImage, @ScriptDir & "\RIGHT_1.png") _GDIPlus_ImageSaveToFile($gdi_hCenterImage, @ScriptDir & "\CENTER_1.png") ; Release resources _WinAPI_DeleteObject($gdi_hLeftImage) _WinAPI_DeleteObject($gdi_hRightImage) _WinAPI_DeleteObject($gdi_hCenterImage) _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Thank you very much ! Works great. If i only knew it was this simple, i didn't even know you can use _GDIPlus_BitmapCloneArea on an loaded image without the converting to a bitmap... Edited November 5, 2008 by TrueSR Link to comment Share on other sites More sharing options...
Mallikarjun Posted November 10, 2011 Share Posted November 10, 2011 Hi all, how to cut the top portion of the image......................Reply is should be appreciate.. Link to comment Share on other sites More sharing options...
Mallikarjun Posted November 10, 2011 Share Posted November 10, 2011 Thanks . I got it .......................... Link to comment Share on other sites More sharing options...
Mallikarjun Posted November 11, 2011 Share Posted November 11, 2011 (edited) Hi, I have a problem to cut the multiple images from the folder, plz check this code and tel me what is the wrong this code.................... ..Problem is its not cutting and saving the all the image file from the folder..plz help me ..Reply should be appreciate.. Global $imgFile = "C:autoitTestcaseHistory" &@MDAY & @MON & @YEAR & "_" &@HOUR & @MIN &@SEC&".bmp" Func imgcut1() ;$gdi_pngSrc = "C:ImagesCalculatorCalculator_09112011_150408.bmp" ;$file2=_FileListToArray("C:image2","*.bmp",1) For $i=1 To 7 _GDIPlus_Startup() $filesToArraylist=_FileListToArray("C:ImagesCalculator","*.bmp",1) $Imagefiles = _GDIPlus_ImageLoadFromFile("C:ImagesCalculator" & $filesToArraylist[$i]) ;MsgBox(0, "", $gdi_hImage) ; Extract image width and height from PNG $Width = _GDIPlus_ImageGetWidth($Imagefiles) $Height = _GDIPlus_ImageGetHeight($Imagefiles) MsgBox(0, "", $gdi_pngSrc[$i]) ; Get Left, Right & Center from back.png $Topimgcut = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, 20,$Width,$Height - 20, $GDIP_PXF32ARGB) _GDIPlus_ImageSaveToFile($Topimgcut, $imgFile) ; Release resources _WinAPI_DeleteObject($Topimgcut) _GDIPlus_ImageDispose($Imagefiles) Next _GDIPlus_Shutdown() EndFunc Thanks&Regards Mallikarjun Edited November 11, 2011 by Mallikarjun Link to comment Share on other sites More sharing options...
Malkey Posted November 11, 2011 Share Posted November 11, 2011 Try this. #include <GDIPlus.au3> #include <File.au3> Local $sFolder = "C:\Images\Calculator\" ; @TempDir & "\" ; Local $aFilesToArraylist = _FileListToArray($sFolder, "*.bmp", 1) For $j = 1 To UBound($aFilesToArraylist) - 1 ;ConsoleWrite($aFilesToArraylist[$j] & @LF) _ImageCut($ImageFileSrc, 7) Next ; Cut Image file name, $SrceFile, into $NumPieces number of pieces. ; Returns $NumPieces files e.g. if $SrceFile = "c:\dir\image.bmp" with $NumPieces = 3 then 3 files will be returned ; named "c:\dir\image_00.bmp", "c:\dir\image_01.bmp", and "c:\dir\image_03.bmp" Func _ImageCut($SrceFile, $NumPieces) Local $gdi_CutImage _GDIPlus_Startup() Local $gdi_hImage = _GDIPlus_ImageLoadFromFile($SrceFile) Local $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) Local $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ConsoleWrite($gdi_imageWidth & " x " & $gdi_imageHeight & @LF) For $i = 0 To $NumPieces - 1 $gdi_CutImage = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, Int($gdi_imageHeight * $i / $NumPieces), $gdi_imageWidth, Ceiling($gdi_imageHeight / $NumPieces), $GDIP_PXF32ARGB) ;ConsoleWrite(0 & " " & Int($gdi_imageHeight * $i / $NumPieces) & " " & $gdi_imageWidth & " " & Int($gdi_imageHeight * ($i + 1) / $NumPieces) & @LF) _GDIPlus_ImageSaveToFile($gdi_CutImage, StringRegExpReplace($SrceFile, "(.+\\)(.+)(\..*)", "$1${2}_" & StringRight("0" & $i, 2) & "$3")) _WinAPI_DeleteObject($gdi_CutImage) Next _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() EndFunc ;==>_ImageCut Link to comment Share on other sites More sharing options...
Mallikarjun Posted November 11, 2011 Share Posted November 11, 2011 (edited) Hi, Malkey, Thanks for Reply..............I want to cut the Top portion of the images and store it in to the same folder..... from your example i am not getting where the images are getting store Please see the attached images.. #include <GDIPlus.au3> #include<file.au3> _GDIPlus_Startup() ;$gdi_pngSrc = @ScriptDir & "arjun1.bmp" $gdi_pngSrc=_FileListToArray("C:ImagesCalculator","*.bmp",1) ;$file2=_FileListToArray("C:image2","*.bmp",1) For $i=1 To $i=3 $gdi_hImage = _GDIPlus_ImageLoadFromFile($gdi_pngSrc[$i]) ;MsgBox(0, "", $gdi_hImage) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ;MsgBox(0, "", $gdi_pngSrc[$i]) $TopImage1 = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, 20,$gdi_imageWidth,$gdi_imageHeight - 20, $GDIP_PXF32ARGB) _GDIPlus_ImageSaveToFile($TopImage1, $gdi_pngSrc) ;Here i want to save the images in same folder from which i would have taken, _WinAPI_DeleteObject($TopImage1) Next _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Thanks.. MallikarjunBefoure Cutting.bmpAfter cutting.bmp Edited November 11, 2011 by Mallikarjun Link to comment Share on other sites More sharing options...
Malkey Posted November 11, 2011 Share Posted November 11, 2011 (edited) Referring to the For - Next loop. Instead of "UBound($gdi_pngSrc) - 1" being used, the first element of the array returned from _FileListToArray(), namely $gdi_pngSrc[0], could have been used. expandcollapse popup#include <GDIPlus.au3> #include<file.au3> #include <Array.au3> Local $gdi_hImage, $gdi_imageWidth, $gdi_imageHeight, $TopImage1 _GDIPlus_Startup() ;$gdi_pngSrc = @ScriptDir & "arjun1.bmp" Local $gdi_pngSrc = _FileListToArray("C:ImagesCalculator", "*.bmp", 1) ;$file2=_FileListToArray("C:image2","*.bmp",1) If IsArray($gdi_pngSrc) Then _ArrayDisplay($gdi_pngSrc) ; View contents of $gdi_pngSrc Else MsgBox(0, "Error", "Files not found in directory") EndIf For $i = 1 To UBound($gdi_pngSrc) - 1 $gdi_hImage = _GDIPlus_ImageLoadFromFile("C:ImagesCalculator" & $gdi_pngSrc[$i]) ;MsgBox(0, "", $gdi_hImage) ; Extract image width and height from PNG $gdi_imageWidth = _GDIPlus_ImageGetWidth($gdi_hImage) $gdi_imageHeight = _GDIPlus_ImageGetHeight($gdi_hImage) ;MsgBox(0, "", $gdi_pngSrc[$i]) $TopImage1 = _GDIPlus_BitmapCloneArea($gdi_hImage, 0, 20, $gdi_imageWidth, $gdi_imageHeight - 20, $GDIP_PXF32ARGB) _GDIPlus_ImageSaveToFile($TopImage1, "C:ImagesCalculatorimageTop" & $i & ".bmp") ; If not full path of file given then file is saved in script directory. _WinAPI_DeleteObject($TopImage1) Next _GDIPlus_ImageDispose($gdi_hImage) _GDIPlus_Shutdown() Edit: Added full file path to _GDIPlus_ImageLoadFromFile() function. Edited November 11, 2011 by Malkey Link to comment Share on other sites More sharing options...
Mallikarjun Posted November 12, 2011 Share Posted November 12, 2011 Thanks Mr. Malkey, Its working exact as per my requirement thank u very much ................................. Link to comment Share on other sites More sharing options...
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