- def image(self):
- metr = Metric(self, self.scale)
- img = Image.new('RGB', (metr.width, metr.height), self.background_color)
- draw = ImageDraw.Draw(img)
-
- if self.epoch in self.epoch_colors:
- epoch_color = self.epoch_colors[self.epoch]
- else:
- epoch_color = '#000'
- draw.rectangle((0, 0, metr.bar_width, metr.height), fill=epoch_color)
-
- if self.background_img:
- src = Image.open(self.background_img)
- trg_size = (metr.width - metr.bar_width, metr.height)
- if src.size[0] * trg_size[1] < src.size[1] * trg_size[0]:
- resized = (
- trg_size[0],
- src.size[1] * trg_size[0] / src.size[0]
- )
- cut = (resized[1] - trg_size[1]) / 2
- src = src.resize(resized, Image.ANTIALIAS)
- src = src.crop((0, cut, src.size[0], src.size[1] - cut))
- else:
- resized = (
- src.size[0] * trg_size[1] / src.size[1],
- trg_size[1],
- )
- cut = (resized[0] - trg_size[0]) / 2
- src = src.resize(resized, Image.ANTIALIAS)
- src = src.crop((cut, 0, src.size[0] - cut, src.size[1]))