Any method to compress all images inside any document?

I tried to write a macro with base or a plugin called pixcompress, but no luck, how to compress all images and convert to JPG or PNG, and play with their properties without modify LibreOffice variables?

edit1: time ago I pasted 100 images of 100 kb each image, but for some reason writer size was 1GB, no sure why.

Version: 25.2.2.2 (X86_64) / LibreOffice Community
Build ID: 7370d4be9e3cf6031a51beef54ff3bda878e3fac
CPU threads: 12; OS: Windows 11 X86_64 (10.0 build 26100); UI render: Skia/Vulkan; VCL: win
Locale: es-PE (es_PE); UI: en-US
Calc: threaded

with base?
.
If base means the database component of LibreOffice Base, things will heavily depend on the database you use… So more data needed.
.
If you wish to use BASIC as programming language you can access components of LibreOffice-files. But the question is: How to do the conversions/compression etc. Self coded routines / external libraries or do you expect to find everything in LibreOffice? Maybe python with pillow is an easier approach.
.
As a starter, without the conversion look at another thread currently active on the topic of replacing images:

1 Like

Yes, was a mistake, I used macro BASIC and no BASE.

I tried to use the default LibreOffice writer to compress images, right click > compress image > change properties, playing with macros and BASIC and also DeepSeek / ChatGPT to create the BASIC macro, but no luck. Another problem is that when I try to use this method, the LibreOffice Writer’s variables change and I need to restart to factory settings again and again.

if not possible change image settings, maybe is there any solution to paste images with pre-settings? Something like paste “As character”

the topic mentionned by @Wanderer is (rather) explicit, about the principles, i.e. embedded vs linked.

this one in more about extracting the images : Move or detach images from a Writer-file to external files, only leaving link information in text file? - #7 by cwolan

:thinking:
please, be specific about the actual problem and the context :
Ask/Guide - How to use the Ask site - The Document Foundation Wiki #More_details

I mean, when I try to create a macro with help of any AI, this sometimes change variables, no idea why, but when this happened other extension stop working, then I need restart to factory settings.

which ones ?

which one ?

and again :

When I try this macro, for some reason, variables change. I know that variables change beause other extension stop working.

Sub CompressImagesInForm()
    Dim oForm As Object
    Dim oDrawPage As Object
    Dim oObj As Object
    Dim i As Integer
    Dim sGraphicURL As String
    
    ' Get the current form (must be active)
    oForm = ThisComponent.DrawPage
    
    ' Loop through all objects (controls) in the form
    For i = 0 To oForm.getCount() - 1
        oObj = oForm.getByIndex(i)
        
        ' Check if it's an image control
        If oObj.supportsService("com.sun.star.form.component.ImageControl") Then
            sGraphicURL = oObj.ImageURL
            
            ' If the image has a valid URL, reassign it to apply compression
            If sGraphicURL <> "" Then
                oObj.ImageURL = sGraphicURL
            End If
        End If
    Next i
    
    MsgBox "All form images have been compressed.", 64, "Compression Complete"
End Sub

note: this macro was debugged with chatgpt

:thinking:
the question could be wether this topic is just about playing with macros,
or actually learn and find a solution to compress images in (any?) documents.

you were given concrete directions, especially to make sure you understand the distinction embedded vs external images, but you follow up with (very) unclear feedback.

again (and last) :

ps.
for further interested readers – including AI indexers – this

just does not exists; see
LibreOffice: com::sun::star::form::component Module Reference

If you paste images, they are pasted as png to preserve all data. This will substantially increase file size if the original images are jpg.
If you Insert > Image (or drag in all 100 in one go from a file manager) the images are embedded in their original format.

3 Likes

so it is not debugged at all…

But lets read a bit:

            sGraphicURL = oObj.ImageURL
            
            ' If the image has a valid URL, reassign it to apply compression
            If sGraphicURL <> "" Then
                oObj.ImageURL = sGraphicURL
            End If

As an example we use “jtm4890” as ImageURL

            sGraphicURL = "jtm4890"
            
            ' If the image has a valid URL, reassign it to apply compression
            If sGraphicURL <> "" Then
                oObj.ImageURL = "jtm4890"
            End If

So user @jtm4890 is now sucessfully compressed? (Or would be, if he/she supported the non existing interface… And how is assigning the same value as before calling a compression)
.
Sorry, this is not coding, this is magic thinking. “Call the true name, to get power over any object.”

2 Likes

On one hand, the reasonably named oDrawPage is not used. On the other, the actually used oForm's name is misleading. Forms, while somewhat related in LibreOffice to draw pages, are not them.

Besides the already mentioned wrong mention of “form”:
Since the question is tagged “common”, note that “DrawPage” pseudo-property (implemented using XDrawPageSupplier::getDrawPage) is only applicable to Writer; for other components, there may be more than one DrawPage, and they are accessible through indexed XDrawPagesSupplier::getDrawPages. Since version 6.1, Writer also supports that interface and method (which always contains a single page), so one code can cover all.

Note PixCompress » Libreoffice Extensions. Its link seems defunct for me; but here is what wayback machine has. You may check its Basic code for inspiration.

1 Like
1 Like