How to properly import an SVG path to manim - python

I am working on a logo on Inkscape and I would like to import it to manim. The file does import properly with all the paths of the SVG but a weird thing is happening.
My code for running the file is this:
class U_letter(Scene):
def construct(self):
letter = SVGMobject("u_letter")
self.add(letter)
letter.set_color(GREEN)
The SVG format of the letter I am trying to add is available here.
It currently has two layers and when importing it this way it displays fine on manim but my problem is that I have to set the fill of the 'inner path'(i.e the inside of the letter) to match the background of the scene and I would like to avoid this.
I tried creating a single path out of the letter by using the difference functionality on Inkscape and that's where my problems started because the final image appears distorted as shown here.
I am however looking for something like this as my final solution. I should also point out that I have been experiencing this behavior with other letters I have tried so far, D and A letters to be exact.
Your help would be greatly appreciated

As of current date (June 3, 2020), There is no "proper" way to import a SVG object, as you see, you can you use SVGMObject and it will work most of the time, but as manim parses the path itself it ignores many things from the SVG specification in it's implementation, so you would have to fix it yourself or wait until it is fixed.

Related

How can I use svgwrite.image.Image to set an image in the background of a canvas I have other elements on?

I have an svgwrite.Drawing into which I wish to insert a JPG image as the background image. Here's the code to what I have so far. It is using computer vision for a machine learning task. The essential part to help me begin understanding how I can insert an image, is on line 147 where I create a drawing using svgwrite. The problem I'm running into is that in the documentation, if you scroll down to where the factory methods are, it says that you can use Drawing.image and set an href for an image that you want to use. I did further down notice that in the factory methods it also has Drawing.saveas in the form svg_canvas.saveas, which does end up working for me so I know that I am using this Drawing method to create an svgwrite.image.Image object correctly, but it doesn't seem to be working. For reference, I insert the following code right after this line:
svg_canvas.image(href="/home/mendel/project/bg/bg.jpg", size = src_size)
I triple checked that my href was correctly written out and that my bg image was in fact in the folder. src_size is already defined before as well so I'm pretty sure it's all working. But I'm not sure why the above line isn't working for me at all to give me a background image for the canvas. Please let me know if I'm using it incorrectly I really appreciate it!
I already tried the above solutions using the Python API but it isn't working. I also tried looking into other stack posts, none have worked for me so far.

Alternate to pygame Text Rendering in python [duplicate]

We have a small web app that we want to convert into something native. Right now, it's got a lot of moving parts (the backend, the browser etc.) and we'd like to convert it into a single tight application. We decided to use PyGame to do this and it's been fine so far except for a font rendering issue.
The string I'd like to render is कोझिकोड. This, correctly rendered looks like .
The specific code points are \u0915 \u094b \u091d \u093f \u0915 \u094b and \u0921
Now, this looks fine in my editor and my browser but when I try to render it in PyGame, I get this . Basically, the vowel sign (\u093f ि) should have been on the left of the झ but it appears to its right (and to the left of the क) thereby messing it up completely. This doesn't happen in a browser or a text editor (with the same input string) so I'm guessing it a renderer problem in PyGame.
There is one crude fix which works only in this specific case which i s to put the ि (\u093f) before the झ (\u091d). In that case, it renders properly like so . This relies on me knowing something about the language and putting that logic into the code. I have to deal with multiple languages here so that's not really feasible.
I don't have much experience with unicode so I don't know how to approach this problem. Is there something I can do to fix this?
In case it matters, I'm using the freesans font which is there on Debian and which has the necessary glyphs to render this.
Update:
The code to actually render this is as follows
# -*- coding: utf-8 -*-
import time
import pygame
# Pygame setup and create root window
pygame.font.init()
screen = pygame.display.set_mode((320, 200))
empty = pygame.Surface((320, 200))
font_file = pygame.font.match_font("freesans") # Select and
font = pygame.font.Font(font_file, 30) # open the font
writing = font.render(u"कोिझकोड कोझिकोड", True, (0, 0, 0)) # Render text on a surface
screen.fill((255, 255, 255)) # Clear the background
screen.blit(writing, (10, 10)) # Blit the text surface on the background
pygame.display.flip() # Refresh the display
input() # Wait for input before quitting
This is what it looks like
The first word is rendered correctly but we've done it by inverting the vowel and the letter positions as I mentioned in the crude fix. The second is written properly but not rendered correctly.
Update 2:
In the absence of anything else, I've decided to try to render the string into an image using an external program and then blit this image onto the PyGame Surface. I tried imagemagick but it messes us in the same way as this. Gimp works fine and so I'm planning to use the batch mode to get my work done.
I think is a SDL_ttf problem (the underlying component which actually renders the text).
While my IDE correctly renders the string
The SDL_TTF program does not:
There is the code: https://gist.github.com/ilario-pierbattista/be6b967b05fa2f1eb322f35988a33ad0
I'm still looking for a solution
I had to finally resort to a really ugly but usable workaround for my own situation. I wrote a script-fu plugin which takes a filename and a piece of text as arguments. It then writes out the text and saves it a png file using gimp. My program then loads this up and blits the png directly onto the surface.

Python hide already printed text

I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:
opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.
clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)
I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!
EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.
EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to #zondo.
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.
curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.
I'd personally just use this.
import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama import init
colorama.init() # AND THEN: init()
So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...
A simple example:
numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI.
I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.

Create a multi-view Paraview screenshot from a Python script

I'm trying to write a Python script for Paraview that will create a .png or .pdf screenshot file with multiple views in it. The emphasis here being the MULTIPLE VIEWS part. To be clear, I have three different windows in my Paraview display: one showing the model viewed in the XZ plane, one in the XY plane and one in the YZ plane. I'm trying to use a python script to create a single file showing all three of these views. This can be done manually by clicking File->Save Screenshot and then unchecking the Save only selected view button. I need to do this several hundred times, so clearly a script is the way to go.
I've tried using the "Start trace" option to see how this operation works, but the code it produces seems incomplete:
try: paraview.simple
except: from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()
RenderView1 = GetRenderView()
RenderView2 = GetRenderViews()[1]
RenderView3 = GetRenderViews()[2]
WriteImage(r'E:\TestFolder\TestFile_00.png', view=RenderView1)
WriteImage(r'E:\TestFolder\TestFile_01.png', view=RenderView2)
WriteImage(r'E:\TestFolder\TestFile_02.png', view=RenderView3)
Render()
When I run something similar to this it just produces three separate .png files, one for each view. Maybe these are meant to be temporary files that Paraview combines to make the finished product, but I have no idea how to combine them.
Does anyone have any experience with this problem? I've scoured the internet and the Paraview documentation, but the only examples I can find have a single view only. Any help would be much appreciated.
I'm using Paraview 3.12.0 32-bit on Windows XP
Starting with ParaView 4.2, this will directly supported as follows:
# Get the layout/tab for the active view.
aLayout = GetLayout()
SaveScreenshot("AllViewsImage.png", layout=aLayout)
You can also use Tools|Start Trace and try to save the screenshot out to get the commands to use.
I have the same problem. The workaround is to use convert command. Here is the example script:
for i in range(len(GetRenderViews())) :
RenderView = GetRenderViews()[i]
WriteImage("tmp_"+str(i)+".png",view=RenderView)
commands.getoutput("convert -border 1x1 -append tmp_*.png tmp.png")
I hope this will help.

Is there any good python library for generating and rendering text in image format?

I'm developing a CMS like application where the user should be able to create the menu the way he wants. Ideally, each menu object wouldn't be a text with a background but rather an image of the text. I envision this in either of these 2 ways:
a) By rendering a font in a/several image file/s with every letter and a script that given a word would give me either the sequence of images to string together or a single image file with the combination of all letters. I understand this could be done manually, but I feel there's probably a library to help with this.
b) With some kind of imaging library that would be able to render text with several blending effects such as gradient color, shadows, glow, etc. While I believe this one's a bit harder, maybe there's something that suits this need.
Any tips on any library that does this or anything similar?
Thanks in advance!
Bruno
We are using Imagemagick.
http://www.imagemagick.org/Usage/text/#attributes
This will render a simple button with text:
convert -background white -fill dodgerblue -font Candice -strokewidth 2 -stroke blue -undercolor lightblue -size 165x70 -gravity center label:Anthony label_color.gif
Wrapping this into a Python module is straight forward.
Although nowadays I'd settle to go with web fonts and CSS,a couple of years ago I faced this problem, and put together a small project that would generate text-within a templated image on the file, according to passed URL parameters.
The project is still publicized here: https://bitbucket.org/jsbueno/dynabutton -- it is made to work as a CGI script, but could be easily adapted to work with a more eficient server (I'd recomend some security tunning as well, if you are putting it online). You can also use it to generate all your images with a server side script, and just put the resulting image files online.
(it does use PIL underneath)
Ah yes, it can do shadow, and glow with proper parameters, can use any server-installed font, and will use an image template for providing the background, so you can apply any effect manually. (the included temlates, though, are quite amateurish)
Check out pycairo, bindings for the cairo rendering package. It can render text as well as graphics.
Well, with modern CSS techniques, the issue of nonmatching client-side fonts is less of a problem these days. Still there's demand for text-to-image tools.
PIL is often given as the answer to this question, but personally, I would give a good, hard look at pythonmagick as well. Pick the one that works best for you.
Actually the pygtk also has a pango renderer, as well.

Categories