Pillow : How to get Image Object from a raw file - python

How can I get a raw file into an Image Object of Pillow python library ?
when i try the following code :
path = '/Users/me/Desktop/SonyRX100III_raw.ARW'
img = Image.open(path)
I get this error
OSError: cannot identify image file '/Users/me/Desktop/SonyRX100III_raw.ARW'

Related

PIL unable to identify image file

The object list_of_contents[0] is based64. I am trying to convert it to it's original image.
image_64_decode = base64.b64decode(list_of_contents[0])
image_result = open('test.jpg', 'wb')
image_result.write(image_64_decode)
img = Image.open(r'C:\Users\st-am\CANCER_APP\test.jpg')
However, I get the following error:
PIL.UnidentifiedImageError: cannot identify image file 'C:\\Users\\st-am\\CANCER_APP\\test.jpg'
Anybody has an idea why PIL cannot identify the image?
Here is the image

Inserting icc color profile into image using Pillow ImageCMS

I am trying to insert an ICC color profile into an image. Using code from this post as an example my code looks like this:
from PIL import Image, ImageCms
# Read image
img = Image.open(IMG_PATH)
# Read profile
profile = ImageCms.getOpenProfile(PROFILE_PATH)
# Save image with profile
img.save(OUT_IMG_PATH, icc_profile=profile)
I get the following error
Traceback (most recent call last):
File "/home/----/Documents/code_projects/hfss-misc/icc_profiles/insert_icc_profile_into_image.py", line 17, in <module>
img.save(OUT_IMG_PATH, icc_profile=srgb_profile)
File "/home/----/.virtualenvs/color-correction/lib/python3.6/site-packages/PIL/Image.py", line 2102, in save
save_handler(self, fp, filename)
File "/home/----/.virtualenvs/color-correction/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 706, in _save
markers.append(icc_profile[:MAX_DATA_BYTES_IN_MARKER])
TypeError: 'PIL._imagingcms.CmsProfile' object is not subscriptable
I thought that there might be a problem with my ICC profile so I tried to use one generated by Pillow.
from PIL import Image, ImageCms
# Read image
img = Image.open(IMG_PATH)
# Creating sRGB profile
profile = ImageCms.createProfile("sRGB")
# Save image with profile
img.save(OUT_IMG_PATH, icc_profile=profile)
I still get the same error, however.
Does anyone know what the cause for this error is?
My system environment is as follows:
Ubuntu 18.04
Python 3.6
Pillow==7.0.0
The answer from github at (https://github.com/python-pillow/Pillow/issues/4464) was to use profile.to_bytes():
img.save(OUT_IMG_PATH, icc_profile=profile.tobytes())

Error while trying to get the EXIF tags of the image

I'm trying to get the EXIF tags of an JPG image. To do this, I'm using piexif module.
The problem is that I get an error - KeyError, saying this:
Traceback (most recent call last):
File "D:/PythonProjects/IMGDateByNameRecovery/recovery.py", line 98, in selectImages
self.setExifTag(file_str)
File "D:/PythonProjects/IMGDateByNameRecovery/recovery.py", line 102, in setExifTag
exif = piexif.load(img.info["Exif"])
KeyError: 'Exif'
I've did everything as in the docs, here on some questions StackOverflow and on pypi website. Everything the same. My code:
img = Image.open(file)
exif_dict = piexif.load(img.info["exif"])
altitude = exif_dict['GPS'][piexif.GPSIFD.GPSAltitude]
print(altitude)
How do I read the image's EXIF tags then? Am I doing it wrong?
Please, I'm so clueless. This is such a weird error.
Pillow only adds the exif key to the Image.info if EXIF data exists. So if the images has no EXIF data your script will return the error because the key does not exist.
You can see what image formats support the info["exif"] data in the Image file formats documentation.
You could do something like this...
img = Image.open(file)
exif_dict = img.info.get("exif") # returns None if exif key does not exist
if exif_dict:
exif_data = piexif.load(exif_dict)
altitude = exif_data['GPS'][piexif.GPSIFD.GPSAltitude]
print(altitude)
else:
pass
# Do something else when there is no EXIF data on the image.
Using mydict.get("key") will return a value of None if the key does not exist where as mydict["key"] will throw a KeyError.
Say you have encoded metadata within the MakerNotes.
Make sure to install the following dependencies:
pixeif
Pillow
Then run the following code considering the image is Image.png and is in the same directory of the script:
from PIL import Image
import piexif
import pickle
img = Image.open('Image.png')
exif_dict = img.info.get("exif") # returns None if exif key does not exist
if exif_dict:
exif_data = piexif.load(exif_dict)
raw = exif_data['Exif'][piexif.ExifIFD.MakerNote]
tags = pickle.loads(raw)
print(tags)

Getting error while extracting text from Image with type 'PIL.PpmImagePlugin.PpmImageFile' using pytesseract

trying to extract text from image whose type is 'PIL.PpmImagePlugin.PpmImageFile' using pytesseract. The code and the error is as below
from pdf2image import convert_from_path
pages = convert_from_path('D:/pdf_csv/HealthCare/eRDS - ML/eRDS - ML/2001468/2001468,69,70.pdf',poppler_path='C:/Users/Hp/poppler-0.68.0/bin')
text = pyt.image_to_string(Image.open(pages[0]), lang='eng')
Error I am getting:
AttributeError: 'PpmImageFile' object has no attribute 'read'
Or Is there any method to convert the PpmImageFile to 'jpg' or 'png' format
Add fmt='jpeg' or fmt='png' to your function call to get non-PPM images from pdf2image.
In you example, change
pages = convert_from_path('D:/pdf_csv/Health....001468,69,70.pdf',poppler_path='C:/Users/Hp/poppler-0.68.0/bin')
to
pages = convert_from_path('D:/pdf_csv/Health...001468,69,70.pdf', fmt='jpeg', poppler_path='C:/Users/Hp/poppler-0.68.0/bin')

wxPhyton error loading a image failed to load image from file

I have an error on wxPhyton. When I try to load and image It gives me this error:
"Failed to load image from file14:25:14:'resources/AboutPlatformDUT255b.bmp'
can't open file 'resources/AboutPlatformDUT255b.bmp' (error 0: la operación se completó correctamente.)
14:25:14: Failed to load image from file "resources/AboutPlatformDUT255b.bmp".
I try to change the format between jpg, png, bmp and to resize the resolution but doesn't works. Any idea?
Solved,
The problem was the path of the image file. I don´t not why but I can`t get the absolut path to show some images.

Categories

Resources