I've tried this but it doesn't help: Image resize under PhotoImage
I already have this:
import tkinter
root = tkinter.Tk()
root.attributes("-fullscreen", True)
photo = tkinter.PhotoImage(file="image.gif")
img1 = tkinter.Label(root, image = photo)
img1.pack()
root.mainloop()
This works but my image doesn't take up the full screen and I wanted to know how to resize it to full screen.
I realised it is not possible to do this and I will try to use PIL. Thanks to #BryanOakley for helping me with this question.
Related
The reason for this question is for informational purposes, also to provide the beginning reader with the correct path to follow if you intend to display a png image without a background, in a tkinter window. Try wm_attributes ("-transparentcolor", " "), but it is not an absolute and correct solution. Thank you very much for your patience and recommendation.
from tkinter import Tk,Frame,Label,PhotoImage
root = Tk()
image = PhotoImage(file="image.png")
frame = Frame(root)
label = Label(frame, image=image)
label.pack(side="bottom", fill="both")
frame.pack(side="bottom", fill="both")
root.wm_attributes ('-transparentcolor', 'black')
root.mainloop()
So I have 2 images, 1 image is supposed to be the background and the other 1 is just an image of a character.
I am able to put one image over the other but the image has white borders even though it's a PNG file.
This is how it looks like:
This is how I want it to look like:
Here are the two separte images:
https://imgur.com/a/SmE5lgC
Sorry that I didnt post the images directly but I can not since I do no have 10 reputation points.
I've tried converting it to RGBA but same thing happened.
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
background = PhotoImage(file="back.png")
backgroundlabel = Label(root, image=background)
backgroundlabel.pack()
character = PhotoImage(file="hero.png")
characterlabel = Label(root, image=character)
characterlabel.place(x=0,y=0)
root.mainloop()
You just need to use the canvas widget in Tkinter. Only the canvas widget supports transparency. This has to do with how Tkinter draws the display. As of right now, your code is just overlaying two images. Tkinter does not know how to compose them with transparency unless you use the canvas widget.
See the following code:
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
frame = Frame(root)
frame.pack()
canvas = Canvas(frame, bg="black", width=700, height=400)
canvas.pack()
background = PhotoImage(file="background.png")
canvas.create_image(350,200,image=background)
character = PhotoImage(file="hero.png")
canvas.create_image(30,30,image=character)
root.mainloop()
All I did was download the images you provided. I did not modify the images. So, the bottom line is that you just need to use the canvas widget.
VoilĂ !
Note: The question asked is a duplicate of How do I make Tkinter support PNG transparency?
I am making a game in Tkinter and I would really like partially transparent images in the game for obvious reasons...
How would I do that? PNG doesn't allow transparency, and JPEG does allow transparency but isn't considered an 'image file'... What file type would allow that? Can I use bitmap somehow to make a JPEG useable? Thanks!
Some use info:
I started learning Python 3 months ago and am not horribly good, but I do know classes, functions, and how to make stuff in Tkinter. I also obviously know a lot more, but as I said I am not too terribly good at it. Thanks!
Tkinter does recognise .ico files, but I have never used them apart from as an icon at the top left.
I would use a GIF file, and tk.PhotoImage to display it. This does allow for transparent images to be shown:
My Proof:
The image in a tk window, but when I change the background colour to say yellow, the button background is the same as the tk window background, so it is transparent.
EDIT: HERE IS CODE
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file = "FILENAME.gif")
label1 = tk.Button(image = image1)
label1.pack()
root.mainloop()
hope that helps you.
from tkinter import *
root = Tk()
startpic = PhotoImage(file = "ddsp.gif")
startbut = PhotoImage(file = "start.gif")
helpbut = PhotoImage(file = "help.gif")
exitbut = PhotoImage(file = "exit.gif")
root.geometry("640x480")
root.title("Deep Death")
root.resizable(0,0)
starter = Label(image = startpic)
starter.pack()
start = Label(image = startbut)
start.place(x=340,y=80)
root.mainloop()
Even though the .gifs' are transparent, they are appearing with a white background.
I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\user\desktop\33.jpg, and I want to take only that jpg file and I have to display that image using label or something. If it is possible, I want to know how?
Thanks in advance!
Here is a sample code for what you are asking:
from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog
root = Tk()
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()
root.mainloop()
You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins.
Also you might wanna add more file extensions to filetypes
I found a lot of similar questions, but not quite the solution to this case :
I want to
Load an image file from disk
Crop it (lazy or not)
Place it on a TKinter canvas
And oh, it would even be better that step 1 would not need to be a gif-file, but even if it has to be I'll be happy. That's it..
I can load a file, I can crop it (In PIL) I can place it on a canvas (In TKinter), but I don't seem able to combine it all.. (So maybe a simple cast from PIL to TKinter is enough?) I'm a newbee in TKinter of course.
There is ImageTk module in PIL.
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
im = Image.open("image.png")
cropped = im.crop((0, 0, 200, 200))
tk_im = ImageTk.PhotoImage(cropped)
canvas.create_image(250, 250, image=tk_im)
root.mainloop()