Jump to content

Adding white space to JPG images in bulk - (Moved)


Recommended Posts

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 post
Share on other sites
  • Developers

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 post
Share on other sites

GDI+ can do it faster and with more robustness :

#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 by Nine
tidy
Link to post
Share on other sites

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 :

1472798810_Addborders.png.472a76b99c916e70dc826426559510bc.png

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 by pixelsearch
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...