Changeset 626 for mds-and-trees


Ignore:
Timestamp:
10/24/16 15:01:12 (4 years ago)
Author:
konrad
Message:

Added multisampling (antialiasing) option for raster output

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/tree-genealogy.py

    r624 r626  
    118118
    119119    def draw_scale(self, file, filename):
    120         self.add_text(file, "Generated from " + filename.split("\\")[-1], (5, 15), "start")
    121         self.add_dashed_line(file, (self.width*0.7, self.h_margin), (self.width, self.h_margin))
     120        self.add_text(file, "Generated from " + filename.split("\\")[-1], (5, 5), "start")
    122121
    123122        start_text = ""
     
    133132           end_text = "Depth " + str(self.design.props['adepth']['max'])
    134133
    135         self.add_text(file, start_text, (self.width, self.h_margin + 15), "end")
     134        self.add_dashed_line(file, (self.width*0.7, self.h_margin), (self.width, self.h_margin))
     135        self.add_text(file, start_text, (self.width, self.h_margin), "end")
    136136        self.add_dashed_line(file, (self.width*0.7, self.height-self.h_margin), (self.width, self.height-self.h_margin))
    137         self.add_text(file, end_text, (self.width, self.height-self.h_margin + 15), "end")
     137        self.add_text(file, end_text, (self.width, self.height-self.h_margin), "end")
    138138
    139139    def compute_property(self, part, prop, node):
     
    168168
    169169class PngDrawer(Drawer):
    170     def draw_design(self, filename, input_filename, scale="SIMPLE"):
     170
     171    def scale_up(self):
     172        self.width *= self.multi
     173        self.height *= self.multi
     174        self.w_margin *= self.multi
     175        self.h_margin *= self.multi
     176        self.h_no_margs *= self.multi
     177        self.w_no_margs *= self.multi
     178
     179    def scale_down(self):
     180        self.width /= self.multi
     181        self.height /= self.multi
     182        self.w_margin /= self.multi
     183        self.h_margin /= self.multi
     184        self.h_no_margs /= self.multi
     185        self.w_no_margs /= self.multi
     186
     187    def draw_design(self, filename, input_filename, multi=1, scale="SIMPLE"):
    171188        print("Drawing...")
     189
     190        self.multi=multi
     191        self.scale_up()
    172192
    173193        back = Image.new('RGBA', (self.width, self.height), (255,255,255,0))
     
    183203            self.draw_scale(back, input_filename)
    184204
    185         back.show()
     205        #back.show()
     206        self.scale_down()
     207
     208        back.thumbnail((self.width, self.height), Image.ANTIALIAS)
     209
    186210        back.save(filename)
    187211
    188212    def add_dot(self, file, pos, style):
    189213        x, y = int(pos[0]), int(pos[1])
    190         r = style['r']
     214        r = style['r']*self.multi
    191215        offset = (int(x - r), int(y - r))
    192216        size = (2*int(r), 2*int(r))
     
    201225    def add_line(self, file, from_pos, to_pos, style):
    202226        fx, fy, tx, ty = int(from_pos[0]), int(from_pos[1]), int(to_pos[0]), int(to_pos[1])
    203         w = int(style['width'])
     227        w = int(style['width'])*self.multi
    204228
    205229        offset = (min(fx-w, tx-w), min(fy-w, ty-w))
     
    210234        img = Image.new('RGBA', size)
    211235        ImageDraw.Draw(img).line((w, w, size[0]-w, size[1]-w) if (fx-tx)*(fy-ty)>0 else (size[0]-w, w, w, size[1]-w),
    212                                   (int(2.55*c[0]), int(2.55*c[1]), int(2.55*c[2]), int(255*style['opacity'])), int(style['width']))
     236                                  (int(2.55*c[0]), int(2.55*c[1]), int(2.55*c[2]), int(255*style['opacity'])), w)
    213237        file.paste(img, offset, mask=img)
    214238
     
    225249
    226250    def add_text(self, file, text, pos, anchor, style=''):
    227         font = ImageFont.truetype("Vera.ttf", 16)
     251        font = ImageFont.truetype("Vera.ttf", 16*self.multi)
    228252
    229253        img = Image.new('RGBA', (self.width, self.height))
    230254        draw = ImageDraw.Draw(img)
    231255        txtsize = draw.textsize(text, font=font)
    232         pos = pos if anchor == "start" else (pos[0]-txtsize[0], pos[1]-txtsize[1])
     256        pos = pos if anchor == "start" else (pos[0]-txtsize[0], pos[1])
    233257        draw.text(pos, text, (0,0,0), font=font)
    234258        file.paste(img, (0,0), mask=img)
     
    245269
    246270class SvgDrawer(Drawer):
    247     def draw_design(self, filename, input_filename, scale="SIMPLE"):
     271    def draw_design(self, filename, input_filename, multi=1, scale="SIMPLE"):
    248272        print("Drawing...")
    249273        file = open(filename, "w")
     
    650674    parser.add_argument('-c', '--config', dest='config', default="", help='config file name ')
    651675
    652     #TODO: better names for those parameters
    653676    parser.add_argument('-W', '--width', default=600, type=int, dest='width', help='width of the output image (600 by default)')
    654     parser.add_argument('-H', '--height', default=800, type=int, dest='height', help='heigt of the output image (800 by default)')
     677    parser.add_argument('-H', '--height', default=800, type=int, dest='height', help='height of the output image (800 by default)')
     678    parser.add_argument('-m', '--multi', default=1, type=int, dest='multi', help='multisampling factor (applicable only for raster images)')
    655679
    656680    parser.add_argument('-t', '--time', default='GENERATIONAL', dest='time', help='values on vertical axis (BIRTHS/GENERATIONAL(d)/REAL); '
     
    701725    else:
    702726        drawer = PngDrawer(designer, args.config, w=args.width, h=args.height)
    703     drawer.draw_design(args.output, args.input, scale=SCALE)
     727    drawer.draw_design(args.output, args.input, multi=args.multi, scale=SCALE)
    704728
    705729
Note: See TracChangeset for help on using the changeset viewer.