Skip to main content
Solved

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

  • November 3, 2022
  • 5 replies
  • 86 views

egge
Contributor
Forum|alt.badge.img+14
  • Contributor
  • 102 replies

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

Best answer by egge

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

david_r
Celebrity
  • 8391 replies
  • November 3, 2022

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.


f.kemminje
Contributor
Forum|alt.badge.img+11
  • Contributor
  • 189 replies
  • November 3, 2022

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 

 


egge
Contributor
Forum|alt.badge.img+14
  • Author
  • Contributor
  • 102 replies
  • Best Answer
  • November 30, 2022

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


david_r
Celebrity
  • 8391 replies
  • November 30, 2022

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!


f.kemminje
Contributor
Forum|alt.badge.img+11
  • Contributor
  • 189 replies
  • November 30, 2022

Thank you!@egge​