Solved

Handle Graphic of a Feature in PythonCaller (to add image to MS Word document)

  • 3 November 2022
  • 5 replies
  • 5 views

Userlevel 1
Badge +11

Hi all, Good morning,

 

The challenge we have is: to put a signature (image) in a particular cell in a table in an MS Word document. We have got something working, but we would like to have your kindly advice.

 

Situation:

The signature is read from an AGOL attachment as a blob and with a RasterReplacer and a RasterResampler it is turned into a JPEG (which is visible in the Graphics window). To put this image into the Word document we could use an MSWordStyler where we set Image Source to From Feature.

But because of the very specific location of the image in the doc (in this particular cell in this particular table) we use a Python docx solution in a PythonCaller.

 

Question:

The solution we have now is to temporarily store the image and then retrieve it to put it on the right location in the doc. And this works... But, but, but wouldn't it be possible - inside the PythonCaller - to read the image "From Feature", like in the MSWordStyler...

Any ideas would be highly appreciated.

 

Cheers,

Egge-Jan

icon

Best answer by egge 30 November 2022, 10:24

View original

5 replies

Userlevel 4

It's indeed possible to extract the raster from a feature inside a PythonCaller, but it's fairly complex. You can find some examples of raster manipulation in the PythonCaller here: https://community.safe.com/s/question/0D54Q000080hG7RSAU/python-fme-objects-api-for-raster-manipulation

The most interesting example is by @Takashi Iijima​ near the bottom of the thread, titled "example to calculate statistics of cell values", it shows how to retrieve the raster data in Python.

You'll need to look up the specifics in the fmeobjects API documentation.

However, for your use case it might be easier to simply save the raster in a temporary folder and then read it back into Python, e.g. using Pillow.

Badge +3

here is the small python code to read image

import fme
import fmeobjects
from PIL import Image
import string
import os
import tempfile
 
class small_image(object):
    def __init__(self):
        pass
    def input(self,feature):
        image1 = feature.getGeometry()
        feature.setGeometry(image1)
        self.pyoutput(feature)
    def close(self):
        pass 

 

Userlevel 1
Badge +11

Hi @david_r​ and @f.kemminje​,

Thanks for your prompt replies.  I have tried to apply PIL/Pillow to extract the raster from a feature, but I did not get it running...

As my main goal was to just insert the images in a Word doc I decided to go for the solution where I temporarily save the images (no need for Python there, just using a FeatureWriter there).

So, in the attached template images-to-table-python-docx.fmwt images are created with the MapnikRasterizer, then the images are temporarily stored. And while an MSWordStyler prepares the table in my document, it is a little Python script that is doing some post-processing. Called from a PythonCaller it inserts the images in the correct cells in the table and deletes the temp files. The images are removed from the filesystem before 'Translation was SUCCESSFUL'.

Cheers,

Egge-Jan

Userlevel 4

Hi @david_r​ and @f.kemminje​,

Thanks for your prompt replies.  I have tried to apply PIL/Pillow to extract the raster from a feature, but I did not get it running...

As my main goal was to just insert the images in a Word doc I decided to go for the solution where I temporarily save the images (no need for Python there, just using a FeatureWriter there).

So, in the attached template images-to-table-python-docx.fmwt images are created with the MapnikRasterizer, then the images are temporarily stored. And while an MSWordStyler prepares the table in my document, it is a little Python script that is doing some post-processing. Called from a PythonCaller it inserts the images in the correct cells in the table and deletes the temp files. The images are removed from the filesystem before 'Translation was SUCCESSFUL'.

Cheers,

Egge-Jan

Cool, thanks for sharing your solution!

Badge +3

Thank you!@egge​ 

Reply