I want to get the photo(thumbnail) of a window which I already have it's handle (hwnd) using python in windows os.
From the link i posted in your question comments, I was able to get a sample working that thumbnails my python interpreter window.
from PIL import ImageGrab, Image
import win32gui
hwnd = 2622054 # My python intepreter window
thumbnailsize = 128, 128
# Set the current window to your window
win32gui.SetForegroundWindow(hwnd)
# Get the size of the window rect.
bbox = win32gui.GetWindowRect(hwnd)
# Grab the image using PIL and thumbnail.
img = ImageGrab.grab(bbox)
img.thumbnail(thumbnailsize, Image.ANTIALIAS)
# Save.
img.save('c:/test/test.png')
Related
Python Versions: 3.7.9
I've tried using Pillow/PIL with show() and close() however that just closes the command prompt and leaves the file viewer still open.
I've tried subprocess Popen but that just gives me the error 'The system cannot find the file specified' even though the file is correct. So I'm just stuck right now. I figured this would've been a simple process for Python.
I'm just trying to open an image and then after the user clicks a button, be able to close it.
Open to suggestions/libraries. Thanks!
Just call this function and a new window will pop up with the image.
Your code wont be executed until the window is closed.
import tkinter
from PIL import ImageTk, Image
def image_preview (path, size = (0, 0)):
root = tkinter.Tk ()#Start window
root.title ("Preview")#Modify this to change the title
rawimg = Image.open(path)#Open image path
if size != (0, 0):#If a specific resolution has been requested
rawimg = rawimg.resize (size)#Change the image resolution
img = ImageTk.PhotoImage(rawimg)#Adapt the image to tkinter
panel = tkinter.Label (root, image = img)#Add image to the window
panel.pack ()#Build window
root.mainloop ()#Run window
Example line to use this:
image_preview ("image.png", (500, 500))#With specified resolution
image_preview ("image.png")#Use the image resolution
I am attempting to load a .gif image into a canvas. Once loaded the picture is to be resized using the .scale method on the canvas. The image reloads but does not resize.
I am using python 3.6. I tried it in python 2.7 with the same result although on the full implementation rather that the simplified code shown below.
import tkinter as tk
f = "C:\Fanny Poer.gif"
root = tk.Tk()
dis = tk.Canvas(root)
dis.grid()
new_image = tk.PhotoImage(file=f)
id = dis.create_image(500, 500, image = new_image)
dis.scale(id, 0, 0, .1, .1)
root.mainloop()
The image loads but does not resize. What am I doing wrong?
Once loaded the picture is to be resized using the .scale method on the canvas.
You can't do that with the scale method. That method only affects the coordinates of an object. It won't resize an image.
To resize an image you can use the subsample and zoom methods on the PhotoImage instance, or you can use some other library (eg: pillow) to resize the image.
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.
I am currently working on reading an image and displaying it to a window. I have successfully done this, but upon displaying the image, the window only allows me to see a portion of the
full image. I tried saving the image after loading it, and it saved the entire image. So I am fairly certain that it is reading the entire image.
imgFile = cv.imread('1.jpg')
cv.imshow('dst_rt', imgFile)
cv.waitKey(0)
cv.destroyAllWindows()
Image:
Screenshot:
Looks like the image is too big and the window simply doesn't fit the screen.
Create window with the cv2.WINDOW_NORMAL flag, it will make it scalable. Then you can resize it to fit your screen like this:
from __future__ import division
import cv2
img = cv2.imread('1.jpg')
screen_res = 1280, 720
scale_width = screen_res[0] / img.shape[1]
scale_height = screen_res[1] / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
cv2.resizeWindow('dst_rt', window_width, window_height)
cv2.imshow('dst_rt', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
According to the OpenCV documentation CV_WINDOW_KEEPRATIO flag should do the same, yet it doesn't and it's value not even presented in the python module.
This can help you
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
In openCV whenever you try to display an oversized image or image bigger than your display resolution you get the cropped display. It's a default behaviour.
In order to view the image in the window of your choice openCV encourages to use named window. Please refer to namedWindow documentation
The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
cv.namedWindow(name, flags=CV_WINDOW_AUTOSIZE)
where each window is related to image container by the name arg, make sure to use same name
eg:
import cv2
frame = cv2.imread('1.jpg')
cv2.namedWindow("Display 1")
cv2.resizeWindow("Display 1", 300, 300)
cv2.imshow("Display 1", frame)
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()