Crop images in a custom manner

    From The Document Foundation Wiki

    Other languages:


    This macro allows you to automate the cropping of images in a customizable manner. To that end, among other things it makes use of[1] Note that simply changing the .GraphicCrop of an image does not execute smoothly, which is why the below macro adjusts the position and size of the image so that it is static and not compressed or stretched after execution of the macro. The macro is also designed to take into account previously applied cropping values to the image, so you can execute the macro multiple times on the same image.

    Note that one main missing feature of this macro is that the image must be in its original size to avoid compression/stretching of the image, upon execution of the macro. (If you find a solution that allows changing the size, feel free to edit this wiki page.) Be wary also that if you paste an image that is larger in pixel dimension than the slide itself, it will be automatically compressed, which would cause stretching of the image when you execute the macro.


    LibreOffice Basic

    'Option Explicit
    Sub custom_crop()
        '''Apply a custom crop of currently selected image.'''
        Dim oGC   As New
        Dim oPos  As New
        Dim oSize As New
        oImage = ThisComponent.CurrentSelection(0)
        x = oImage.getPosition().X
        y = oImage.getPosition().Y
        w = oImage.getSize().Width
        h = oImage.getSize().Height
        ' Specify crop parameters.
        leftCrop = 3000
        rightCrop = 0
        topCrop = 0
        bottomCrop = 0
        ' Apply the graphic crop such that previous crop values are taken into account.
        oGC.Left   = leftCrop   + oImage.GraphicCrop.Left
        oGC.Right  = rightCrop  + oImage.GraphicCrop.Right
        oGC.Top    = topCrop    + oImage.GraphicCrop.Top
        oGC.Bottom = bottomCrop + oImage.GraphicCrop.Bottom
        oImage.GraphicCrop = oGC
        ' Adjust image position & size so that it does not move nor stretch/compress in the crop process.
        ' (Note that the image must still have original size for this to work.)
        oPos.X = x + leftCrop
        oPos.Y = y + topCrop
        oSize.Width  = w - leftCrop - rightCrop
        oSize.Height = h - topCrop  - bottomCrop
    End Sub ' custom_crop()



    Download ODP file