From bd0e336e3556e293ecc0a4e9711b69dbaf0aebb1 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Mon, 27 Jul 2015 16:51:17 +1000 Subject: [PATCH 1/3] k2flix: core: make sure pixels are square Previously, if you had a FITS file with a non-square array (such as 204367097-c02), the pixels will be squashed to an elongated rectangle. This patch fixes it by passing in the height and width of the figure in the right order. Signed-off-by: Aleksa Sarai --- k2flix/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k2flix/core.py b/k2flix/core.py index 61322a8..5715c89 100644 --- a/k2flix/core.py +++ b/k2flix/core.py @@ -130,7 +130,7 @@ def annotated_image(self, frame=0, dpi=None, vmin=1, vmax=5000, warnings.filterwarnings('ignore', message="(.*)invalid value(.*)") flx[np.isnan(flx) | (flx < vmin)] = vmin # Create the frame - fig = pl.figure(figsize=flx.shape, dpi=dpi) + fig = pl.figure(figsize=flx.shape[::-1], dpi=dpi) ax = fig.add_subplot(111) ax.matshow(flx, aspect='auto', norm=LogNorm(vmin=vmin, vmax=vmax), From 65c550bdcc609076fe180e1eab17f657985dd6a3 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Mon, 27 Jul 2015 23:57:47 +1000 Subject: [PATCH 2/3] k2flix: core: ensure the annotations do not overlap This patchset ensures that the annotations will not overlap by scaling up the figure size so that the figure will always be big enough to fit the annotation (plus some padding). Signed-off-by: Aleksa Sarai --- k2flix/core.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/k2flix/core.py b/k2flix/core.py index 5715c89..8f4b84c 100644 --- a/k2flix/core.py +++ b/k2flix/core.py @@ -13,6 +13,7 @@ import imageio import argparse import numpy as np +import io import matplotlib matplotlib.use('Agg') @@ -26,6 +27,19 @@ from astropy.utils.console import ProgressBar from astropy import log +def find_renderer(fig): + if hasattr(fig.canvas, "get_renderer"): + # Some renderers are nice and just given us get_renderer. + renderer = fig.canvas.get_renderer() + else: + # We have to hack around ones that don't by printing the figure to a + # temporary file object. We can then access the Agg renderer which is + # cached after writing it. Yes, this is ugly but there isn't really + # another way around it. + fig.canvas.print_figure(io.BytesIO()) + renderer = fig._cachedRenderer + return renderer + class BadKeplerFrame(Exception): """Raised if a frame is empty.""" @@ -125,12 +139,15 @@ def annotated_image(self, frame=0, dpi=None, vmin=1, vmax=5000, if dpi is None: # Twitter timeline requires dimensions between 440x220 and 1024x512 dpi = 440 / float(flx.shape[0]) + # DPI needs to be a multiple of two for ffmpeg. + dpi += dpi % 2 fontsize = 3. * flx.shape[0] with warnings.catch_warnings(): warnings.filterwarnings('ignore', message="(.*)invalid value(.*)") flx[np.isnan(flx) | (flx < vmin)] = vmin # Create the frame fig = pl.figure(figsize=flx.shape[::-1], dpi=dpi) + rendr = find_renderer(fig) ax = fig.add_subplot(111) ax.matshow(flx, aspect='auto', norm=LogNorm(vmin=vmin, vmax=vmax), @@ -154,6 +171,22 @@ def annotated_image(self, frame=0, dpi=None, vmin=1, vmax=5000, txt2.set_path_effects([path_effects.Stroke(linewidth=fontsize/6., foreground='black'), path_effects.Normal()]) + + # Get text size to make sure it doesn't overlap. + txt_bbox = txt.get_window_extent(rendr) + txt2_bbox = txt2.get_window_extent(rendr) + + # Then scale the figure to match. + old_size = fig.get_size_inches() + ratio = max((1 + margin) * ((txt_bbox.x1 - txt_bbox.x0) + + (txt2_bbox.x1 - txt2_bbox.x0)) / fig.dpi + 2, + old_size[0]) / old_size[0] + new_size = np.around(np.multiply(old_size, ratio)) + + # Size needs to be divisible by two for video. + new_size += np.mod(new_size, 2) + fig.set_size_inches(*new_size) + ax.set_xticks([]) ax.set_yticks([]) ax.axis('off') From 461d21091fb72551f5feafe33a8b30fb8891f502 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Tue, 28 Jul 2015 13:27:37 +1000 Subject: [PATCH 3/3] *: add Aleksa to contributors Signed-off-by: Aleksa Sarai --- LICENSE | 1 + k2flix/core.py | 1 + 2 files changed, 2 insertions(+) diff --git a/LICENSE b/LICENSE index 44a22a8..c0b8acc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2015 Geert Barentsen +Copyright (c) 2015 Aleksa Sarai Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/k2flix/core.py b/k2flix/core.py index 8f4b84c..4744240 100644 --- a/k2flix/core.py +++ b/k2flix/core.py @@ -3,6 +3,7 @@ """"Create movies or animated gifs from Kepler Target Pixel Files. Author: Geert Barentsen +Contributor: Aleksa Sarai """ from __future__ import (absolute_import, division, print_function, unicode_literals)