Printing colored texts in Powershell, using python - python-3.x

I used termcolor to print a colored text which ended up printing a weird code in Powershell, thus I used colorama.init() but it didn't change anything. I copied and texted some other code from Stackoverflow as I failed to import colorama at the beginning.
import sys
from termcolor import colored
if sys.platform == 'win32':
try:
import colorama
except ImportError:
pass
else:
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
This codes prints out a colored message in Powershell (it doesn't blink though). However I don't know why this shorter code doesn't work. (It prints "[5m[46m[35mHI THERE![0m")
from termcolor import colored
import colorama
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
I do import termcolor and colorama; and use init, colored methods in both these codes but I don't know why it only works with the first code. Do you know what might be the reason behind this? Thanks in advance.

Related

Matplotlib & LaTeX

I use MikTeX and try to obtain LaTeX fonts in my matplotlib plots.
However, using the demo code, Jyputer Notebook says that there is no latex,
Failed to process string with tex because latex could not be found
I try to add into PATH the path to latex.exe, dvipng.exe and ghostscript. Unfortunately, it still does not work. What I do wrong?
If I evaluate the following
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.sin(np.arange(0, 10, 0.1)),label=r"$\mathcal{M}=2$")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
it returns me the next picture,
So, I see that \mathcal{} command works perfectly, whereas the fonts are not "latex".
You need to add plt.rc('text', usetex=True).

How to import "termcolor" function properly?

I imported termcolor to my code and it didn't give any colors when i ran the code. The rest worked fine.
I have imported termcolor in powershell and it says "Requirement already satisfied".
from termcolor import colored
color = input("What colour would you like? ")
colored_ascii = colored(hello, color=color)
print(colored_ascii)
I expected the output to be colored, but it's always white(on blue background), which is the default in powershell.

How to change Python background to a certain colour with colorama?

I am using colorama to change the colour of my text and it works fines, but now I want to change the colour of the whole background instead of just my text.
I am using Windows:
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Back.GREEN)
print(Fore.RED)
But, that code only makes the text coloured. Is there a way to do that in python? I want it like CMD where you can have the background of it a colour. I cannot use the OS module as I do not have admin rights, but I'm open to using any other module.
How do I solve this problem?
After a while in playing with it i figured it out. Just forgot about this post. Here is what I did.
import colorama
from colorama import Back as bg
colorama.init()
print(bg.RED)
print(colorama.ansi.clear_screen())
I think clearing the screen fixed the Issue
After playing with colorama on my Windows 10 box it seems it's only used to change the text but not the console/terminal background. I was however able to change the background using this standard library solution:
import ctypes
try:
ctypes.windll.msvcrt.system(b"color 4f")
print("Hello World!")
except ValueError:
pass
The terminal background will change to red with white text.

Can you print text in colors other than blue using a print command in python 3.2?

My code currently only prints text in blue but I would like some important messages to be printed using the print() command but in colors such as red.
I tried using something called colorama which I found in a different question's answer.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED+'Hello')
But it came up with this:
Traceback (most recent call last):
File "E:/Color test", line 1, in <module>
import colorama
ImportError: No module named colorama
Does anyone know how to do it or at least know if it is possible?
EDIT: I have found a similar question and tried all of the answers but none of them worked. I will really appreciate anybody's help.
You can do this using ANSI escape codes.
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
Reset: \u001b[0m
Suppose you want to print "Hello world" in blue, You will have to do:
print("\u001b[34m" + "Hello world" + "\u001b[0m")
Please note that I have added the reset code at the end to avoid coloring the rest of the text.
Additionally, if you are using a windows operating system you will need to clear the screen for the colors to appear, like this:
import os
os.system("cls")
Good luck!
There is a way - but that it is much more difficult than what I can just simply put in here and is a lot more advanced. is it possible to add colors to python output? Go to that weblink for more info.

getting the color of pixels on the screen in Python3.x

I want to get the color of pixels on the screen in Python3.x. (example x,y)
I work on windows. and using tkinter.
But, It can't use PIL in Python 3.x.
How can i do this.
Thank you.
You can use PIL on Python3.x; there is a binary installer for Windows. The code might not work as is both on Python2.x and Python3.x:
try: import Image # Python 2.x
except ImportError:
from PIL import Image # Python 3.x
You could capture an area of the screen using an analog of grab() method.

Resources