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

# Source Code for Module Gnumed.timelinelib.plugin.plugins.exporters.exporttobitmap

      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  import os.path
     20
     21  import wx
     22
     23  from timelinelib.wxgui.utils import _ask_question
     24  from timelinelib.wxgui.utils import WildcardHelper
     25  from timelinelib.wxgui.utils import display_warning_message
     26  from timelinelib.plugin.pluginbase import PluginBase
     27  from timelinelib.plugin.factory import EXPORTER
     28
     29
     30 -class BitmapExporter(PluginBase):
     31
     32 -    def service(self):
     33          return EXPORTER
     34
     35 -    def display_name(self):
     36          return _("Export Current view to Image...")
     37
     38 -    def wxid(self):
     39          from timelinelib.wxgui.frames.mainframe.guicreator import ID_EXPORT
     40          return ID_EXPORT
     41
     42 -    def run(self, main_frame):
     43          export_to_image(main_frame)
     44
     45
     46 -class MultiBitmapExporter(PluginBase):
     47
     48 -    def service(self):
     49          return EXPORTER
     50
     51 -    def display_name(self):
     52          return _("Export Whole Timeline to Images...")
     53
     54 -    def wxid(self):
     55          from timelinelib.wxgui.frames.mainframe.guicreator import ID_EXPORT_ALL
     56          return ID_EXPORT_ALL
     57
     58 -    def run(self, main_frame):
     59          export_to_images(main_frame)
     60
     61
     62 -def export_to_image(main_frame):
     63      path = get_image_path(main_frame)
     64      if path is not None and overwrite_existing_path(main_frame, path):
     65          main_frame.main_panel.timeline_panel.timeline_canvas.SaveAsPng(path)
     66
     67
     68 -def export_to_images(main_frame):
     69      path = get_image_path(main_frame)
     70      if path is not None:
     71          try:
     72              periods, current_period = main_frame.get_export_periods()
     73          except ValueError:
     74              msg = _("The first image contains a Julian day < 0\n\nNavigate to first event or\nUse the feature 'Accept negative Julian days'")
     75              display_warning_message(msg)
     76              return
     77          view_properties = main_frame.main_panel.timeline_panel.timeline_canvas.controller.view_properties
     78          view_properties.set_use_fixed_event_vertical_pos(True)
     79          path_without_extension, extension = path.rsplit(".", 1)
     80          view_properties.set_use_fixed_event_vertical_pos(True)
     81          view_properties.periods = periods
     82          count = 1
     83          paths = []
     84          for period in periods:
     85              path = "%s_%d.%s" % (path_without_extension, count, extension)
     86              if overwrite_existing_path(main_frame, path):
     87                  main_frame.main_panel.timeline_panel.timeline_canvas.Navigate(lambda tp: period)
     88                  main_frame.main_panel.timeline_panel.timeline_canvas.SaveAsPng(path)
     89              count += 1
     90              paths.append(path)
     91          view_properties.set_use_fixed_event_vertical_pos(False)
     92          main_frame.main_panel.timeline_panel.timeline_canvas.Navigate(lambda tp: current_period)
     93          merged_image_path = "%s_merged.%s" % (path_without_extension, extension)
     94          merge_images(paths, merged_image_path)
     95
     96
     97 -def get_image_path(main_frame):
     98      path = None
     99      file_info = _("Image files")
    100      file_types = [("png", wx.BITMAP_TYPE_PNG)]
    101      images_wildcard_helper = WildcardHelper(file_info, file_types)
    102      wildcard = images_wildcard_helper.wildcard_string()
    103      dialog = wx.FileDialog(main_frame, message=_("Export to Image"), wildcard=wildcard, style=wx.FD_SAVE)
    104      if dialog.ShowModal() == wx.ID_OK:
    105          path = images_wildcard_helper.get_path(dialog)
    106      dialog.Destroy()
    107      return path
    108
    109
    110 -def overwrite_existing_path(main_frame, path):
    111      if os.path.exists(path):
    112          overwrite_question = _("File '%s' exists. Overwrite?") % path
    113          return _ask_question(overwrite_question, main_frame) == wx.YES
    114      return True
    115
    116
    117 -def merge_images(images_paths, merged_image_path):
    118      from PIL import Image
    119      images = map(Image.open, images_paths)
    120      widths, heights = zip(*(i.size for i in images))
    121      total_width = sum(widths)
    122      max_height = max(heights)
    123      new_image = Image.new('RGB', (total_width, max_height))
    124      x_offset = 0
    125      for image in images:
    126          new_image.paste(image, (x_offset, 0))
    127          x_offset += image.size[0]
    128      new_image.save(merged_image_path)
    129