I'm unable to find any way to load .bmp file into Tkinter() so that I can use it in a canvas widget!Plz help me!
from Tkinter import *
from PIL import Image
import ImageTk
import tkFileDialog
import tkMessageBox
root=Tk()
class lapp:
def __init__(self,master):
w=Canvas(root,width=300,height=300)
w.pack()
p=Image.open("001.bmp")
tkimage=ImageTk.PhotoImage(p)
w.creat_image(0,0,image=tkimage)
App=lapp(root)
root.mainloop()
Its not showing any image on the canvas, its just blank!
Btw I'm using win7 with python 2.7
This works for me.
The image doesn't show when I use the Tk PhotoImage class. But it works ok when using PIL.
My image size is 50*250, so I've put coordinates that center it (25, 125)
from Tkinter import *
from PIL import Image, ImageTk
root=Tk()
root.title("My Image")
w = Canvas(root, width=50, height=250)
image = Image.open("blog0.bmp")
w.create_image((25, 125), image=ImageTk.PhotoImage(image))
w.pack()
root.mainloop()
I hope it helps
Related
Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
\#from PIL import Image
import PIL.Image
root=Tk()
root.geometry('300x400')
Button(root,text='open second window',command=open).pack()
def open():
global myimage , img
img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
mainloop()
I want the image to come on top level window but it is showing attribute error
I do not used PIL. Re-arranged your code in order to make it working. Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image
root=Tk()
root.geometry('300x400')
def open():
global myimage , img
img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
Button(root,text='open second window',command=open).pack()
root.mainloop()
Output:
I've been using tkinter for a while now and currently I am working on an app in which I need to display an image at the home screen. The problem is that though the code is fine, the image is not being displayed. This is the image:
This the code I am using. Please tell the problem:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
bg_img = Label(image=ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png")), compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
I have tried using the grid geometry manager but it is not working. Please help
When you add a PhotoImage to a Tkinter widget, you have to keep a separate reference to the image object. Otherwise, the image will not show up.
You need to define image object separately and then load it into Label:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.resizable(False, False)
image = ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png"))
panel = Label(root, image=image)
panel.pack(expand=1, fill=BOTH)
root.mainloop()
Output:
try this code it might work
from PIL import Image
from PIL.ImageTk import PhotoImage
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
bg_img = Label(image=ImageTk.PhotoImage(Image.open("Jitn6.png")), compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
you should do it like this
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
my_image = ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png"))
bg_img = Label(image=my_image, compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
you should first put the image in a variable then pass it in
Working in a EXE file with background and Icon embed, I read some ideas about to use img2py in order to create module.py that can be imported into the EXE with pyinstaller.
I successfully create the imagebg.py (BG2.png Background) and imageico.py (ico2.ico Icon) modules using img2py. But there is no example or way to set that images into the modules in the tkinter label as background and the icon into the code.
Please anybody can help me.
Main code where must be imported BG ad ICON modules and use them as background and Icon images for tkinter GUI
#Main code GUI tkinter
import imagebg #from here we must import PNG_File.png for Background
import imageico #from here we must import ICO_File.ico for Icon
import wx #from img2py installation
from tkinter import *
root=Tk()
#set windows size
root.resizable(width=False, height=False)
root.geometry("925x722")
#set title
root.title("SOFT1)")
#frame 1
f1=Frame(root, width=345,height=475,bg="light
grey",highlightbackground="black",highlightthickness=4)
f1.place(x=20,y=235)
#set a image as BG
Logo=PhotoImage(file="PNG_File.png")
lab6=Label(root, image=Logo)
lab6.place(x=0, y=0)
#set a image as ICON
root.iconbitmap("ICO_File.ico")
mainloop()
The module imagebg.py for backgroud generated with IMG2PY
BG2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAIAAABgrdCGAAAACXBIWXMAAA7EAAAOxAGVKw4b'
b'AAAgAElEQVR4nOzdd2Ab5d0H8Oe0hy1vecYjcZw4cfYimwQIEEbYhQJlFgqU3UFZLR2M9i2l'
b'tLRlFcouI5Qww15hZJEdO4kdOx7xtiVrj7v3DwdFlmXppu4kfT9/Wfbdc78YY339TMrpdBIA'
b'AAAAAVRyFwAAAABJD3kCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeA..... to much characters to be placed in this post
The module imageico.py for icon generated with IMG2PY
from wx.lib.embeddedimage import PyEmbeddedImage
ico2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAYAAADvz0fRAAAABHNCSVQICAgIfAhkiAAAIABJ'
b'REFUeJzs3WeAVNXdBvDnTi/b+7L0svSmIIKd2Gus0ZhiSeKreZOYmGpL15i8SdTEFKMpGkss'
b'GEuMqNFYsCAgHRZYWFiWsn12p7f7fkBwy8zszC1z7537/L7ozs6c82dh4dkz/3OOEAgERBAR'
b'ERERmZhF6wKIiIiIiLTGUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESm'
b'x1BMRERERKbHUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESmx1BMRERE'
b'RKbHUExEREREps.... to much characters to be placed in this post
Please any idea of how to use the modules as images for background and icon in the main code GUI Tkinter
You need to convert the wx.Image to PIL compatible image if you want to use it in tkinter application.
Below is an example based on your posted code:
import tkinter as tk
from PIL import Image, ImageTk
import imagebg
import imageico
# function to convert a wx.Image to PIL.ImageTk.PhotoImage
def wx2pil(wx_image):
image = wx_image.Image
w, h = image.GetWidth(), image.GetHeight()
data = image.GetData()
img = Image.frombytes('RGB', (w, h), bytes(data))
return ImageTk.PhotoImage(img)
root = tk.Tk()
root.resizable(False, False)
root.geometry('925x722')
root.title('SOFT1')
# use iconphoto() instead of iconbitmap()
root.iconphoto(False, wx2pil(imageico.ico2))
#set a image as BG
logo = wx2pil(imagebg.BG2)
tk.Label(root, image=logo).place(x=0, y=0)
#frame 1
f1 = tk.Frame(root, width=345, height=475, bg='lightgray', highlightbackground='black', highlightthickness=4)
f1.place(x=20, y=235)
root.mainloop()
Note that I use wxPython v4.1.0.
As it depends on wxPython, why don't you use wxPython for the GUI directly instead of tkinter?
I'm trying to zoom in on an image and display it with the following code
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1.zoom(2)
window.mainloop()
But python says AttributeError: 'PhotoImage' object has no attribute 'zoom'. There is a comment on a related post here:
Image resize under PhotoImage
which says "PIL's PhotoImage does not implement zoom from Tkinter's PhotoImage (as well as some other methods)".
I think this means that I need to import something else into my code, but I'm not sure what. Any help would be great!
img1 has no method zoom, however img1._PhotoImage__photo does. So just change your code to:
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1._PhotoImage__photo.zoom(2)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
By the way if you want to reduce the image you can use the method subsample img1 = img1._PhotoImage__photo.subsample(2) reduces the picture by half.
If you have a PIL Image, then you can use resize like following example:
import tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('C:\\Two.jpg')
image = image.resize((200, 200), Image.ANTIALIAS)
img1 = ImageTk.PhotoImage(image=image)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
Note I just import Image and ImageTk, see no need to rename to PIL_image and PIL_imagetk, which to me is only confusing
I am trying to load an image into a canvas in python. However I am getting the error: TclError: couldn't recognize data in image file "C:\testimage\spongesea.jpg"
import Tkinter
from Tkinter import *
import time
import inspect
import os
from PIL import Image, ImageTk
class game_one:
def __init__(self):
global root
global canvas_one
root = Tk()
root.title(" Thanks Josh Dark
canvas_one = Tkinter.Canvas(root, bg="BLACK")
canvas_one.pack(expand= YES, fill= BOTH)
canvas_one.focus_set() #allows keyboard events
p = PhotoImage(file="C:\testimage\spongesea.jpg")
canvas_one.create_image(0, 0, image = p, anchor=NW)
root.grab_set()#I forget what this does. Don't change it.
root.lift()#This makes root appear in front of the other applications
ObjectExample = game_one()# starts the animation
I can open the image manually from the file, so it is not corrupted, and it is calling the correct place. Any ideas? Thanks
PhotoImage works only with GIF and PGM/PPM.
You have to use Image, ImageTk to work with other formats
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
BTW: read PhotoImage and see "Garbage Collection problem" in note.