I am trying to read a png file into a python-flask application running in docker and am getting an error that says
ValueError: Could not find a format to read the specified file in mode
'i'
i have uploaded a file using an HTML file and now i am trying to read it for further processing. i see that scipy.misc.imread is deprecated and i am trying to replace this with imageio.imread
if request.method=='POST':
file = request.files['image']
if not file:
return render_template('index.html', label="No file")
#img = misc.imread(file)
img = imageio.imread(file)
i get this error :
File "./appimclass.py", line 34, in make_prediction
img = imageio.imread(file)
File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 221, in imread
reader = read(uri, format, "i", **kwargs)
File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader
"Could not find a format to read the specified file " "in mode %r" % mode
Different, but in case helpful. I had an identical error in a different library (skimage), and the solution was to add an extra 'plugin' parameter like so -
image = io.imread(filename,plugin='matplotlib')
Had the exact same problem recently, and the issue was a single corrupt file. Best is to use something like PIL to check for bad files.
import os
from os import listdir
from PIL import Image
dir_path = "/path/"
for filename in listdir(dir_path):
if filename.endswith('.jpg'):
try:
img = Image.open(dir_path+"\\"+filename) # open the image file
img.verify() # verify that it is, in fact an image
except (IOError, SyntaxError) as e:
print('Bad file:', filename)
#os.remove(dir_path+"\\"+filename) (Maybe)
I had this problem today, and found that if I closed the file before reading it into imageio the problem went away.
Error was:
File "/home/vinny/pvenvs/chess/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader "Could not find a format to read the specified file " "in mode %r" % mode ValueError: Could not find a format to read the specified file in mode 'i'
Solution:
Put file.close() before images.append(imageio.imread(filename)), not after.
Add the option "pilmode":
imageio.imread(filename,pilmode="RGB")
It worked for me.
I encountered the same error, and at last, I found it was because the picture was damaged.
I had accidentally saved some images as PDF, so the error occurred. resolved after deleting those incompatible format images.
Related
I have a folder that contains 2000 TIF images and I want to convert them to jpg images.
I wrote two codes and both work well until they convert 370 images and then they raise an error
Here is my first code :
DestPath='/media/jack/Elements/ToJPG95/'
from PIL import Image
import os
def change(path, row):
filename1=path+row
filename=row.split('.')[0] + '.jpg'
im = Image.open(filename1)
img= im.convert('RGB')
Dest=os.path.join(DestPath,filename)
img.save(Dest, format='JPEG',quality=95)
import csv
sourcePath='/media/jack/Elements/TifImages/'
with open("TIFFnames.csv") as f:
filtered = (line.replace('\n', '') for line in f)
reader = csv.reader(filtered)
for row in filtered:
change(sourcePath , row)
and here is my second code which I ran in inside the folder that has the images :
from PIL import Image # Python Image Library - Image Processing
import glob
DestPath='/media/jack/Elements/ToJPG95/'
print(glob.glob("*.TIF"))
for file in glob.glob("*.TIF"):
im = Image.open(file)
rgb_im = im.convert('RGB')
rgb_im.save(DestPath+file.replace("TIF", "jpg"), quality=95)
# based on SO Answer: https://stackoverflow.com/a/43258974/5086335
they convert up to 370 images and then give an error
Here is the error I am getting :
Traceback (most recent call last):
File "conmg.py", line 7, in <module>
rgb_im = im.convert('RGB')
File "/home/jack/.local/lib/python3.6/site-packages/PIL/Image.py", line 873, in convert
self.load()
File "/home/jack/.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "/home/jack/.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
I have tried imagemagick mentioned in the solution Here
but this is what I am getting when I click enter to run the command:
jack#jack-dell:/media/jack/Elements/TifImages$ for f in *.tif; do echo "Converting $f"; convert "$f" "$(basename "$f" .tif).jpg"
>
>
>
>
As you can see, it does nothing
I think the codes work well but for some reason they fail after converting 370 images
I am running on a 6 TB external hard drive.
Can any one tell me please whats wrong ?
As #fmw42 says, you likely have a problem with the 370th file (corrupt, or some ill-supported TIFF variant). You bash code will convert all the files that can be read, it doesn't work because you are missing a closing done:
for f in *.tif; do echo "Converting $f"; convert "$f" "$(basename "$f" .tif).jpg" ; done
Your Python would also convert all the readable files if you use try/except to catch errors and continue with the next file:
for file in glob.glob("*.TIF"):
try:
im = Image.open(file)
rgb_im = im.convert('RGB')
rgb_im.save(DestPath+file.replace("TIF", "jpg"), quality=95)
except:
print('File not converted:',file)
I'm currently trying to write a simple Python (3.6.6) program that can grab a Youtube video, play it back, and graph the waveplot and spectogram of the file. But it only runs properly in Jupyter notebook. I'm using this site as a guide on the program. I want to make sure it can run in IDLE as well, but so far no luck. Here's the code section regarding the file retrieval / path set and trying to play it back:
# Downloading audio
audiostream = video.getbestaudio()
# audiostream.download()
from tkinter import filedialog as fd
# Asking where to save it
print("Select the directory...")
dir_name = fd.askdirectory()
print(dir_name)
path = dir_name + "/" + video.title + ".wav"
print(path)
audiofile = audiostream.download(filepath=path)
import IPython.display as ipd
# Playing back the audio
print("Playing back audio...")
ipd.Audio(filename=path)
And the error message:
Traceback (most recent call last):
File "C:\Users\(myPCname)\Desktop\YTpyDwnlder.py", line 45, in <module>
ipd.Audio(filename=path)
File "C:\Python36\lib\site-packages\IPython\lib\display.py", line 110, in __init__
super(Audio, self).__init__(data=data, url=url, filename=filename)
File "C:\Python36\lib\site-packages\IPython\core\display.py", line 627, in __init__
self.reload()
File "C:\Python36\lib\site-packages\IPython\lib\display.py", line 121, in reload
super(Audio, self).reload()
File "C:\Python36\lib\site-packages\IPython\core\display.py", line 652, in reload
with open(self.filename, self._read_flags) as f:
OSError: [Errno 22] Invalid argument: 'C:/Users/(myPCname)/Desktop/gui/Low Roar - "I\'ll Keep Coming".wav'
The path seems correct for the most part so I'm not sure why the program thinks it's wrong. I've tried to double and triple check through looking up other sites on Python string formatting for files and I can't find what's wrong. I've also tried inserting '\' to all potential single or double quotes within (provided they don't already have the '\' before them) the string but it still doesn't like it (I know this is a futile attempt and is redundant).
How can I fix this?
Also sorry if the tags are incorrect; this is my first time posting a question on here.
Nevermind. I found that the problem was due to invalid characters in the filename ('"' is changed to '#' in the filename).
I am trying to insert an wmf file to docx using python-docx which is producing the following traceback.
Traceback (most recent call last):
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 79, in <module>
read_ppt(path, file)
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 73, in read_ppt
write_docx(ppt_data, False)
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 31, in write_docx
document.add_picture(slide_data.get('picture_location'), width=Inches(5.0))
File "C:\Python34\lib\site-packages\docx\document.py", line 72, in add_picture
return run.add_picture(image_path_or_stream, width, height)
File "C:\Python34\lib\site-packages\docx\text\run.py", line 62, in add_picture
inline = self.part.new_pic_inline(image_path_or_stream, width, height)
File "C:\Python34\lib\site-packages\docx\parts\story.py", line 56, in new_pic_inline
rId, image = self.get_or_add_image(image_descriptor)
File "C:\Python34\lib\site-packages\docx\parts\story.py", line 29, in get_or_add_image
image_part = self._package.get_or_add_image_part(image_descriptor)
File "C:\Python34\lib\site-packages\docx\package.py", line 31, in get_or_add_image_part
return self.image_parts.get_or_add_image_part(image_descriptor)
File "C:\Python34\lib\site-packages\docx\package.py", line 74, in get_or_add_image_part
image = Image.from_file(image_descriptor)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 55, in from_file
return cls._from_stream(stream, blob, filename)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 176, in _from_stream
image_header = _ImageHeaderFactory(stream)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 199, in _ImageHeaderFactory
raise UnrecognizedImageError
docx.image.exceptions.UnrecognizedImageError
The image file is in .wmf format.
Any help or suggestion appreciated.
python-docx identifies the type of an image-file by "recognizing" its distinctive header. In this way it can distinguish JPEG from PNG, from TIFF, etc. This is much more reliable than mapping a filename extension and much more convenient than requiring the user to tell you the type. It's a pretty common approach.
This error indicates python-docx is not finding a header it recognizes. Windows Metafile format (WMF) can be tricky this way, there's a lot of leeway in the proprietary spec and variation in file specimens in the field.
To fix this, I recommend you read the file with something that does recognize it (I would start with Pillow) and have it "convert" it into the same or another format, hopefully correcting the header in the process.
First I would try just reading it and saving it as WMF (or perhaps EMF if that's an option). This might be enough to do the trick. If you have to change to an intermediate format and then back, that could be lossy, but maybe better than nothing.
ImageMagick might be another good choice to try because it probably has better coverage than Pillow does.
Explanation
python-docx/image.py will read differernt picture file format from SIGNATURES
Format
1.jpg
Use Image converter to convert 1.jpg to different file formats.
Use magic to get mime type.
File format
Mime type
add_picture()
.jpg
image/jpeg
√
.png
image/png
√
.jfif
image/jpeg
√
.exif
√
.gif
image/gif
√
.tiff
image/tiff
√
.bmp
image/x-ms-bmp
√
.eps
application/postscript
×
.hdr
application/octet-stream
×
.ico
image/x-icon
×
.svg
image/svg+xml
×
.tga
image/x-tga
×
.wbmp
application/octet-stream
×
.webp
image/webp
×
How to solve
Plan A
Convert other format to supported formats like .jpg
Install
pip install pillow
Code
from pathlib import Path
from PIL import Image
def image_to_jpg(image_path):
path = Path(image_path)
if path.suffix not in {'.jpg', '.png', '.jfif', '.exif', '.gif', '.tiff', '.bmp'}:
jpg_image_path = f'{path.parent / path.stem}_result.jpg'
Image.open(image_path).convert('RGB').save(jpg_image_path)
return jpg_image_path
return image_path
if __name__ == '__main__':
from docx import Document
document = Document()
document.add_picture(image_to_jpg('1.jpg'))
document.add_picture(image_to_jpg('1.webp'))
document.save('test.docx')
Plan B
First, try to add picture into Word manually. If success, it means Word supports this format. Then modify this library by inheriting the BaseImageHeader class and implementing the from_stream() method with SIGNATURES adding the image format.
Lack of file suffix
modify 1.jpg to 1
from docx import Document
document = Document()
document.add_picture('1')
document.save('test.docx')
It will show this
Using this
from docx import Document
document = Document()
document.add_picture(open('1', mode='rb'))
document.save('test.docx')
Conclusion
import io
from pathlib import Path
import magic
from PIL import Image
def image_to_jpg(image_path_or_stream):
f = io.BytesIO()
if isinstance(image_path_or_stream, str):
path = Path(image_path_or_stream)
if path.suffix in {'.jpg', '.png', '.jfif', '.exif', '.gif', '.tiff', '.bmp'}:
f = open(image_path_or_stream, mode='rb')
else:
Image.open(image_path_or_stream).convert('RGB').save(f, format='JPEG')
else:
buffer = image_path_or_stream.read()
mime_type = magic.from_buffer(buffer, mime=True)
if mime_type in {'image/jpeg', 'image/png', 'image/gif', 'image/tiff', 'image/x-ms-bmp'}:
f = image_path_or_stream
else:
Image.open(io.BytesIO(buffer)).convert('RGB').save(f, format='JPEG')
return f
if __name__ == '__main__':
from docx import Document
document = Document()
document.add_picture(image_to_jpg('1.jpg'))
document.add_picture(image_to_jpg('1.webp'))
document.add_picture(image_to_jpg(open('1.jpg', mode='rb')))
document.add_picture(image_to_jpg(open('1', mode='rb'))) # copy 1.webp and rename it to 1
document.save('test.docx')
So my program is able to open PNGs but not PDFs, so I made this just to test, and it still isn't able to open even a simple PDF. And I don't know why.
from PIL import Image
with Image.open(r"Adams, K\a.pdf") as file:
print file
Traceback (most recent call last):
File "C:\Users\Hayden\Desktop\Scans\test4.py", line 3, in <module>
with Image.open(r"Adams, K\a.pdf") as file:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2590, in open
% (filename if filename else fp))
IOError: cannot identify image file 'Adams, K\\a.pdf'
After trying PyPDF2 as suggested (Thanks for the link by the way), I am getting this error with my code.
import PyPDF2
pdf_file= open(r"Adams, K (6).pdf", "rb")
read_pdf= PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
print number_of_pages
Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
Following this article: https://www.geeksforgeeks.org/convert-pdf-to-image-using-python/ you can use the pdf2image package to convert the pdf to a PIL object.
This should solve your problem:
from pdf2image import convert_from_path
fname = r"Adams, K\a.pdf"
pil_image_lst = convert_from_path(fname) # This returns a list even for a 1 page pdf
pil_image = pil_image_lst[0]
I just tried this out with a one page pdf.
As pointed out by #Kevin (see comment below) PIL has support for writing pdfs but not reading them.
To read a pdf you will need some other library. You can look here which is a tutorial for handling PDFs with PyPDF2.
https://pythonhosted.org/PyPDF2/?utm_source=recordnotfound.com
I'm trying to export resized JPEGs of a given image using the following code (the downloading part is omitted, since that works fine):
basewidth = 400 # user-defined variable
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
theitemishere = "/home/myusername/public_html/resizer/" + filename
img.save(theitemishere + extension, extension_caps)
However, I get the following error when it comes time to save the new image (here's the traceback):
File "/home/myusername/public_html/cgi-bin/PIL/Image.py", line 1467, in save
save_handler(self, fp, filename)
File "/home/myusername/public_html/cgi-bin/PIL/JpegImagePlugin.py", line 557, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
File "/home/myusername/public_html/cgi-bin/PIL/ImageFile.py", line 466, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/home/myusername/public_html/cgi-bin/PIL/Image.py", line 395, in _getencoder
return encoder(mode, *args + extra)
TypeError: function takes at most 11 arguments (13 given)
Any thoughts on why this is happening?
FWIW, I'm unable to install the PIL module on the server, which is why I have it as a subdirectory of cgi-bin.
I had the same problem and solved it. Writing this solution as I felt that the above answer is not descriptive enough for anyone else having the same problem and looking for solutions
I had this problem because I had both PIL n Pillow installed. So had to uninstall one of them. That solved the problem.
Thanks.
You might want to skip everything and start from loading image and immediately saving it with:
img.save("/home/myusername/public_html/resizer/file.jpg", format="JPEG")
and see what happens. if it works, then add more details, resizing and other stuff.
ah, and don't forget to check write permissions on the folder where you save, because web-server usually runs under different user name.