Importing a .GIF image into a Python Graphics Window - python

so I'm trying to import a test .GIF file into my graphics window for Python 3.x and I can't seem to get it to work. So far I have:
from graphics import *
from tkinter import *
def main():
win = GraphWin ("Angry Birds!", 500,500)
angry = PhotoImage(250,250,'angry.gif')
angry.draw(win)
win.mainloop()
win.getMouse()
win.close()
main()
Yes I am in a class trying to learn but we never went over this and my professor neven mentioned downloading external packages like I've seen mentioned on this website of PIL. What am I missing?

Related

How to fix Tkinter dead frezzing or crashing because of huge data?

I am trying to make a program to display one single image (.png extension) at once but giving a button to the user to change the picture.
What I have done is:
Reading the Image from my directory with help of Pillow module
Appended it to a list
With a button I increase or decrease the index of the list.
(Note I have to read 600 images approx.)
Here's the code:
import os
from tkinter import *
from PIL import ImageTk,Image
import threading,time
#Define the tkinter instance
x=0
win= Tk()
dir_path= os.path.dirname(os.path.realpath(__file__))
print(dir_path)
l1=[]
#Define the size of the tkinter frame
win.geometry("700x400")
def start():
threading.Thread(target=bg).start()
win.after(5000,threading.Thread(target=fg).start())
#Define the function to start the thread
def bg():
print("bg")
for i in range(1,604):
a=Image.open(f"{dir_path}\\{i}.png")
a=a.resize((500,700), Image.ANTIALIAS)
b=ImageTk.PhotoImage(a)
l1.append(b)
print(b)
print(len(l1))
def fg():
def add():
global x
x+=1
img2=l1[x]
d.configure(image=img2)
d.image = img2
d.update()
global d
d=Label(win,image=l1[x])
d.pack()
Button(win,text="add",command=add).place(x=0,y=0)
label= Label(win)
label.pack(pady=20)
#Create button
b1= Button(win,text= "Start", command=start)
b1.pack(pady=20)
win.mainloop()
But the problem is that the Tkinter gets dead freeze and laggy to an extent that the GUI is not operable.
So my question is,
How to fix Tkinter dead Frezzes and if there is any way to read the images as fast as possible?
The freezes of Tkinter depends on reading speed of the interpreter, because:
continuously reading and Showing the pictures are a huge job for Python.
reading images using opencv or any other image processing module wont help as reading part can become faster, but showing the image in tkinter is done using python only, so rapid changes in Label will cause tkinter to crash.
Solution:
Switch to a different compiler based language for example c++.
this solution is specifically for a image slideshow,
The solution is to use selenium with python. You can specify the image location in driver.get(image_path) and it will open the image for you, and if you want to change the image with a button, then just rename all your images with the numbers and make a button to add +1 to the index.

How to display an SVG image in Python

I was following this tutorial on how to write a chess program in Python.
It uses the python-chess engine. The functions from that engine apparently return SVG data, that could be used to display a chessboard.
Code from the tutorial:
import chess
import chess.svg
from IPython.display import SVG
board = chess.Board()
SVG(chess.svg.board(board=board,size=400))
but when I run that code, all I see is a line in the terminal and no image.
<IPython.core.display.SVG object>
The tutorial makes a passing reference to Jupyter Notebooks and how they can be used to display SVG images. I have no experience with Jupyter Notebooks and even though I installed the package from pip and I dabbled a little into how to use it, I couldn't make much progress with regards to my original chessboard problem. But what I do have, is, experience with Qt development using C++ and since Qt has Python bindings, I decided to use those bindings.
Here is what I wrote:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget(chess.svg.board(board=board, size=400));
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
A Qt window opens and shows nothing and in the terminal I see a lot of text - (apparently the SVG data is ending up in the console and not in the Qt window that is opening?).
I figured I have to install some SVG library under python so I installed drawSvg from pip. But it seems that library generates SVG images. And was of no use for me.
What is even more strange is, after seeing this SO question, I tried the following:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget('d:\projects\python_chess\Zeichen_123.svg');
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
And it showed an image - an SVG image! What is the difference then between my case and this case?
Question: So my question is, what I am doing wrong in the case of the chessboard SVG data? Is the SVG data generated by the python-chess library not compatible with QtSvg?
I think you are getting confused by the scripting nature of Python. You say, you have experience with Qt development under C++. Wouldn't you create a main window widget there first and add to it your SVG widget within which you would call or load SVG data?
I would rewrite your code something like this.
import chess
import chess.svg
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 1100, 1100)
self.widgetSvg = QSvgWidget(parent=self)
self.widgetSvg.setGeometry(10, 10, 1080, 1080)
self.chessboard = chess.Board()
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
EDIT
It would be even better if you would add a paint function to the MainWindow class. Because for sure in future, you would want to repaint your board image many times, whenever you would move a piece. So I would do something like this.
def paintEvent(self, event):
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
this is how i display SVG image with python, no need third party to do that.
First i save SVG image on disk
second i just call it like os.Startfile("exemple.svg")
Exemple:
boardsvg = svg.board(board(fen), size=600, coordinates=True)
with open('temp.svg', 'w') as outputfile:
outputfile.write(boardsvg)
time.sleep(0.1)
os.startfile('temp.svg')
that's it !

How can I run a python code opening a Tkinter window and not a shell's one?

This is my first question.
I have a python program that recognize voice and reply with voice.
I wish to add a little GUI for my program (it should have only an image on background and a button to quit the program)
I would like that when I launch my code from terminal, it opened a Tkinter window and at the same time the python program start.
I’m working on Mac Os.
I use speech_recognition package to recognize voice and I use NSS speaker to let my computer speak.
This is a example of my code:
import speech_recognition as sr
from AppKit import NSSpeechSynthesizer
#VARIABLES
L = sr.Recognizer() #LISTENING
nssp = NSSpeechSynthesizer #SPEAKING
S = nssp.alloc().init()
while True:
audio = L.listen(source)
s = L.recognize_google(audio, language="en-US")
if s == "hi":
S.startSpeakingString_("Hello!!!")
Where do I have to write the Tkinter instructions to make sure that when I run my code it opens only a Tkinter window (while my program goes on) and not a shell's one?
You'll find it difficult to introduce your GUI as your code has already been written, note that everything in Tkinter has to be stored in some sort of Label or Widget and so you can't just print what you already have onto the Tkinter screen.
Here is some code to create a basic Tkinter window. Try searching online and playing around with how to present your variables within said window
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background = "#66ffdd") #here you can use any hex color code or just leave it blank and configure as default
root.title("Voice Program") #use the name of your program (this is the window header/title)
root.geometry("800x500") #these are your window dimensions
welcome = tkinter.Message(root, text = "Welcome to my program")
button = tkinter.Button(root, text="This button", command=print("hello")) #here insert a function for the button to perform i.e quit
welcome.pack()
button.pack() #packing presents your variables to the window - you can also use other geometry managers like grid
This site is really useful for showing you what widgets are available and what you can do with them - try searching any issues or posting a more specific question in the future if you struggle.
http://effbot.org/tkinterbook/button.htm

ImageTk.PhotoImage Crash

I have been trying to resize images using PIL then display them using Tkinter, but the program has been crashing and I've isolated the problem to the second line below:
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
And this is my imports:
from Tkinter import *
from PIL import Image, ImageTk
I've read around that Tk must be initialized and I do this in the program before it reaches those lines in the program. So I don't know what it is.
I am running OSX and python 2.7 interpreter on eclipse (using PyDev).
UPDATE:
Error message on eclipse says:
STACK: Stack after current is in use
I've seen that error before using tkinter. I think it had something to do with an older version of tkinter. I updated my python version and tkinter version and it went away. Does this error happen when you run your code on a different OS/Computer/Platform/Version of Python? What version of tkinter are you using? Some google searching revealed these two pages which describe the same bug while using tkinter...
http://osdir.com/ml/python.leo.general/2008-03/msg00060.html
http://fornax.phys.unm.edu/lwa/trac/ticket/3
I can't see all your code, but I'm betting that there is not necessarily anything wrong with your code. The following code worked for me...
from Tkinter import *
from PIL import Image, ImageTk
# resize image with PIL
im = Image.open('path to gif')
resized_im = im.resize((400,400,),Image.ANTIALIAS)
# display image in tkinter window
window = Tk()
tk_im = ImageTk.PhotoImage(resized_im)
window.geometry('%dx%d' % (resized_im.size[0],resized_im.size[1]))
label_image = Label(window, image=tk_im)
label_image.place(x=0,y=0,width=resized_im.size[0],height=resized_im.size[1])
window.mainloop()
Using....
ubuntu 10.04 64 bit
python 2.6.5
python-imaging-tk 1.1.7
python-tk 2.6.5 (which uses version 8.5.0 of tkinter)
python imaging library (PIL) 1.1.7
eclipse 3.7.1
pydev 2.5.0.2012050419
Good luck!
I've been using both Tk, PIL and resizing images for a current project and the following code works fine for me.
#Imports
from Tkinter import *
from PIL import Image, ImageTk
#Create Tk instance
root = Tk()
#Open image and resize
image = Image.open("path/to/image/file").resize((400,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
After that, I find it easiest to display the images as labels in tkinter like so.
image_label = Label(root, width = 400, height = 400, image = photo bd = 0)
(I like the bd = 0 as otherwise I get a thin white border around the image.)
Hope this has helped you. Good luck!
Ed
So this is an ancient question, but in case someone stumbles upon this (like I just did), the error message is from Tcl (tclExecute.c). I have no idea what's triggering it, but one thing worth trying is to create a Tk instance before calling PhotoImage:
root = Tk()
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)

Turtle module - Saving an image

I would like to figure out how to save a bitmap or vector graphics image after creating a drawing with python's turtle module. After a bit of googling I can't find an easy answer. I did find a module called canvas2svg, but I'm very new to python and I don't know how to install the module. Is there some built in way to save images of the turtle canvas? If not where do I put custom modules for python on an Ubuntu machine?
from tkinter import * # Python 3
#from Tkinter import * # Python 2
import turtle
turtle.forward(100)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="duck.eps")
This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module.
The canvas (tkinter) object has the postscript function; you can use it.
The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.
This will save you in encapsulated PostScript format, so you can use it in GIMP for sure but there are other viewers too. Or, you can Google how to make a .gif from this. You can use the free and open source Inkscape application to view .eps files as well, and then save them to vector or bitmap image files.
I wrote the svg-turtle package that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install it with pip install svg-turtle, and then call it like this:
from svg_turtle import SvgTurtle
def draw_spiral(t):
t.fillcolor('blue')
t.begin_fill()
for i in range(20):
d = 50 + i*i*1.5
t.pencolor(0, 0.05*i, 0)
t.width(i)
t.forward(d)
t.right(144)
t.end_fill()
def write_file(draw_func, filename, width, height):
t = SvgTurtle(width, height)
draw_func(t)
t.save_as(filename)
def main():
write_file(draw_spiral, 'example.svg', 500, 500)
print('Done.')
if __name__ == '__main__':
main()
The canvasvg package is another option. After you run some turtle code, it will convert all the items on the tkinter canvas into an SVG file. This requires tkinter support and a display, where svg-turtle doesn't.

Categories

Resources