Text under PNG display using Tkinter Python - python

I am working on tkinter and what I wanted to do was to show a cancel sign (a PNG) over text that will show the price in background of PNG.
So far I was able to display the text over the image properly using the "compound" option of tkinter.label().
But I have tried different ways to get the PNG on top of text but i haven't succeeded so far.
Is what I am trying to do at all possible? If yes kindly guide, if no, then suggest me a way around. what is the closest I can make using the available features.
I even tried making a cancel sign using canvas.draw() but that also hides the text.
Kindly help me out
Regards

Your best solution to display a partially transparent PNG on top of a text in tkinter is probably to use a Canvas with the create_text() and create_image() methods.
Example code (Python 2.x):
import Tkinter as tk
from PIL import ImageTk
from io import BytesIO
from urllib import urlopen
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
canvas.create_text(150, 150, text="word word word word word word word word")
url = "https://www.dropbox.com/s/etqan5h62d14mv8/Pikachu-PNG-Photos.png?dl=1"
img = ImageTk.PhotoImage(file=BytesIO(urlopen(url).read()))
canvas.create_image(150, 150, image=img)
root.mainloop()
For Python 3.x, use the following in the import statements:
import tkinter as tk
from urllib.request import urlopen
You should get this:

Related

How to use google font in tkinter

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.

Tkinter image blank (without PIL)

I am making an animated presentation and I want to add an image to it, but I can't use PIL because it will be presented at school, where PIL isn't installed, so I used the method given by the website http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm, but it doesn't work - my slide is still blank. What should I do?
Here is my code (copied almost completely from the website - did I miss something out?):
photo = PhotoImage('Alveoli.png')
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
You can not open .png files without using an external image library. Since PIL isn't available I'd think other similar libraries aren't either.
EDIT: Note: PNG files are supported for tkinter v8.6+.
The only possibility that comes to mind is converting the image to another, compatible format like GIF or PGM
If you have the image in one of those formats you can simply add them. It worked just fine for me with this code:
from tkinter import *
root = Tk()
photo = PhotoImage(file="img.ppm")
img = Label(root, image=photo)
img.image = photo
img.place(x=0, y=0)
root.mainloop()

Tkinter : How to change position of text on image

I have developed some application on my laptop with python and tkinter. Then, I was stuck at some point. Question is : how can I change text position on image.
import tkinter as tk
from PIL import Image, ImageTk
path_to_pic = "....."
root = tk.Tk()
pic = Image.open(path_to_pic)
tkpic = ImageTk.PhotoImage(pic)
tk.Label(root, image = tkpic, text = ".....", compound = tk.CENTER).pack()
root.mainloop()
This shows that my text appears on the picture, only on the center. I would like to move my text little by little and find best position. Do you know any solution or similar way to achieve this ?
You can move text horizontally and vertically by adding spaces and '\n's respectively, to any side(s) of the text you wish:
text = "caption \n\n\n\n\n\n\n"
This will put "caption" at the top left of the text.

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]

Tkinter using a non-saved picture as an image

I'm trying to grab a screenshot every 30 seconds and display it on my GUI, heres what I've got so far.
Code:
from Tkinter import *
from PIL import ImageGrab
window = Tk()
box = (100,100,400,400)
MyImage = ImageGrab.grab(box)
MyPhotoImage = PhotoImage(file=MyImage) #I know this is where its going wrong, just not sure how to fix it
PictureLabel = Label(window, image=MyPhotoImage)
PictureLabel.pack()
window.mainloop()
Python doesnt like the fact I haven't saved the image, is there a possible way to do this without saving the image (not much point since its being renewed every 30 seconds)
Its also not saving every 30 seconds yet, is there a simple way to do this without the program hanging?
As I could just use a time.sleep(30) but the program would just freeze up for 30 seconds take a picture then freeze again.
Thanks :)
You should be able to use StringIO for this:
import cStringIO
fp = cStringIO.StringIO()
MyImage.save(fp,'GIF')
MyPhotoImage = PhotoImage(data=fp.getvalue())
EDITS
Looks like I should read the docs a little closer. The PhotoImage data must be encoded to base64
from Tkinter import *
from PIL import ImageGrab
import cStringIO, base64
window = Tk()
box = (100,100,500,500)
MyImage = ImageGrab.grab(box)
fp = cStringIO.StringIO()
MyImage.save(fp,'GIF')
MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue()))
PictureLabel = Label(image=MyPhotoImage)
PictureLabel.pack()
PictureLabel.image = MyPhotoImage
window.mainloop()
tk images accept a "data" option, which allows you to specify image data encoded in base64. Also, PIL gives you ways to copy and paste image data. It should be possible to copy the data from MyImage to MyPhotoImage. Have you tried that?

Categories