Gnumed.timelinelib.plugin.plugins.exporters

Package Gnumed :: Package timelinelib :: Package plugin :: Package plugins :: Package exporters |
[frames] | no frames]

# Source Code for Package Gnumed.timelinelib.plugin.plugins.exporters

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  """
     20
     21  """
     22
     23
     24  import os
     25
     26  import wx
     27
     28  from timelinelib.wxgui.utils import _ask_question
     29  from timelinelib.wxgui.utils import WildcardHelper
     30  from timelinelib.wxgui.utils import display_warning_message
     31  from timelinelib.proxies.drawingarea import DrawingAreaProxy
     32
     33
     34  EXPORTER = "exporter"
     35
     36
     37 -def export_to_image(main_frame):
     38      path = _get_image_path(main_frame)
     39      if path is not None and _overwrite_existing_path(main_frame, path):
     40          main_frame.main_panel.timeline_panel.timeline_canvas.SaveAsPng(path)
     41
     42
     43 -def export_to_images(main_frame):
     44      path = _get_image_path(main_frame)
     45      if path is not None:
     46          try:
     47              periods, current_period = main_frame.get_export_periods()
     48          except ValueError:
     49              msg = _("The first image contains a Julian day < 0\n\nNavigate to first event or\nUse the feature 'Accept negative Julian days'")
     50              display_warning_message(msg)
     51              return
     52          view_properties = DrawingAreaProxy(main_frame).view_properties
     53          view_properties.set_use_fixed_event_vertical_pos(True)
     54          path_without_extension, extension = path.rsplit(".", 1)
     55          view_properties.set_use_fixed_event_vertical_pos(True)
     56          view_properties.periods = periods
     57          count = 1
     58          paths = []
     59          for period in periods:
     60              path = "%s_%d.%s" % (path_without_extension, count, extension)
     61              if _overwrite_existing_path(main_frame, path):
     62                  main_frame.main_panel.timeline_panel.timeline_canvas.Navigate(lambda tp: period)
     63                  main_frame.main_panel.timeline_panel.timeline_canvas.SaveAsPng(path)
     64              count += 1
     65              paths.append(path)
     66          view_properties.set_use_fixed_event_vertical_pos(False)
     67          main_frame.main_panel.timeline_panel.timeline_canvas.Navigate(lambda tp: current_period)
     68          merged_image_path = "%s_merged.%s" % (path_without_extension, extension)
     69          merge_images(paths, merged_image_path)
     70
     71
     72 -def _get_image_path(main_frame):
     73      path = None
     74      file_info = _("Image files")
     75      file_types = [("png", wx.BITMAP_TYPE_PNG)]
     76      images_wildcard_helper = WildcardHelper(file_info, file_types)
     77      wildcard = images_wildcard_helper.wildcard_string()
     78      dialog = wx.FileDialog(main_frame, message=_("Export to Image"), wildcard=wildcard, style=wx.FD_SAVE)
     79      if dialog.ShowModal() == wx.ID_OK:
     80          path = images_wildcard_helper.get_path(dialog)
     81      dialog.Destroy()
     82      return path
     83
     84
     85 -def _overwrite_existing_path(main_frame, path):
     86      if os.path.exists(path):
     87          overwrite_question = _("File '%s' exists. Overwrite?") % path
     88          return _ask_question(overwrite_question, main_frame) == wx.YES
     89      return True
     90
     91
     92 -def merge_images(images_paths, merged_image_path):
     93      from PIL import Image
     94      images = list(map(Image.open, images_paths))
     95      widths, heights = list(zip(*(i.size for i in images)))
     96      total_width = sum(widths)
     97      max_height = max(heights)
     98      new_image = Image.new('RGB', (total_width, max_height))
     99      x_offset = 0
    100      for image in images:
    101          new_image.paste(image, (x_offset, 0))
    102          x_offset += image.size[0]
    103      new_image.save(merged_image_path)
    104