How to use google font in tkinter - python

Is There any way to use google custom fonts in Tkinter Application.
I have google Font zip file from google font and I want to use that in my tkinter application.

I've came to the same problem, unfortunately it doesn't seem to be possible to use custom fonts in traditional way anyways.
But I came to a workaround by making a image with that text onto it.
def create_text_image(self, text, color_rgba_code_tuple):
fnt = Enums.ImageFont.truetype(Enums.FONT_DEFAULT, Enums.FONT_SIZE)
text_width, text_height = fnt.getsize(text)
img = Enums.Image.new('RGBA', (text_width,text_height))
d = Enums.ImageDraw.Draw(img)
d.text((0,0), text, font=fnt,fill=color_rgba_code_tuple)
img.save(Enums.FONT_TEMPORARY_IMAGE_PATH)
return Enums.PhotoImage(file=Enums.FONT_TEMPORARY_IMAGE_PATH)
Imports
from PIL import Image, ImageDraw, ImageFont, ImageTk
Explanation
You need to pass in your text and rgba tuple into the function; so, something like (255,255,255,255) for white text.
And then simply add that image that the function returns to a list, and use it in wherever place you want (like Canvas for example).
Hope this is clear.

Related

How can I mirror/flip a string and/or integer on the y-Axis (vertically) in Python?

I'm writing a GUI in Python (Tkinter) which will be used as a Head Up Display in a car. Unfortunately I didn't find any solution to mirror the whole displayed Screen so I try to mirror the words and numbers. When I tried to look it up everybody just takes 'hallo' and gets 'ollah'. But I try to even display the letters like you would get them when you mirror them vertically.
Are you using PIL Images? You can transpose these:
import Tkinter as tk
from PIL import Image, ImageTk
pil_image = Image.open(...)
flipped_pil_image = pil_image.transpose(Image.FLIP_LEFT_RIGHT)

Python, PIL; Text to Image and fonts

I have an issue with writing text to an image under Python and PIL -
I'm able to write text to a png file, though not bold text. Could anyone provide an example of how to achieve this?
I thought the easiest solution may be was use a bold-variant of a text, but I'm unable to see anything in the Windows/font folder that supplies this - does this mean font types have a 'bold attribute' that is T/F?:
Code I'm using:
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
# font = ImageFont.truetype("Arial-Bold.ttf",14)
font = ImageFont.truetype("Arial.ttf",14)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
A simple way to do it:
font = ImageFont.load_default().font
Also you can do a google search for 'verdana.ttf' and download it put it in the same directory as the python file:
Then add it like this:
font = ImageFont.truetype("Verdana.ttf",14)
You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family:
Then right-click and choose properties to find the file name:

View a document TKinter

I am new to TKinter and cant seem to find any examples on how to view a document in a window. What I am trying to accomplish is that when selecting a PDF or TIF it will open the file and show the first page in a window using TKinter. Is this possible?
A long time has passed since this question was posted but, for those still looking for a solution, here's one I've found :
https://github.com/rk700/PyMuPDF/wiki/Demo:-GUI-script-to-display-a-PDF-using-wxPython-or-Tkinter
Basically, it uses PyMuPDF, a python binding for MuPDF. MuPDF is a lightweight document viewer capable of displaying a few file formats, such as pdf and epub.
I quote the code used for TKinter:
This demo can easily be adopted to Tkinter. You need the imports
from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk
and do the following to display each PDF page image:
#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1) # create pixmap for a page
#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA",
[pix.width, pix.height],
str(pix.samples))
self.photo = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20,
height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)
No, it is not possible to show a TIF or PDF in a Tkinter window. Your main options are to show plain text, and to show images (.gif with plain Tkinter, other formats if you include PIL - the python imaging library).
Nothing is impossible my friend!
Try using snippets from this:
http://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html
to convert the pdf to an image.
I would then display the image on a label using PIL and Tkinter.
And I think Tif files should display on a label without problems IIRC.
The following code uses the old library pypdfocr (I had to modify it to work with Python 3) and Ghostscript (Ghostscript needs to be installed outside of Python and so isn't ideal for software to be distributed) to convert each of the PDF's pages into an image that can then be opened by PIL. In the sense that it uses very old libraries and packages, DrD's answer is probably more appropriate.
import glob
import PIL.Image
import PIL.ImageTk
import pypdfocr.pypdfocr_gs as pdfImg
files = glob.glob(pdfImg.PyGs({}).make_img_from_pdf(file_name)[1])
pages = [PIL.ImageTk.PhotoImage(PIL.Image.open(file)) for file in files]

Compositing two images with python wand

I need to use python wand (image-magick bindings for python) to create a composite image, but I'm having some trouble figuring out how to do anything other than simply copy pasting the foreground image into the background image. What I want is, given I have two images like:
and
both jpegs, I want to remove the white background of the cat and then paste it on the room. Answers for other python image modules, like PIL, are also fine, I just need something to automatize the composition process. Thanks in advance.
You can achieve this using Image.composite() method:
import urllib2
from wand.image import Image
from wand.display import display
fg_url = 'http://i.stack.imgur.com/Mz9y0.jpg'
bg_url = 'http://i.stack.imgur.com/TAcBA.jpg'
bg = urllib2.urlopen(bg_url)
with Image(file=bg) as bg_img:
fg = urllib2.urlopen(fg_url)
with Image(file=fg) as fg_img:
bg_img.composite(fg_img, left=100, top=100)
fg.close()
display(bg_img)
bg.close()
For those that stumble across this in the future, what you probably want to do is change the 'white' color in the cat image to transparent before doing the composition. This should be achievable using the 'transparent_color()' method of the Image. Something like 'fg_img.transparent_color(wand.color.Color('#FFF')), probably also with a fuzz parameter.
See:
http://www.imagemagick.org/Usage/compose/
http://docs.wand-py.org/en/latest/wand/image.html

How can I find out why PIL isn't drawing the font correctly?

Here's the code I'm using:
from PIL import Image
import ImageFont, ImageDraw
import sys
import pdb
img = Image.new("RGBA",(300,300))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(sys.argv[1],30)
draw.text((0,100),"world",font=font,fill="red")
del draw
img.save(sys.argv[2],"PNG")
and here's the image that results:
img http://www.freeimagehosting.net/image.php?976a0d3eaa.png ( for some reason, I can't make it show on SO, so the link is http://www.freeimagehosting.net/image.php?976a0d3eaa.png )
The thing is, I don't understand why it isn't drawing the font correctly? I should be able to read the word "world" off of it. It's like the picture's been cut in half or something. Does anyone have any clue?
EDIT: after balpha's comment, I decided to try another font. I'm only interested in ttf fonts, so I tried with another one, and it worked. This is kind of strange. The original font I tried to run this with is Beautiful ES. I'm curious if you guys can reproduce the same image on your computers, and if you happen to know the reason for why that is.
PIL uses the freetype2 library, so most possibly it is an issue with the font file; for example, it could have bad metrics defined (e.g see the OS/2 related ones opening the font with FontForge).

Categories