I try to run python code using Linux terminal to display a png image.but it gives me same error again and again.
from PIL import Image
im=Image.open("/content/sample_1.png")
# Lets display the image
display(im)
You could try using im.show() instead of display():
from PIL import Image
im=Image.open("/content/sample_1.png")
# Lets display the image
im.show()
display is a command on the Linux shell, and you can't use in python directly.
If you just want to show the PNG image, you can use the following code:
import subprocess
im="/content/sample_1.png"
subprocess.getoutput("display %s" %im)
I am running through the facial_recognition library tests. In it I construct a PIL.Image.Image object from a numpy array. I then attempt to open that image.
I am using the Spyder IDE on macOS Mojave. I run Spyder by running sudo spyder from the terminal.
import face_recognition
from PIL import Image
image = face_recognition.load_image_file('test_images/biden.jpg')
face_locs = face_recognition.face_locations(image)
for loc in face_locs:
top, right, bottom, left = loc
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
Upon reaching pil_image.show(), Preview opens and tries to open the image, but then displays the following error message in a window:
The file “tmp_pycgvqc.PNG” couldn’t be opened because you don’t have permission to view it.
When I use the pil_image.save() method to save the image, and then I try open it, it works fine. Only using the pil_image.show() method produces the error.
I imported an image to my project using
from PIL import Image
myImage = Image.open("myImageDirectory.png")
So myImage is now imported as a png file. But I want to display it to the Screen using Pygame. Normally I use
import pygame
win = pygame.display.set_mode((500, 500))
win.blit(myImage, (50, 50))
Now I get the Error that the function needs a surface, not a png File.
Has anyone an idea how I can convert the image to a surface or how I can display it?
I tried not much yet because I didn't found anything that could solve my problem.
Edit:
What is wrong with this way that I get the error: Couldn't open bg.png
See PIL and pygame.image. Use Image.tobytes() get the image data as a bytes object and pygame.image.fromstring() to load the data to an pygame.Surface object:
from PIL import Image
import pygame
pilImage = Image.open("myImageDirectory.png")
myImage = pygame.image.fromstring(pilImage.tobytes(), pilImage.size, pilImage.mode)
I want to display a gif image using Pillow
Here is my simple code:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
All help will be appreciated
Thanks!
PIL provides a show method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv. On Macs it uses open, on Windows it uses... something else.
If it can't find an appropriate viewer, ImageShow._viewers will be an empty list.
On Raspbian, you'll need to install an image viewer such as display, xv or fim. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command parameter:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()
What imaging modules for python will allow you to take a specific size screenshot (not whole screen)?
I have tried PIL, but can't seem to make ImageGrab.grab() select a small rectangle
and i have tried PyGame but i can't make it take a screen shot outside of it's main display panel
You can use pyscreenshot module.
The pyscreenshot module can be used to copy the contents of the screen to a PIL image memory or file.
You can install it using pip.
$ sudo pip install pyscreenshot
Usage:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
I have tried PIL, but can't seem to make ImageGrab.grab() select a small rectangle
What did you try?
As the documentation for ImageGrab clearly states, the function has a bbox parameter, and:
The pixels inside the bounding box are returned as an “RGB” image. If the bounding box is omitted, the entire screen is copied.
So, you only get the whole screen if you don't pass a bbox.
Note that, although I linked to the Pillow docs (and you should be using Pillow), old-school PIL's docs say the same thing:
The bounding box argument can be used to copy only a part of the screen.
So, unless you're using a really, really old version of PIL (before 1.1.3, which I believe is more than a decade out of date), it has this feature.
1) Use pyscreenshot, ImageGrab works but only on Windows
2) Grab the image and box it, then save that image
3) Don't use ImageGrab.grab_to_file, it saves the full size image
4) You don't need to show the image with im.show if you just want to save a screenshot
import pyscreenshot as ImageGrab
im=ImageGrab.grab(bbox=(10,10,500,500))
im.save('im.png')
You could use Python MSS.
From documentation to capture only a part of the screen:
import mss
import mss.tools
with mss.mss() as sct:
# The screen part to capture
monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
# Grab the data
sct_img = sct.grab(monitor)
# Save to the picture file
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
You can use pyscreenshot at linux or windows platforms . I am using Ubuntu it works for me. You can force if subprocess is applied setting it to false together with mss gives the best performance.
import pyscreenshot as ImageGrab
import time
t1 = time.time()
imgScreen = ImageGrab.grab(backend="mss", childprocess=False)
img = imgScreen.resize((640,480))
img.save("screen.png")
t2 = time.time()
print("The passing time",(t2-t1))