How to convert text to svg path in Python - python

I'm looking for a way to convert the text in a specific font face (.ttf or .otf) into an SVG path. So that I can continue working with the path in vector graphics as drawSvg (or a similar Library)

I don't know if you want it inside a python script only, but an alternative would be use cnc-text-tool or even text tool of Inkscape and then save it as plain SVG.

Related

Is there a way to paste a local image on abbreviation with AutoKey in Python?

AutoKey allows abbreviations to expand into text, and even emojis. However, I was wondering if there was a way for me to paste an image on a keyboard abbreviation.
There isn't currently. The underlying code just saves abbreviation expansions as a string.
However, if you wanted to do this, you could create a custom python script that loads the image into the clipboard then sends a paste signal. Check the Autokey docs for how to create and run custom scripts.
Of course, this would only work if the place you paste the image supports image input, e.g., OpenOffice.

How can I send an image to the clipboard in python?

I'm trying to use some images I take using pillow and that I organize in a list.
I would like to send those images to the clipboard, so I can use 'ctrl v' to manage those images in several word documents.
Right now, what I do is sending those images to an open word file using win32, so there's no useful code to show... but the images are in lists like:
[<PIL.Image.Image image mode=RGB size=281x164 at 0x2A46AAAB7F0>]
I would appreciate any guidance.
Explore modules/libraries such as: tkinter, pyperclip, clipboard, xerox.
These questions may be of use to dig through:
How do I copy a string to the clipboard on Windows using Python
Python Script to Copy Text to Clipboard
How to Add/Get Image
Data in the OS Clipboard using Python

Is it possible to save in a file an animation created with Tkinter?

I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).
In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.
Is there a simple way to save the animation that I see in my GUI in a file?
There is no simple way.
The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).
The question How can I convert canvas content to an image? has answers related to saving the canvas as an image
You might be able to take the best answers from those two questions and combine them into a single program.
I've accomplished this before, but not in a particularly pretty way.
Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif
This won't require any external dependencies or new packages except having imagemagick already installed on your machine
Save the image
I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can
Concurrently draw in PIL and save the PIL image https://www.daniweb.com/software-development/python/code/216929/saving-a-tkinter-canvas-drawing-python
Take a screenshot at every step, maybe using imagegrab http://effbot.org/imagingbook/imagegrab.htm
Converting the images to to an animation
Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.
The command
convert ../location/*.ps -quality 100 ../location/animation.gif
should do the trick.
Quirks:
There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name
name9.ps
Is alphabetically greater than
name10.ps
From imagemagick's point of view.
If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either
There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".
Click the link -->LICEcap : https://github.com/lepht/licecap
They say that it's free software for Mac (OS X) and Windows
You could look at Panda3D, but it could be a little over killed for what you need.
I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.

Python Tkinter postscript wrong size export

Im using python turtle (Tkinter) to draw some lines which I need to export to a .jpg or .png file. To do so, I'm using python's turtle method to export my canvas to a postscript file:
pen.getcanvas().postscript(file="grafica.ps")
Where pen is just a fancy name for my turtle.
I get my .ps file, I convert it and... surprize! The image gets cut.
I tried some modifications like:
pen.getcanvas().postscript(file="grafica.ps", colormode='color', pagewidth=1600, pageheight=1200, width=1600, height=1200)
Since my turtle's window is 800x600 I thought that maybe twice as much space would be enough space to fit all the image but it still gets cut down...
I'm posting some output examples after the convertion, how my turtle's screen looks like when saving it, and how it should look exported.
Window while saving the image:
(Yes, there are sliders for the canvas)
How should it look:
And this is what I get:
I'm wondering how should I call postscript(), any idea?
I don't want to code this again on WxPython or other library :(
thanks!
This is probably a problem with ImageMagick interacting with the bounding box of the EPS file. My typical workflow for .eps files on Windows may be slightly convoluted, but it works. Similar thing should work for Linux. Install GhostScript (you'll have to make sure the GhostScript executables are on your path), then use the ps2pdf utility from the command line with the -dEPSCrop option:
ps2pdf -dEPSCrop input.eps output.pdf.
Then, use ImageMagick to convert the PDF to anything else, e.g. PNG
convert output.pdf output.png
You can control the PNG resolution etc. through ImageMagick. Like I said, convoluted, but it works.

LaTeX type rendering with pygame

I am trying to read text with LaTeX formatting and render it using pygame. I am a novice to pygame and any help will be appreciated.
I looked into Glyph package. The problem is that I would have to rewrite all my .txt files in Glyph formatting for them to be useful. If I try and define macros I would essentially have to build a LaTeX translator. Both these options don't seem very practical considering the number of text files I have.
You could render LaTeX via matplotlib and use these figures in PyGame as sprites.
http://matplotlib.sourceforge.net/users/usetex.html
Check out pylasem (yes, it says MathML rendering library, but it also supports a subset of Latex) or lasem itself via gobject-introspection.

Categories