unable to view jpeg file - python

I was trying to view a jpeg file using the codes that I found online.
Here is the codes:
from PIL import Image
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
image = Image.open('/kaggle/input/chest-xray-pneumonia/chest_xray/__MACOSX/chest_xray/test/PNEUMONIA/._person121_bacteria_576.jpeg')
image.show()
But I get this error:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-1-637f422434fd> in <module>
19
20
---> 21 image = Image.open('/kaggle/input/chest-xray-pneumonia/chest_xray/__MACOSX/chest_xray/test/PNEUMONIA/._person121_bacteria_576.jpeg')
22 image.show()
/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
2685 warnings.warn(message)
2686 raise IOError("cannot identify image file %r"
-> 2687 % (filename if filename else fp))
2688
2689 #
OSError: cannot identify image file '/kaggle/input/chest-xray-pneumonia/chest_xray/__MACOSX/chest_xray/test/PNEUMONIA/._person121_bacteria_576.jpeg'
I'm wondering if this is an issue because I didn't load the data or something else?
I wrote nothing else on the notebook on the Kagglenotebook with the dataset: https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia

I usually name my files '.jpg' when using the PIL module and will then reference the appropriate file path.
I tried your code with a renamed image.jpg file and it worked fine, when I use '.jpeg' I get a similar error.
from PIL import Image
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
image = Image.open('/Users/GameComputerC/ocr3.jpg')
image.show()
Let me know if it works for you. That might only be part of the error.

Related

Pytesseract : UnidentifiedImageError: cannot identify image file '1.jpg'

I want to tried using OCR and pytesseract. I have code like this
!sudo apt install tesseract-ocr
!pip install pytesseract
import pytesseract
import shutil
import os
import random
try:
from PIL import Image
except ImportError:
import Image
from google.colab import files
uploaded = files.upload()
extractedInformation = pytesseract.image_to_string(Image.open(1.jpg))
or you can check it out here : Google Colab
But when I try to upload some image and try to extract the file,it show error like this
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-37-44411f3825fa> in <module>
----> 1 extractedInformation = pytesseract.image_to_string(Image.open('1.jpg'))
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2894
2895 Note that this function decodes pixel data only, not entire images.
-> 2896 If you have an entire image file in a string, wrap it in a
2897 :py:class:`~io.BytesIO` object, and use :py:func:`~PIL.Image.open` to load it.
2898
UnidentifiedImageError: cannot identify image file '1.jpg'
I have asked my friend to try the code but why that he has no error like me? The code is run smoothly with no error. is that something missing in my computer? or something that I should Install first?

Web Hosted Picture Reading in Sagemaker via facenet_pytorch?

Long shot but I am stuck. Running this test code in Amazon Sagemaker with a Python3 kernel and the function doesn't recognize the web location of the image (media.ci.org). I can't find anything in the documentation from facenet_pytorch about this, anyone know if this is possible? I can do this in R, but much slower.
pip install facenet-pytorch
from facenet_pytorch import MTCNN, InceptionResnetV1
# If required, create a face detection pipeline using MTCNN:
mtcnn = MTCNN()
# Create an inception resnet (in eval mode):
resnet = InceptionResnetV1(pretrained='vggface2').eval()
from PIL import Image
img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
FileNotFoundError Traceback (most recent call last)
in
----> 1 img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
/opt/conda/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
3090
3091 if filename:
-> 3092 fp = builtins.open(filename, "rb")
3093 exclusive_fp = True
3094
FileNotFoundError: [Errno 2] No such file or directory: 'https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg'
You should ideally use requests to download the image locally and then using PIL.Image to open something like this.
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))

"FileNotFoundError" when importing Image from PIL on Google Colab

I want to import an image on Google Colab from PIL. By doing so:
from PIL import Image
img = Image.open('smallhouse.jpg')
img
However, I get this error.
FileNotFoundError Traceback (most recent call last)
<ipython-input-20-8ab03a99a9db> in <module>()
1
2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
4 img
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2807
2808 if filename:
-> 2809 fp = builtins.open(filename, "rb")
2810 exclusive_fp = True
2811
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
Not sure why. Thanks!
1. You must upload the smallhouse.jpg image to your Google Drive.
2. In your .ipynb file, you need to include this code at the beginning of your application: drive.mount('/content/gdrive').
Go to the URL provided. Then, you will be prompted to give permission to access your Google Drive. Copy the code provided, access your application and paste the code there.
3. Finally, you can pass your image's path, which now is: /content/gdrive/My Drive/smallhouse.jpg.
Your code will look something like this:
# Imports
from google.colab import drive
from PIL import Image
# Mount Google Drive for fast, responsible access to files
drive.mount('/content/gdrive')
# Open image
img = Image.open('/content/gdrive/My Drive/smallhouse.jpg')
img

"cannot identify image file"-error when trying to load a tiff in python

I am trying to access to the temperature data stored in a tiff-file. I was provided with a python script that was supposed to be able to do this, but I keep getting the following error:
Traceback (most recent call last):
File "read_tiff.py", line 57, in <module>
im = Image.open(sourcePath)
File "/Users/myname/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file 'Testbild.tiff'
This is the relevant section of the code:
sourcePath = "Testbild.tiff"
im = []
try:
sourcePath = sys.argv[1]
except IndexError:
print('usage: python read_tiff.py filename')
sys.exit()
try:
im = Image.open(sourcePath)
except FileNotFoundError:
print('File not found: ' + sourcePath)
sys.exit()
imarray = np.array(im)
This is what I checked:
with another random tiff file it worked, so it is probably not the script but the file itself (/maybe I need to install some additional package??)
the tiff file can be opened without a problem in IrfanView and Photoscan
when prompting "file Testbild.tiff" I get "Testbild.tiff: TIFF image data, little-endian" so it definitely IS a tiff file
Is anyone able to help me out?
Cheers!
EDIT:
The import statement for Image is from PIL import Image
If necessary, these are all the import statements of the script:
import matplotlib.pyplot as plt
from PIL import Image
import sys
import numpy as np
import math
import cv2
Try using:
from skimage import io
im = io.imread(sourcePath)
This will open it directly as an array as well.
In my case, it worked to read .tif file to ndarray:
path2dir = r'C:\data\WORK\image4example'
name_img = 'Slice_25.tif'
path2img = os.path.join(path2dir, name_img)
im = cv2.imread(path2img , cv2.IMREAD_ANYDEPTH)
plt.imshow(im)

Don't understand this FileNotFoundError when trying to use pillow/PIL library

In the following code I am trying to use pythons pillow/PIL library. This is the first time using this and I am just trying to open my image to make sure it is working. I have my code I used below. I just googled an image and saved it using the name smallhouse.jpg. However I am running into the following errors below.
import numpy as np
from PIL import Image
img = Image.open('smallhouse.jpg')
img.show()
This is the error I am getting. Do I need to download a PIL package? I am using Jupiter notebook.
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-10-e26e3b39215f> in <module>()
1 import numpy as np
2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
4 img.show()
~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2546
2547 if filename:
-> 2548 fp = builtins.open(filename, "rb")
2549 exclusive_fp = True
2550
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
The problem described here:
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
Please make sure you put the image file smallhouse.jpg in the same directory with your notebook.

Categories