Set image to original size with Python macro

I am embedding an image in a writer document using Python3. I just can’t figure out how to set the image to the original size.

import uno
import unohelper

# a UNO struct later needed to create a document
from import AS_CHARACTER
from import Size

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
				"", localContext )

smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
remoteContext = smgr.getPropertyValue( "DefaultContext" )
desktop = smgr.createInstanceWithContext( "",remoteContext)

# open a writer document
doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )

text = doc.Text
cursor = text.createTextCursor()
text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
text.insertString( cursor, "Now we are in the second line\n" , 0 )

# create a text table
table = doc.createInstance( "" )

# with 4 rows and 4 columns
table.initialize( 4,4)

text.insertTextContent( cursor, table, 0 )
rows = table.Rows

img = doc.createInstance('') 
img.GraphicURL = 'file:///home/reuben/reuben.jpg' 
tableText = table.getCellByName( "C2" )
cursor = tableText.createTextCursor()
tableText.insertTextContent(cursor, img, False)
img.setPropertyValue('AnchorType', AS_CHARACTER)

Any suggestions? Thanks.

[Edit] LO skews the image size sometimes, and I want it to be the original size. The problem is I don’t know what the original size was.


Not sure about the rest of you code, but here is how to set the size:

imageSize = Size(2000,1500)

where 2000 is width and 1500 is width (the size in 100/th mm ).

If this answers your question please click on the :heavy_check_mark: (upper left area of answer).

Thanks, your answer was the other thing I was confused about. But anyway, I want to set the image to the original size. You know, the size it was taken on the camera. Or should I say the size it shows up on the monitor when opened for the first time. See my edit.

This is what I was looking for:

w = img.ActualSize.Width
h = img.ActualSize.Height
size = Size (w, h)
img.setSize (size)

@reuben Thanks for posting the code. My fault for not being specific on “ActualSize”.

@Ratslinger That’s fine. Your answer pointed me in the right direction. Thanks again.