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 com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
from com.sun.star.awt import Size

localContext = uno.getComponentContext()
				   
resolver = localContext.ServiceManager.createInstanceWithContext(
				"com.sun.star.bridge.UnoUrlResolver", localContext )

smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
remoteContext = smgr.getPropertyValue( "DefaultContext" )
    
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",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( "com.sun.star.text.TextTable" )

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

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

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

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.

Hello,

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

imageSize = Size(2000,1500)
img.setSize(imageSize)

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.