def save_chart_image(self, chart_data_dict: dict, mpl_plt: plt) -> Path: """ Save image in db, and return path to file in temp storage. :param chart_data_dict: dict :param mpl_plt: matplotlib.pyplot :return: Path """ # Get image data: image = BytesIO() mpl_plt.savefig(image, format='png', dpi=300) # dpi - 120 comes to 1920*1080, 80 - 1280*720 image.seek(0) # Return pointer to start of binary stream. # Save image in db with self.session_scope() as session: chart = session.query( self.Chart).filter_by(id=chart_data_dict['chart_id']).one() chart.image = image.read() session.commit() image.seek(0) # Save file to temp and pass back Path temp_image_path = Path( DataFolder.generate_rel_path(DataFolder.TEMP_DIR.value), f"{chart_data_dict['chart_name']}.png") temp_image_path.write_bytes(image.read()) return temp_image_path
def save_chart_image(self, chart_data_dict: dict, mpl_plt: plt) -> Path: """ Save image in db, and return path to file in temp storage. :param chart_data_dict: dict :param mpl_plt: matplotlib.pyplot :return: Path """ # Get image data: image = BytesIO() mpl_plt.savefig(image, format='png', dpi=300) # dpi - 120 comes to 1920*1080, 80 - 1280*720 image.seek(0) # Return pointer to start of binary stream. # Save image in db with self._connection() as conn: cursor = conn.cursor() cursor.execute( """ UPDATE chart SET image=? WHERE id=?; """, (image.read(), chart_data_dict['chart_id'])) image.seek(0) conn.commit() conn.close() # Save file to temp and pass back Path temp_image_path = Path( DataFolder.generate_rel_path(DataFolder.TEMP_DIR.value), f"{chart_data_dict['chart_name']}.png") temp_image_path.write_bytes(image.read()) return temp_image_path
def capture_figure(metadata: ReportFigure, figure: pyplot): fig_rel_path = os.path.join('figures', f'{metadata.title}.png') fig_path = os.path.join(report_directory, fig_rel_path) figure.savefig(fig_path) metadata.img_path = fig_rel_path report.figures.append(metadata)
def matplot(self, plot: plt, win: str): """Draw matplot chart""" buffer = io.StringIO() plot.savefig(buffer, format='svg') buffer.seek(0) svg = buffer.read() buffer.close() return self.svg(svg=svg, win=win)
def __init__( self, chart: MatPlotLibPlot, width: Optional[Decimal] = None, height: Optional[Decimal] = None, ): byte_buffer = io.BytesIO() chart.savefig(byte_buffer, format="png") byte_buffer.seek(0) super(Chart, self).__init__(image=PILImage.open(byte_buffer), width=width, height=height)
def export_plt(plot_obj: plt, filename: str, filepath: str) -> str: """ Exports single frames from pyplot object. :param plot_obj: pyplot object to store frame from. :param filename: Desired file name of the *.png. :param filepath: Desired file path to store the frame. :return: Full storage path of the new *.png file (with extension) """ sub_folder = filepath # os.path.join(ICAP_DIR, filepath) if not os.path.exists( sub_folder): # Creates folder if folder does not exist os.makedirs(sub_folder) full_path = os.path.join(sub_folder, filename + '.png') plot_obj.savefig(full_path) return full_path
def save_chart_image(self, chart_data_dict: dict, mpl_plt: plt) -> Path: """ Save image, and return path to file in application storage. Save to app_data/classname/chart_data with same filename as chart_name and chart_data_file name. NB User copy stored elsewhere. :param chart_data_dict: dict :param mpl_plt: plt - matplotlib.pyplot object :return: Path """ class_id = chart_data_dict['class_id'] default_chart_name = chart_data_dict['chart_default_filename'] app_data_save_pathname = self.class_data_path.joinpath(class_id, 'chart_data', f"{default_chart_name}.png") Path.mkdir(app_data_save_pathname.parent, parents=True, exist_ok=True) # Save in app_data/class_data/class_id/chart_data with chart_default_filename mpl_plt.savefig(app_data_save_pathname, format='png', dpi=300) # dpi - 120 comes to 1920*1080, 80 - 1280*720 return app_data_save_pathname
def _save_to_png_image(plot: plt, filename): # write to disk isntead - this is used as alternative route for a different use case filedir = Path(__file__).parent relative_path = 'results' save_location = (filedir / relative_path / filename).resolve() plot.savefig(save_location, format='png')
def _save_to_bytes_image(plot: plt): bytes_image = io.BytesIO() plot.savefig(bytes_image, format='png') bytes_image.seek(0) return bytes_image
def save_plot(plot_to_save: plt, filename: str): plot_to_save.savefig(filename)