I want my python script so save the python window as a .JPG, which it does. But when I try and open that .jpg with any application it tells me the file type is unsupported.
When I save my file as a .eps file it opens correctly but when I try and save as any other file type the file will not open.
win.postscript(file="image.jpg", colormode='color')
from PIL import Image as NewImage
img = NewImage.open("image.jpg")
Tkinter canvas object can only be saved as a postscript file, which is actually a postscript printer language text file.
Thus if you call:
win.postscript(file="image.jpg", colormode='color')
It will still write and create a postscript file, that is why it works when you rename the extension [or simply append] with .ps
Check this blog for a better implementation: https://www.daniweb.com/programming/software-development/code/216929/saving-a-tkinter-canvas-drawing-python
Related
I have lots of images saved with no file extension.
I am trying to convert those images and do some work with PIL:
for img_path in list_imgs:
with Image.open(img_path) as img:
# more code
For some files this works ok but for others it shows PIL.UnidentifiedImageError. I tried to handle the exception by opening the file with OpenCV:
img = cv2.imread(img_path)
But I get the following error:
[ WARN:0#2.312] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_tiff.cpp (462) cv::TiffDecoder::readData OpenCV TIFF: TIFFRGBAImageOK: Sorry, requested compression method is not configured
If I manually changed the file extension to .jpg for the specific file that files, I can open it using Windows Photos or any other app, even Microsoft Paint, so I know that the file is not damaged. However, both OpenCV and PIL have problems reading said file.
How can I open the image? Are there any other parameters that can be configured? Or can it be read directly into a NumPy array and then handled to PIL?
I have a script that is downloading and performing certain operations on files from a website. A couple of times a week some of the images will throw this error (edit for bump):
OSError: cannot identify image file '57343948435235236aede7dceb672559.png'
Even when manually running code as simple as the following, it produces the error:
imagefile = Image.open('57343948435235236aede7dceb672559.png')
Here are the important facts:
If I redownload the file using requests.get() I get the same error when attempting to open it.
If I redownload the file by saving it using a web browser, I instead get the error PIL.UnidentifiedImageError: cannot identify image file when attempting to open it using PIL
I can view the files (using Window Photo Viewer, a Linux image viewer, etc.) so they aren't completely corrupted.
If I open the image in Paint and save it under the same name, I can then open it using PIL.
Given all this, it appears to be an issue with specific image files hosted on the site; they are viewable in applications, but not openable using PIL. I'm assuming something is wrong with the byte structure or headers in the image, and saving overwrites those issues. Is there some library or automatic process I can use to 'fix' these images when the error occurs?
Below, I am including a minimal, reproducible example. PLEASE NOTE: THE EXAMPLE URL IN THIS CASE IS NSFW IN NATURE, AND THEREFORE HAS BEEN REDACTED. SEE MY COMMENT TO THIS POST FOR A PASTEBIN LINK TO THE FULL CODE.
from PIL import Image
url = 'SEE ABOVE NOTE'
file_name =(url).split('/')[-1:][0]
# Downloading the image file data.
file_data = get(url).content
# Write the data to a file.
with open(file_name, 'wb+') as file_object:
file_object.write(file_data)
# Opening the image with PIL
with Image.open(file_name) as image_file:
# "OSError: cannot identify image file" happens here
pass
when I display an image with PIL Image it opens an imagemagick window but the title is some gibberish name like 'tmpWbfj48Bfjf'. How do I make the image filename to be the title of the viewer window?
Use the title attribute: Image.show(title="Your Title Here" [...]
From the documentation:
Image.show(title=None, command=None) Displays this image. This method
is mainly intended for debugging purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it (usually Paint).
Parameters: title – Optional title to use for the image window, where
possible. command – command used to show the image
I'm having trouble saving my Matplotlib plots to a readable .tiff format. The file itself does save, but when I double-click on it on the folder, I get the following error in Windows Photo Viewer:
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer."
But attempted workarounds such as trying to load the file with Paint or Photo do not work. I get the feeling that the file is not saving correctly. (The image displays just fine and can be saved as a .gif just fine.) How do I resolve this?
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.