I am working on some sort of animation maker with tkinter but I have run into a problem, i would want to save the current application window or canvas widget to a .png file for later use, is this possible? Thanks in advance.
import pyautogui
myScreenshot = pyautogui.screenshot()
myScreenshot.save(r'Path to save screenshot\filename.png')
And there it is ! ^^
And if you don't want everything else on your screen, you can try to crop it with pil
How to crop an image using PIL?
Related
I am trying to create a weather App in Tkinter GUI. It is working fine. I want to add a background image to it but it wont show up. initially i did it without the reference line but it didnt work. On some website they said to keep reference of the image but that too didnt work.
Also my Tkinter window size is 1920x1080 and the image was of same dimension but it still doesnt show up. I tried to reduce image size than window but still not working. Any suggestions accepted. Also there is no error.
bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
Its sad that tkinter.PhotoImage does not support JPEG files, but it does support PNG in the newer version and has proper support for GIF too. To use JPEG you need PIL installed.
In the terminal say:
pip install Pillow
After that import it like:
from PIL import Image,ImageTk
Then now, to open the image with PIL, say:
img_file = Image.open('clearsky2.jpg')
bg = ImageTk.PhotoImage(img_file)
bgl = Label(gui,image=bg)
bgl.place(x=0, y=0, relwidth=1,relheight=1)
This will work with JPEG and PNG files as well and, keep in mind you dont need to keep a reference unless your looping over images or creating image inside of a function.
Hope it cleared your problem, do let me know if any more errors
Cheers
I'm making a Minesweeper in pygame but when I set the icon it changes the color. The icon that I get is bluer than the expected icon and I don't understand why. Here's the code that I use:
Actual Icon
Expected Icon
import pygame as pg
pg.init()
window = pg.display.set_mode(size)
icon = pg.image.load("icon.png")
pg.display.set_icon(icon)
pg.display.set_caption("Minesweeper")
I tried to use icon = pg.image.load("icon.png").convert() and icon = pg.image.load("icon.png").convert_alpha() but it doesn't work.
Do you know why it does this and how to fix it?
It looks like your PNG file has some embedded information or formatting that is causing this distortion. I saved the image as a bitmap using paint.net and it displays without modification:
Perhaps some conversion commands could transform your image as desired, but it's probably easier to do the image manipulation outside of pygame.
I want to make canvas and user will draw some picture in that which I want to save as Image. I am using Python 3.3.2. It doesn't support PIL or Image module. Can someone guide me on this..?
Thanks.
Maybe it's a bit too late to answer this question. But, I'll still go ahead.
from tkinter import *
import pyscreenshot as ImageGrab
r=Tk()
canvas = Canvas(r,height=1000,width=2000,bg="snow")
def getter():
x2=r.winfo_rootx()+canvas.winfo_x()
y2=r.winfo_rooty()+canvas.winfo_y()
x1=x2+canvas.winfo_width()
y1=y2+canvas.winfo_height()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("./test.jpg")
b1=Button(r,text="Save",command=lambda:getter())
b1.grid()
Let me know if this doesn't work. Hope it helps..
I am trying to open an image using python; I wrote the following code :
from PIL import Image
im=Image.open("IMG_1930.jpg")
im.show()
But the windows photo viewer opens but it shows the following message instead of the photos:
"windows photo viewer can not open this picture because either the picture is deleted , or it isn't in a location that is accessible."
The show method in PIL is a poor's man way of viewing an image - it has got a hardcoded image viewer application, and writes your image data to a temporary file before calling that as an external application.
What is happening there is that you are either having problems with Windows' uneven access rights policies, and the viewer can't open the file in Python's temporary directory, or there is a problem with Window's problematic path specifications - it might even be a bug in PIL, that renders the temporary paht generated by PIL unusable by the image viewer.
If you are using show in a windowing application, use your tookit's way of viewing images to display it instead - otherwise, if it is a simpler application, build up a Tkitner Window and put the image in it, instead of show.
import sys
import Tkinter
from PIL import Image, ImageTk
window = Tkinter.Tk()
img = Image.open("bla.png")
img.load()
photoimg = ImageTk.PhotoImage(img)
container = Tkinter.Label(window, image=photoimg)
container.pack()
Tkinter.mainloop()
(Linux users: some distributions require the separate install of Tkinter support for PIL/PILLOW. In Fedora, for example, one has to install the python-pillow-tk package )
I also had problems with this. Take a look at this post it fixed my problem: PIL image show() doesn't work on windows 7
Good luck fixing it.
I am a beginner at programming at can't seem to figure out how to use bgpic('photo.gif') in turtle.
I have enclosed the 'photo.gif' in the same folder as the script. Every time I execute the program with bgpic, the turtle window crashes. Do I have to resize the picture? Change its format?
bgpic only accepts gif images.
You should convert your image to gif format with photoshop or another similar tool or alternatively use for example python PIL to make the conversion programmatically