Here is some Python code that allows you to read in SVG files and approximate their paths using a Fourier series.

Overview

Visualization of the Fourier series

Here is some Python code that allows you to read in SVG files and approximate their paths using a Fourier series. The Fourier series can be animated and visualized, the function can be output as a two dimensional vector for Desmos and there is a method to output the coefficients as LaTeX code.

Some example videos of the animations can be found under example_animations.

How to use the program

You will need the packages numpy, matplotlib.pyplot, matplotlib.animation, svgpathtools and scipy.optimize. The important settings can be done in the main function (file FourierMain.py) using the different variables (shortly explained in the code). If you want to read your own image, change the path for the SVG handler, for example handler = SVG_Handler("images/img13.svg"). Here images/img13.svg is the path relative to the FourierMain.py file.

How to create a usable SVG file

I used Inkscape to draw the images. I tested the program with the freehand pen (the result of which can be seen here, for example) and the Bézier tool. Since the Fourier series at discontinuity points is only (mostly) point convergent and no longer uniformly convergent, one should try to start the new path as close as possible to the end of the old path in the case of several lines. For the same reason, the start and end points of the complete image should be close together.

What do the internal equations look like?

It is mainly a set of complex polynomials (representations of mostly Bézier curves). These complex polynomials are then parameterized from to , depending on the setup, to represent a "partial curve". If we concatenates all partial curves together, we have a large parameterization, which can be normalized by means of the method _get_parameter_func() in the file svg_handler.py again also to a parameterization for .

Finding the Fourier coefficients

Once one has the parameterization of the function (which corresponds to the paths of "the image") , one can integrate over the complete function . Calculating this integral for coefficients (), the Fourier series is , which approximates the path of the SVG file. Based on the representation of the curves in the svgpathtools library, the image is now described by the two dimensional path for .

Animation of the "Fourier vector"

Sorting all coefficients by their absolute value and then appending the vectors for each of the "partial sums " with the newly sorted summands, we have a nice looking path the Fourier series takes to approximate a point. If you do this for every single data point of the graph, all plotted one after the other result in the animation.

You might also like...
csv2ir is a script to convert ir .csv files to .ir files for the flipper.

csv2ir csv2ir is a script to convert ir .csv files to .ir files for the flipper. For a repo of .ir files, please see https://github.com/logickworkshop

dotsend is a web application which helps you to upload your large files and share file via link

dotsend is a web application which helps you to upload your large files and share file via link

This program can help you to move and rename many files at once
This program can help you to move and rename many files at once

This program can help you to rename and save many files in a folder in seconds, but don't give the same name to files, it can delete both files.

This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that have that extension.
This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that have that extension.

FileBulk This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that h

The best way to convert files on your computer, be it .pdf to .png, .pdf to .docx, .png to .ico, or anything you can imagine.
The best way to convert files on your computer, be it .pdf to .png, .pdf to .docx, .png to .ico, or anything you can imagine.

The best way to convert files on your computer, be it .pdf to .png, .pdf to .docx, .png to .ico, or anything you can imagine.

Python code snippets for extracting PDB codes from .fasta files

Python_snippets_for_bioinformatics Python code snippets for extracting PDB codes from .fasta files If you have a single .fasta file for all protein se

An easy-to-use library for emulating code in minidump files.

dumpulator Note: This is a work-in-progress prototype, please treat it as such. An easy-to-use library for emulating code in minidump files. Example T

Python Fstab Generator is a small Python script to write and generate /etc/fstab files based on yaml file on Unix-like systems.

PyFstab Generator PyFstab Generator is a small Python script to write and generate /etc/fstab files based on yaml file on Unix-like systems. NOTE : Th

Uproot is a library for reading and writing ROOT files in pure Python and NumPy.
Uproot is a library for reading and writing ROOT files in pure Python and NumPy.

Uproot is a library for reading and writing ROOT files in pure Python and NumPy. Unlike the standard C++ ROOT implementation, Uproot is only an I/O li

Comments
  • Code comments in FourierMain.py are truncated

    Code comments in FourierMain.py are truncated

    in FourierMain.py, the comment for line 97 says "number of fourier coefficients, will calculate from k=-N up to k=N, the more " and it looks like there was supposed to be a further explanation what happens when you increaset he value.

    image

    (It's "quite obvious" what it does, but this is a documentation report.)

    opened by JoonasD6 1
  • Missing LICENSE

    Missing LICENSE

    I see you have no LICENSE file for this project. The default is copyright.

    I would suggest releasing the code under the GPL-3.0-or-later or AGPL-3.0-or-later license so that others are encouraged to contribute changes back to your project.

    opened by TechnologyClassroom 1
  • Animation saving crashes the program

    Animation saving crashes the program

    I tried many different parameters (only touching the values of 'save_video', 'plot_reverse', 'fourier_N', 'animation_time' and 'n_eval') with custom images and then I ran into a problem when I set save_video to 'True'. No video was saved (I checked the whole project directory and no text.mp4 was found with my contents), and when closing the animation window -- regardless of original svg (as I tried with the original unmodified parameters again) -- I get this in terminal:

    950 of 1000 pointers calculated.

    Calculation of data points from coefficients done.

    Animation object created.

    Begin saving the video.

    Traceback (most recent call last):

    File "\Visualization-of-the-Fourier-series\FourierMain.py", line 149, in main()

    File "\Visualization-of-the-Fourier-series\FourierMain.py", line 144, in main anim.save('test.mp4', writer=writer)

    File "C:\Python310\lib\site-packages\matplotlib\animation.py", line 1073, in save with mpl.rc_context({'savefig.bbox': None}), \

    File "C:\Python310\lib\contextlib.py", line 135, in enter return next(self.gen)

    File "C:\Python310\lib\site-packages\matplotlib\animation.py", line 234, in saving self.setup(fig, outfile, dpi, *args, **kwargs)

    File "C:\Python310\lib\site-packages\matplotlib\animation.py", line 320, in setup self._w, self._h = self._adjust_frame_size()

    File "C:\Python310\lib\site-packages\matplotlib\animation.py", line 309, in _adjust_frame_size self.fig.set_size_inches(w, h, forward=True)

    File "C:\Python310\lib\site-packages\matplotlib\figure.py", line 2669, in set_size_inches manager.resize(*(size * self.dpi).astype(int))

    File "C:\Python310\lib\site-packages\matplotlib\backends_backend_tk.py", line 461, in resize self.canvas._tkcanvas.configure(width=width, height=height)

    File "C:\Python310\lib\tkinter_init_.py", line 1675, in configure return self._configure('configure', cnf, kw)

    File "C:\Python310\lib\tkinter_init_.py", line 1665, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))

    _tkinter.TclError: invalid command name ".!canvas"

    So something goes wrong saving the video. I made sure latest ffmpeg is installedjust in case, noticing that is used for the video (although it was not in the readme instructions). Running fresh Python 3.10.1 on Windows 11 (both Python and ffmpeg isntalled via Chocolatey with default settings, then imported all packages mentioned in the readme with pip in order for the program to run).

    opened by JoonasD6 2
Owner
Alexander
Alexander
Some-tasks - Files for some of the tasks for the group sessions

Files for some of the tasks for the group sessions Here you can find some of the

INF142@UiB.no Computer Networks 0 Aug 25, 2022
pydicom - Read, modify and write DICOM files with python code

pydicom is a pure Python package for working with DICOM files. It lets you read, modify and write DICOM data in an easy "pythonic" way.

DICOM in Python 1.5k Jan 4, 2023
A Python library that provides basic functions to read / write Aseprite format files

A Python library that provides basic functions to read / write Aseprite format files

Joe Trewin 1 Jan 13, 2022
This python project contains a class FileProcessor which allows one to grab a file and get some meta data and header information from it

This python project contains a class FileProcessor which allows one to grab a file and get some meta data and header information from it. In the current state, it outputs a PrettyTable to txt file as well as the raw data from that table into a csv.

Joshua Wren 1 Nov 9, 2021
Dragon Age: Origins toolset to extract/build .erf files, patch language-specific .dlg files, and view the contents of files in the ERF or GFF format

DAOTools This is a set of tools for Dragon Age: Origins modding. It can patch the text lines of .dlg files, extract and build an .erf file, and view t

null 8 Dec 6, 2022
Maltego transforms to pivot between PE files based on their VirusTotal codeblocks

VirusTotal Codeblocks Maltego Transforms Introduction These Maltego transforms allow you to pivot between different PE files based on codeblocks they

Ariel Jungheit 18 Feb 3, 2022
A JupyterLab extension that allows opening files and directories with external desktop applications.

A JupyterLab extension that allows opening files and directories with external desktop applications.

martinRenou 0 Oct 14, 2021
Python virtual filesystem for SQLite to read from and write to S3

Python virtual filesystem for SQLite to read from and write to S3

Department for International Trade 70 Jan 4, 2023
Python package to read and display segregated file names present in a directory based on type of the file

tpyfilestructure Python package to read and display segregated file names present in a directory based on type of the file. Installation You can insta

Tharun Kumar T 2 Nov 28, 2021
Python function to stream unzip all the files in a ZIP archive: without loading the entire ZIP file or any of its files into memory at once

Python function to stream unzip all the files in a ZIP archive: without loading the entire ZIP file or any of its files into memory at once

Department for International Trade 206 Jan 2, 2023