display an image in a report

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

display an image in a report

Oliver Lehmann
Hi list,

I'd like to enhance the FamilyTree plugin by also showing the persons
photo along with its name, dates and so on.

I know several programming languages ranging from ASM/Z8000 to Java
which feeds my family ;)

I must admit, this is my first encounter with Python and also Gramps
which I plan to use in the future (comming from Ahnenblatt).

Now to my problem:

I was able to look up the first photograph assigned to a person and
have it passed to the internal __draw_box method of FamilyTree. There
I call self.doc.add_media_object before the text of the displayed box
is added with self.doc.draw_text.
I'm not yet in the process of "is everything displayed and aligned
correctly". I just want to see, if the image is displayed. And.... it
is not.

Right now I have no clue why?! The filenames I hand over to
add_media_object are sane and each referenced file exist. It is just not
shown.

My modification to the __draw_box method is the extended for loop of
filenames (a new parameter to __draw_box, containing all the images
to be displayed)

         self.doc.draw_box("FTR-box",
                 "",
                 self.scale * box_x,
                 self.scale * box_y,
                 self.scale * self.box_width,
                 self.scale * height)

         x = self.scale * (box_x + self.box_pad)
         y = self.scale * (box_y + self.box_pad)

         for file in filenames:
             if os.path.exists(file):
                 self._user.warn(_("Could add photo to page"),
                       "%s: %s" % (file, _('File does exist')))
                 self.doc.add_media_object(file, "left", 4.0, 4.0)
             else:
                 self._user.warn(_("Could not add photo to page"),
                       "%s: %s" % (file, _('File does not exist')))

         for (style_name, line) in text:
             # Workaround for draw_text not accepting StyledText
             if isinstance(line, StyledText):
                 if not line.get_tags():
                     line = str(line)
             self.doc.draw_text(style_name, line, x, y)
             y += self.__get_font_height(style_name) * 1.2


The warning I added gets displayed "File does exists" + Filename
for each file but the generated PDF only contains the text - no
image. It feels like add_media_object has just zero effect.

Why is it like that? Do I need to enable something or do I need
to wrap something around the image to make it render? I'm out of
clues right now.... please someone enlight me ;)

The complete script with all the modifications can be found here:
   https://gist.github.com/anonymous/3f6bba297e495743e685fe44639d2ab2

Best regards,
Oliver



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: display an image in a report

prculley
When I first started out with Gramps, I did a lot of my work based on other code snippets from Gramps.  I suggest you look at how the Complete Individual report adds its images to reports.
It appears to me that the add_media_object method basically should add the media acting like a single character at the current position.  One thing I don't see in your code is an expansion of the box to make room for that 'character'; it may be that it is getting drawn outside of the available space.  You might try just expanding the box height/width by 4cm and see what happens.

Paul C.

On Wed, Feb 7, 2018 at 6:04 AM, Oliver Lehmann <[hidden email]> wrote:
Hi list,

I'd like to enhance the FamilyTree plugin by also showing the persons
photo along with its name, dates and so on.

I know several programming languages ranging from ASM/Z8000 to Java
which feeds my family ;)

I must admit, this is my first encounter with Python and also Gramps
which I plan to use in the future (comming from Ahnenblatt).

Now to my problem:

I was able to look up the first photograph assigned to a person and
have it passed to the internal __draw_box method of FamilyTree. There
I call self.doc.add_media_object before the text of the displayed box
is added with self.doc.draw_text.
I'm not yet in the process of "is everything displayed and aligned
correctly". I just want to see, if the image is displayed. And.... it
is not.

Right now I have no clue why?! The filenames I hand over to
add_media_object are sane and each referenced file exist. It is just not
shown.

My modification to the __draw_box method is the extended for loop of
filenames (a new parameter to __draw_box, containing all the images
to be displayed)

        self.doc.draw_box("FTR-box",
                "",
                self.scale * box_x,
                self.scale * box_y,
                self.scale * self.box_width,
                self.scale * height)

        x = self.scale * (box_x + self.box_pad)
        y = self.scale * (box_y + self.box_pad)

        for file in filenames:
            if os.path.exists(file):
                self._user.warn(_("Could add photo to page"),
                      "%s: %s" % (file, _('File does exist')))
                self.doc.add_media_object(file, "left", 4.0, 4.0)
            else:
                self._user.warn(_("Could not add photo to page"),
                      "%s: %s" % (file, _('File does not exist')))

        for (style_name, line) in text:
            # Workaround for draw_text not accepting StyledText
            if isinstance(line, StyledText):
                if not line.get_tags():
                    line = str(line)
            self.doc.draw_text(style_name, line, x, y)
            y += self.__get_font_height(style_name) * 1.2


The warning I added gets displayed "File does exists" + Filename
for each file but the generated PDF only contains the text - no
image. It feels like add_media_object has just zero effect.

Why is it like that? Do I need to enable something or do I need
to wrap something around the image to make it render? I'm out of
clues right now.... please someone enlight me ;)

The complete script with all the modifications can be found here:
  https://gist.github.com/anonymous/3f6bba297e495743e685fe44639d2ab2

Best regards,
Oliver



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel
Reply | Threaded
Open this post in threaded view
|

Re: display an image in a report

Oliver Lehmann
Hi Paul,

thanks for your suggestions. I already had a look at
   gramps/plugins/textreport/detancestralreport.py
and
   gramps/gen/plug/report/utils.py
which defines insert_image but it didn't enlight me so far ;)

The idea with "not enough space" also came to my mind and I just removed
the text output in those boxes completly. After that the boxes are just
rendered empty.

I'm not sure if I've to position the image somehow in those boxes? Maybe
it is positioned outside the box? Maybe I've to put something like a frame
around the image? I've no clue how the engine works at all.... I tried to
look on how the PersonEverything.py is placing the image but didn't saw a
difference exept they are wrapping the image in a paragraph.

                 if os.path.exists(filename):
                     self.doc.start_paragraph("PE-Level%d" % min(level, 32))
                     self.doc.add_media_object(filename, "single", 4.0, 4.0)
                     self.doc.end_paragraph()

But it feels like paragraphs are only there to add style information to
the objects in it?!

I tried to surround it with a paragraph like this:

                 self.doc.start_paragraph("FTR-Title")
                 self.doc.add_media_object(file, "left", 4.0, 4.0)
                 self.doc.end_paragraph()

But ended up with an error:

   File "...FamilyTree\FamilyTree.py", line 672, in __draw_box
     self.doc.start_paragraph("FTR-Title")
   File "...gramps\plugins\lib\libcairodoc.py", line 1436, in start_paragraph
     self._active_element.add_child(new_paragraph)
AttributeError: 'NoneType' object has no attribute 'add_child'

Best regards,
Oliver

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gramps-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gramps-devel