unity06 Posted March 20, 2021 Share Posted March 20, 2021 I am working on a script which needs to download an image and then add white space to top and bottom of the image in order to make it "square" or in other words turn a 3:2 aspect ratio image into a 1:1 aspect ratio without any trimming, but by simply adding white space below and above the original image. Once this is done the image must then be uploaded elsewhere. This is all part of a cross listing script for ecommerce platforms that I am working on. Can someone suggest if this is at all possible with AutoIT? I was thinking to do this by Automating MS Paint but that wouldnt be a very elegant or fast way of doing it. Many Thanks Link to comment Share on other sites More sharing options...
Developers Jos Posted March 20, 2021 Developers Share Posted March 20, 2021 Moved to the appropriate forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Nine Posted March 20, 2021 Share Posted March 20, 2021 (edited) GDI+ can do it faster and with more robustness : expandcollapse popup#include <GDIPlus.au3> #include <GUIConstants.au3> #include <ScreenCapture.au3> Example() Func Example() _GDIPlus_Startup() Local Const $iW = 300, $iH = 200 ; 3:2 Local Const $iNW = 300, $iNH = 300 ; make it 1:1 Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iW, $iH) ;create a GDI bitmap by capturing part of the screen (3:2) Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore Local $hBitmap_Scaled = _GDIPlus_ImageResizeEX($hBitmap, $iNW, $iNH, $iW, $iH) ;resize image with white space below and above Local $hGUI = GUICreate("GDI+ test", $iNW, $iNH) ;create a test gui to display the resized image GUISetState(@SW_SHOW) Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap_Scaled, 0, 0) ;display scaled image _GDIPlus_ImageSaveToFile($hBitmap_Scaled, "Test.jpg") ; save it While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ;cleanup resources _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_BitmapDispose($hBitmap_Scaled) _GDIPlus_Shutdown() EndFunc ;==>Example Func _GDIPlus_ImageResizeEX($hImage, $iNewWidth, $iNewHeight, $iOldWidth, $iOldHeight) Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iNewWidth, $iNewHeight) Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF) ; fill image with white _GDIPlus_GraphicsFillRect($hBmpCtxt, 0, 0, $iNewWidth, $iNewHeight, $hBrush) _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hImage, 0, Int($iNewHeight - $iOldHeight) / 2, $iOldWidth, $iOldHeight) _GDIPlus_GraphicsDispose($hBmpCtxt) _GDIPlus_BrushDispose($hBrush) Return $hBitmap EndFunc ;==>_GDIPlus_ImageResizeEX Edited March 20, 2021 by Nine tidy pixelsearch and dmob 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
pixelsearch Posted March 20, 2021 Share Posted March 20, 2021 (edited) Great result, bravo Nine imho the function deserves another name than "ImageResizeEX". Of course the final image has a new width & height but it's more an "adding borders" function, without the interpolation that is found in a real resize/scale process. PaintShop Pro separates them like this in its menu : Based on your function, it should be easy now to add from 1 to 4 borders to any image, the dimensions of the borders being constants or based on the original image size (like in your example) AutoIt scripters who need to add borders to their images in bulk should be happy now Edited March 20, 2021 by pixelsearch 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