Capturing x,y Coordinates with Python PIL - python

I want to display an image to the user with PIL and when the user clicks anywhere on this image, I want a def onmousedown(x,y) to be called. I will do some extra stuff in this function. How can I do this in PIL?
Thanks,

PIL won't do it alone -- PIL is an image manipulation library with no User Interfaces - it does have a showmethod, which does open an external program which displays the image, but does not communicate back with the Python process.
Therefore, in order to be able to get a user to interact with an image, one does have to build a GUI program using one of the consolidated toolkits for use with Python - the better known ones are Tkinter, GTK and Qt4. Tkinter is interesting because it comes pre-installed with Windows Python installs, and therefore is more easily available for users of that system. Windows users would have to separately download and install gtk or qt libraries to be able to use your program if you decide to use on of the other toolkits.
Here is a minimalist example of a Tkinter application with a clickable image:
import Tkinter
from PIL import Image, ImageTk
from sys import argv
window = Tkinter.Tk(className="bla")
image = Image.open(argv[1] if len(argv) >=2 else "bla2.png")
canvas = Tkinter.Canvas(window, width=image.size[0], height=image.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(image.size[0]//2, image.size[1]//2, image=image_tk)
def callback(event):
print "clicked at: ", event.x, event.y
canvas.bind("<Button-1>", callback)
Tkinter.mainloop()

Here is another related post
How to display picture and get mouse click coordinate on it
On Ubuntu to install
sudo apt-get install python python-tk idle python-pmw python-imaging python-imaging-tk
Then it all works.
I added a resize to #jsbueno's solution and fixed one import issue.
import Tkinter
from PIL import ImageDraw, Image, ImageTk
import sys
window = Tkinter.Tk(className="bla")
image = Image.open(sys.argv[1] if len(sys.argv) >=2 else "bla2.png")
image = image.resize((1000, 800), Image.ANTIALIAS)
canvas = Tkinter.Canvas(window, width=image.size[0], height=image.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(image.size[0]//2, image.size[1]//2, image=image_tk)
def callback(event):
print "clicked at: ", event.x, event.y
canvas.bind("<Button-1>", callback)
Tkinter.mainloop()

Related

Icon does not appear when I use iconbitmap()

OS: Linux - Ubantu
Image I have used: It was originally .ico but I converted online to .xbm
I tried different images and different websites but nothing seems to work
Code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("B")
root.iconbitmap('#/home/zaki/Pictures/mar.xbm')
root.mainloop()
The window appears normally but without icon
Try to use the iconphoto() method.
from tkinter import *
root = Tk()
img = PhotoImage(file="image.png") # Replace "image.png" with any image file.
root.iconphoto(False, img)
root.mainloop()
Works on Ubuntu 18.04 with Python 3.6.
References:
https://www.geeksforgeeks.org/iconphoto-method-in-tkinter-python/

Python Screen Shot, only desktop background (MacBook Catalina)

I am trying to take a screen shot of a GUI window generated by tkinter, but when I take a screen shot I only get the desktop and not the Python window.
I read with the new o/s Catalina you have to give the terminal permissions, which I have done but still I cannot grab the python window. Even if I do a full screen shot I still cannot see the Python window.
https://github.com/BoboTiG/python-mss/issues/134
I have tried several different ways, but still cannot get to screen grab the Python window.
Does any one else have these problems with Catalina O/S?
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas, BOTH, NW, W
from tkinter.ttk import Frame, Label, Style
import pyscreenshot
import io
import os
import subprocess
import sys
import mss
top_border_height = 50
bottom_border_height = 70
screen_width = 800
screen_height = 480
video_icon_640x480_x = (800-640)/2
video_icon_640x480_y = (480-480)/2
homeicon64x64_x = 8
homeicon64x64_y = 8
root = Tk('test Screen')
root.geometry("800x480")
w = Canvas(root, width=screen_width, height=screen_height)
back_ground = ImageTk.PhotoImage(Image.open("./icon/wireframe_mode_background.png"))
w.create_image(0, 0, image=back_ground, anchor='nw')
w.video_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_640x480.png"))
w.create_image(video_icon_640x480_x, video_icon_640x480_y, image=w.video_icon_640x480, anchor="nw")
w.home_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(homeicon64x64_x,homeicon64x64_y,image=w.home_icon_640x480, anchor="nw")
w.video_icon_640x480_1 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,8,image=w.video_icon_640x480_1, anchor="nw")
w.video_icon_640x480_2 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,80,image=w.video_icon_640x480_2, anchor="nw")
w.pack()
root.mainloop()
im = pyscreenshot.grab(bbox=(10, 10, 510, 510)) # X1,Y1,X2,Y2
im.save('screenshot.png')
with mss.mss() as sct:
filename = sct.shot(mon=-1, output='fullscreen.png')
print(filename)
The answer lies in the Python release (or maybe Pillow) setting up the needed resource when on MacOS so the permission is requested when not available. Until that happens, each user of the code / application will have to enable the Python Launcher to have the Screen Recording permission added since Catalina. I simply had to add this limitation / hiccup for MacOS users to my applications' user manual for now User Manual Screenshot. I have made a separate, specific question about this at Can you request the MacOS Screen Recording permission in Python
If this changes or you know a way to request an OS permission from within a Python application, please let us know. The comment above about answering a similar question is not valid. It is not addressing this very specific MacOS permission issue added since Catalina. (I do not have enough points to comment on the comment but could answer the question; go figure.)

Ipython notebook dead kernel from image insert in Tkinter window using PIL

I have tried for several days to get an image displaying using the following code.
import Tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
path = "Aaron.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
The code is originally from this link.
How do I insert a JPEG image into a python Tkinter window?
When I run the code with my own path I get the following error messages
Python quit unexpectedly warning
and then:
The kernel appears to have died. It will restart automatically.
System info
Mac OS X Version 10.7.5
Anaconda
ipython-notebook version 3.2.1
Python 2.7.10-0
PIL 1.1.7
pillow 2.9.0
This is my first question on stackoverflow so please excuse any formatting errors. I will try to correct if there is an issue. I've also tried many version of code that is similar with the same result. This only happens with this specific code.

Insert a .jpg in a canvas with tkinter and Python 3.2

So I want to put a .jpg in a canvas, all i found on internet is to use PIL but i'm using Python 3.2 so PIL doesn't work.
What can i do to insert a .jpg in a canvas with Python 3.2 ?
Just to save anyone else viewing this now from hunting around for the bits and pieces (like I just did)
As Martijn Pieters said use Pillow rather than PIL, but the code looks the same
from tkinter import Tk, Canvas
from PIL import ImageTk, Image
root = Tk()
#Create a canvas
canvas = Canvas(root, width=400, height=300)
canvas.pack()
# Load the image file
im = Image.open('test_image.jpg')
# Put the image into a canvas compatible class, and stick in an
# arbitrary variable to the garbage collector doesn't destroy it
canvas.image = ImageTk.PhotoImage(im)
# Add the image to the canvas, and set the anchor to the top left / north west corner
canvas.create_image(0, 0, image=canvas.image, anchor='nw')
root.mainloop()
PIL does work on Python 3.2; install Pillow, the friendly PIL fork.
Pillow 2.0.0 adds Python 3 support and includes many bug fixes from around the internet.
You just need to create the image in the canvas. Make sure that your image is in the same folder as your code.
image = PhotoImage (file="image.jpg")
yourcanvas.canvas.create_image (0, 0, anchor=NW, image=image, tags="bg_img")
That should do it. This will also extend the canvas to the size of the image, just to let you know. Good luck with your project!

How do I make Tkinter support PNG transparency?

I put in a partially transparent PNG image in Tkinter and all I get is this
How do I make the dark triangle on the right clear? (like it's supposed to be)
This is python 2.6 on Windows 7, btw.
Here's an example (the PNG file example.png has lots of transparency in different places):
from Tkinter import Tk, Frame, Canvas
import ImageTk
t = Tk()
t.title("Transparency")
frame = Frame(t)
frame.pack()
canvas = Canvas(frame, bg="black", width=500, height=500)
canvas.pack()
photoimage = ImageTk.PhotoImage(file="example.png")
canvas.create_image(150, 150, image=photoimage)
t.mainloop()
You need to make sure the image has been stored as "RGBA" which is RGB with an alpha channel. You can check for that using a graphics program of your choice, or using PIL (Python Imaging Library):
import Image
im = Image.open("button.png")
print im.mode
This should print "RGBA". If not, you'll have to make sure the alpha channel is saved with the image. You'll have to consult your graphics program manual for how to do that.

Categories