OSError: broken data stream when reading image file - python-3.5

I am trying to read an image file using Image package of Keras.
Here is my code.
from keras.preprocessing import image
img_path = 'test/test_image.jpg' # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
When I run the code, I get the following error.
anaconda3/lib/python3.5/site-packages/PIL/ImageFile.py in load(self)
238 if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
239 # still raised if decoder fails to return anything
--> 240 raise_ioerror(err_code)
241
242 # post processing
anaconda3/lib/python3.5/site-packages/PIL/ImageFile.py in raise_ioerror(error)
57 if not message:
58 message = "decoder error %d" % error
---> 59 raise IOError(message + " when reading image file")
60
61
OSError: broken data stream when reading image file
Please note, if I convert test_image.jpg to test_image.png, then the given code works perfectly. But I have several thousands of pictures and I can't convert all of them to png format. I tried several things after searching for solution in web but couldn't get rid of the problem.
Any help would be appreciated!

Use this at the beginning of your code:
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
I found it here. And this is working for me.

According to here Pillow upgrade by pip install Pillow --upgrade should solve this issue.
If you are still facing the problem you can use mogrify to batch convert all your images. mogrify -format png *.jpg

Related

I cant read the image file in the particular directory

I took some images in my camera and tried to resize them using opencv library but i think that i can't read the images I don't know the reason why.Thank you for the help in advance.
I have a python 3.8 version and the updated opencv library version.Not much of a background I guess.
import os,cv2
count=0
for file in os.listdir('E:\Projects\Python\Resixing images\Images'):
if file.endswith('.jpg'):
print(file)
img=cv2.imread(file)
img2=img.copy()
img2=cv2.resize(img2,(700,700))
name="resize"+str(count)+".jpg"
cv2.imwrite(name,img2)
count+=1
I receive an error message
P_20191107_214848_SRES.jpg
Traceback (most recent call last):
File "E:\Projects\Python\Resixing images\image changing res.py", line 7, in
img2=img.copy()
AttributeError: 'NoneType' object has no attribute 'copy'
[Finished in 9.7s]
Try this:
img=cv2.imread('E:\Projects\Python\Resixing images\Images' + '\' + file)
The problem was that you were sending only the name of the file to the python program, so the program tried to look for the image in the current directory and not at your specified path. The above change should fix the problem.
also, a good idea would be to have 2 // instead of 1 /, just to avoid any format specifier in the middle of things, or you could just use r to mention the path to be raw string
img=cv2.imread('E:\\Projects\\Python\\Resixing images\\Images' + '\\' + file)
img=cv2.imread(r'E:\Projects\Python\Resixing images\Images\' + file)

how to fix an error during uploading custom dataset in to colab?

I have followed all the steps described in most tutorials on how to upload your custom dataset into google colab. but I am getting an error which I try a lot to fix but not working.
I am trying to train a CNN model using my custom dataset. I try to upload it on colab using the code snippet given in most tutorials.
the following error is displayed when I run the code snippet
Downloading zip file
---------------------------------------------------------------------------
HttpError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/files.py in FetchMetadata(self, fields, fetch_all)
236 fields=fields)\
--> 237 .execute(http=self.http)
238 except errors.HttpError as error:
6 frames
HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW?alt=json returned "File not found: https://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW">
During handling of the above exception, another exception occurred:
ApiRequestError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/files.py in FetchMetadata(self, fields, fetch_all)
237 .execute(http=self.http)
238 except errors.HttpError as error:
--> 239 raise ApiRequestError(error)
240 else:
241 self.uploaded = True
ApiRequestError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW?alt=json returned "File not found: https://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW">
#this is the code snippet I have taken from tutorials to upload dataset to google colab.
!pip install -U -q PyDrive
# Insert your file ID
# Get it by generating a share URL for the file
# An example : https://drive.google.com/file/d/1iz5JmTB4YcBvO7amj3Sy2_scSeAsN4gd/view?usp=sharing
zip_id = 'https://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW'
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import zipfile, os
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
if not os.path.exists('MODEL'):
os.makedirs('MODEL')
# 2. Download Zip
print ("Downloading zip file")
myzip = drive.CreateFile({'id': zip_id})
myzip.GetContentFile('model.zip')
# 3. Unzip
print ("Uncompressing zip file")
zip_ref = zipfile.ZipFile('model.zip', 'r')
zip_ref.extractall('MODEL/')
zip_ref.close()
OMG. after a long hour (almost 8 hours) researching on the internet and brainstorming i found the answer. if any one who is new working on colab and face a similar error here is how i solved this error. The problem on the above code is the way we assign the file id. zip_id = 'https://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW'. most of the tutorial I have seen told us just to take the file id by right clicking the file in google drive and copy the share link address. but the file id is not the whole thing we copied. the file id is only after the id= which is in my case 1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW. After giving this as an id the error is gone. Hope this response will help other colab starters.

Can't load PDF with Wand/ImageMagick in Google Cloud Function

Trying to load a PDF from the local file system and getting a "not authorized" error.
"File "/env/local/lib/python3.7/site-packages/wand/image.py", line 4896, in read self.raise_exception() File "/env/local/lib/python3.7/site-packages/wand/resource.py", line 222, in raise_exception raise e wand.exceptions.PolicyError: not authorized `/tmp/tmp_iq12nws' # error/constitute.c/ReadImage/412
The PDF file is successfully saved to the local 'server' from GCS but won't be loaded by Wand. Loading images into OpenCV isn't an issue, just happening when trying to load PDFs using Wand/ImageMagick
Code to load the PDF from GCS to local file system into Wand/ImageMagick is below
_, temp_local_filename = tempfile.mkstemp()
gcs_blob = STORAGE_CLIENT.bucket('XXXX').get_blob(results["storedLocation"])
gcs_blob.download_to_filename(temp_local_filename)
# load the pdf into a set of images using imagemagick
with(Image(filename=temp_local_filename, resolution=200)) as source:
#run through pages and save images etc.
ImageMagick should be authorised to access files on the local filesystem so it should load the file without issue instead of this 'Not Authorised' error.
PDF reading by ImageMagick has been disabled because of a security vulnerability Ghostscript had. The issue is by design and a security mitigation from the ImageMagick team will exist until. ImageMagick Enables Ghostscript processing of PDFs again and Google Cloud Functions update to that new version of ImageMagick with PDF processing enabled again.
There's no fix for the ImageMagick/Wand issue in GCF that I could find but as a workaround for converting PDFs to images in Google Cloud Functions, you can use this [ghostscript wrapper][2] to directly request the PDF conversion to an image via Ghostscript and bypass ImageMagick/Wand. You can then load the PNGs into ImageMagick or OpenCV without issue.
requirements.txt
google-cloud-storage
ghostscript==0.6
main.py
# create a temp filename and save a local copy of pdf from GCS
_, temp_local_filename = tempfile.mkstemp()
gcs_blob = STORAGE_CLIENT.bucket('XXXX').get_blob(results["storedLocation"])
gcs_blob.download_to_filename(temp_local_filename)
# create a temp folder based on temp_local_filename
temp_local_dir = tempfile.mkdtemp()
# use ghostscript to export the pdf into pages as pngs in the temp dir
args = [
"pdf2png", # actual value doesn't matter
"-dSAFER",
"-sDEVICE=pngalpha",
"-o", temp_local_dir+"page-%03d.png",
"-r300", temp_local_filename
]
# the above arguments have to be bytes, encode them
encoding = locale.getpreferredencoding()
args = [a.encode(encoding) for a in args]
#run the request through ghostscript
ghostscript.Ghostscript(*args)
# read the files in the tmp dir and process the pngs individually
for png_file_loc in glob.glob(temp_local_dir+"*.png"):
# loop through the saved PNGs, load into OpenCV and do what you want
cv_image = cv2.imread(png_file_loc, cv2.IMREAD_UNCHANGED)
Hope this helps someone facing the same issue.

ValueError: Could not find a format to read the specified file in mode 'i'

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.

Python Windows 10 64bit - FFMPEG for trackpy

Similar problem to 'Python Moviepy installation problems (windows 7x64)' except the solution provided did not work.
I have windows 10, 64bit. Every time I attempt to run the following line of code:
frames = pims.Video('exp9_short.avi')
I get the blue Windows 10 error: This app can't run on your PC
as well as the Python error:
OSError: Could not load meta information
=== stderr ===
Access is denied.
I have tried multiple versions from 'https://ffmpeg.zeranoe.com/builds/' to no avail.
I don't know if other image processing tools will work with trackpy, or if there are any alternatives to trackpy.
I would really appreciate some advice.
I solved this problem by processing each image with OpenCV2
vid0 = cv2.VideoCapture('exp9_short.avi')
numfr = int(vid0.get(cv2.CAP_PROP_FRAME_COUNT))
for n1 in range(0,numfr-1):
success,img = vid0.read(n1) #read video frame by frame
if n1==0:
h, w, cols = img.shape #image size
fr=np.zeros([h,w,3,numfr-1]) #frames
frgr=np.zeros([h,w,numfr-1]) #grayscale fr
frgrbi=frgr #binarized frgr
fr[:,:,:,n1]=img
frgr[:,:,n1]=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

Resources