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?
Related
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
For some reason, my python program will not open my file in a directory called imgs in which I have a png file that I wish it to display when it runs.
The imgs directory is in the same location as my python program on my C drive. I have made sure that I am entering the correct names and that there is no / before imgs
It always pops up with the message
pygame.error: Couldn't open imgs/tileGrass1
tileGrass1 being the png
Any suggestions on how to fix this??
Few checks to ensure a pygame program is running well
Ensure You Initialize the pygame library at the beginning of your code
pygame.init()
Ensure Path to your image is correct and that the image exists. Check that the extension is correct (png, jpg, jpeg, etc)
It would also be useful if you share the code to your program so that the exact error is easily noted
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
I am taking pictures using the OpenCV from my webcam, and after I have identified the face, I save the face image using a cv2.imwrite in a folder in my computer.
Now, my question is how can I save the images in path on another computer?
I mean, for example, using FTP, I can directly add another path to storing the images to cv2.imwrite and put them in the another computer?
You need to use cv2.imencode to store the image to memory:
retval, buffer = cv2.imencode('.jpg', image)
Then upload the buffer:
from ftplib import FTP
from io import BytesIO
ftp = FTP('ftp.example.com')
ftp.login('username', 'password')
flo = BytesIO(buffer)
ftp.storbinary('STOR test.jpg', flo)
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?