Forum Discussion

seanmccole's avatar
seanmccole
Copper Contributor
Feb 17, 2022
Solved

Create a macro to crop images

Hi,

 

I'm trying to create a macro to crop an image to 1:1 ratio but I'm not sure if its possible. 
I'm constantly cropping images and resizing them and it would unbelievably benefit my workflow if i could automate this.

 

I have created a macro to resize all of my images in a  document so that's half the battle.
Any advice would be greatly appreciated!

 

Sean

  • seanmccole The information on using those properties is pretty sparse, but after a few attempts, I believe the applicable property is the .PictureFormat.Crop.PictureOffsetX and that the offset is from the center of the picture.  For the result that you want, probably no offset is required.

     

    Using the following code with an image that was distorted to the size of your image:

     

    With ActiveDocument
        For i = 1 To .InlineShapes.Count
           With .InlineShapes(i)
               .PictureFormat.Crop.ShapeWidth = .Height
               .PictureFormat.Crop.ShapeHeight = .Height
               .PictureFormat.Crop.PictureOffsetY = 0
               .PictureFormat.Crop.PictureOffsetX = 0
           End With
        Next i
    End With

     

    the result shown compared to an uncropped version of the image

    Increasing the offset, by using

    .PictureFormat.Crop.PictureOffsetX = 10

    reduces the crop from the left

    and reducing it by using

    .PictureFormat.Crop.PictureOffsetX = -10

    increases the crop from the left

     

     

     

     

12 Replies

  • opticswhiz's avatar
    opticswhiz
    Copper Contributor

    Hi Sean, I am Yousuf from https://opticswhiz.com/

    Yes, it is definitely possible to create a macro to crop an image to a 1:1 ratio. You can automate this process to save time and improve your workflow. Here's a general outline of the steps you can follow to create such a macro:

    Identify the software you are using to perform the image cropping and resizing. Macros can be created in various applications such as Photoshop, GIMP, or Microsoft Office applications like Excel or Word.

    Determine the specific programming or scripting language that the macro will use. For example, if you are working with Excel, you can create a VBA (Visual Basic for Applications) macro. If you are using image editing software like Photoshop or GIMP, you may need to use a scripting language specific to that software (e.g., JavaScript for Photoshop).

    Learn the basic syntax and commands for the chosen language. This will allow you to write the necessary code for cropping and resizing images.

    Write the macro code that performs the desired cropping and resizing operations. The exact code will depend on the software and language you are using. However, here's a general approach you can follow:

    a. Open the image file.
    b. Determine the original dimensions of the image.
    c. Calculate the dimensions for the 1:1 crop. You can choose the shorter side of the image as the reference and crop a square from there.
    d. Crop the image using the calculated dimensions.
    e. Resize the cropped image to the desired size, if necessary.
    f. Save the modified image.

    Test your macro with a sample image to ensure it works as expected. Make any necessary adjustments to the code if needed.

    Apply the macro to your entire image collection or automate it to process multiple images in a batch. This will save you time and effort.

    Remember, the specific details of creating a macro will vary depending on the software and language you're using, so it's important to consult the documentation or resources specific to your chosen tools.

  • sophia122520's avatar
    sophia122520
    Copper Contributor
    Main Attraction
    We have all been there... right?

    The survey is complete, we have looked through and analysed the collected data. It's time to smash out that report document, only, the coverage map visualisations have a ton of white space surrounding them and the floor plan is minuscule in the centre of the page.

    It's a PITA, but, by creating a macro within Word we can automate (and dramatically speed up) the image cropping and resize actions.

    Create and run your Ekahau report template

    Word > Preferences
    Change measurement unit to points

    Tools > Macro > Visual Basic Editor
    double click "This Document"
    copy and paste the VBA script below

    Sub CropAndResize()
    ' This macro crops and resizes images
    ' The units are 'points'
    Dim oILS As InlineShape
    Set oILS = Selection.InlineShapes(1)
    With oILS
    .PictureFormat.CropLeft = 100
    .PictureFormat.CropTop = 100
    .PictureFormat.CropRight = 100
    .PictureFormat.CropBottom = 100
    End With
    With oILS
    .LockAspectRatio = True
    ' .Height = 260
    ' .Width = 450
    End With
    lbl_Exit:
    Exit Sub
    End Sub
    Click "Save" in the top-left

    You will likely get an error message, cancel

    Save the Document / Macro in the special ".docm" format

    Next, create a shortcut for the macro
    Tools > Customise Keyboard

    Assign a keyboard combo of your choosing

    Test the macro on an image

    The crop values are absolute! Running the macro a second time will not affect the image cumulatively.

    Tools > Macro > Visual Basic Editor
    Make the changes
    Save in the top left
    Close the window

    Run the macro again using your shortcut

    The macro can also be used to resize the image
    To define the width or height using the macro, remove the apostrophe ' from the appropriate line width or height and set the value in points

    If you are unsure what the point value should be, consider the following
    cropping an image using the macro
    resize the image manually within Word
    select the "Picture Format" within the ribbon
    take note of the width or height
    enter the value into the macro and run again

    The macro will need to be applied to each image that requires manipulation, depending on your project, you may need to alter the macro for different shape floors within your report output.When designing your report template, you can use the visualisation tag:

    <#"visualization": {"resolution-width": "-1"}#>
    This tag sets the resolution of the rendered visualization. This time the unit is in pixels. This tag is often used to reduce the resolution and save space in the resulting generated report...

    That is NOT what we are doing here, set to negative 1, to force use the original resolution of the map image.

    This should allow you to crop and enlarge the image within Word without it becoming grainy.

    Windows Users
    Create and run your Ekahau report template

    File > Options > Advanced > Display
    Change:
    Show measurements in units of to: Points

    OK

    (In the Ribbon) View > Macros > View Macros

    Type "CropAndResize" into the Macro Name field
    Click "Create"

    We are now in the MSWord Visual Basic Editor
    Copy and paste the VBA script below in the editor in between the lines of Sub "CropAndResize" and "EndSub"
    Commented out lines within VBA scripts begin with ‘'’

    ' This macro crops and resizes images
    ' The units are 'points'
    Dim oILS As InlineShape
    Set oILS = Selection.InlineShapes(1)
    With oILS
    .PictureFormat.CropLeft = 100
    .PictureFormat.CropTop = 100
    .PictureFormat.CropRight = 100
    .PictureFormat.CropBottom = 100
    End With
    With oILS
    .LockAspectRatio = True
    ' .Height = 260
    ' .Width = 450
    End With
    lbl_Exit:
    Exit Sub
    Click "Save" in the top-left

    You may get an error message, click "cancel"

    Save the Document / Macro into the special ".docm" format

    Once the macro is saved and we are returned to looking at the Word document
    Create a shortcut for the macro
    File > Options > Customise Ribbon
    At the bottom of the screen select Keyboard Shortcuts "Customise"
    Scroll down through the categories (on the left) to Macros

    Make sure the Macro Name is visible and selected in your Macros box (on the right)

    Assign a keyboard combo of your choosing in ‘Press new shortcut key:’
    Click "Assign"
    Then you can close the Options windows

    Test the macro on an image
    by selecting the image and using the newly created keyboard shortcut

    Please Note: The crop values used by this macro are absolute! Running the macro a second time will not affect the image cumulatively.

    If the crop amount is not sufficient, we need to go back to the macro and make some changes.

    (In the Ribbon) View > Macros > View Macros
    Edit

    This time, all we need to do is alter the crop values
    Once complete
    Save in the top left
    Close the window

    Run the macro again using your shortcut .for more information you can visit here https://protrackinghub.com/
  • Unless you want the image to be cropped by the same amount from each of the opposite sides, how would the macro determine from which side the image should be cropped?
    • seanmccole's avatar
      seanmccole
      Copper Contributor

      Doug_Robbins_Word_MVP 

       

      Thanks for the reply Doug.

      When you select and image in word you see the "Picture Format" ribbon and from there you can "crop" the selected image. You get multiple options here, "crop", "crop to shape", "aspect ratio", "fill" and "fit."
      Under the "aspect ratio" tab you get the option to crop to square 1:1.
      This is simply all I want to do.


      When I create reports for work, the software I use exports small rectangular images which I then have to manually crop to a square and enlarge. Sometimes there can be 50-100 images. This means I have to manually click on each image, click on crop, click on aspect ratio, click on square 1:1, then resize. 
      I know that's only 5 clicks per image but multiply that over the entire document and it becomes very laborious. I'd really love to have a shortcut key or something that could crop each image or have a macro that would crop all.


      Maybe this isn't possible? 

      • seanmccole 

         

        Use

        Dim myCrop As Crop
        With ActiveDocument
            For i = 1 To .InlineShapes.Count
                With .InlineShapes(i)
                    Set myCrop = .PictureFormat.Crop
                    If .Height > .Width Then
                        myCrop.ShapeHeight = .Width
                    Else
                        myCrop.ShapeWidth = .Height
                    End If
                End With
            Next i
        End With

         

Resources