Pillow looks to be the solution to a lot of my problems, but I can't get it to work correctly. I think there might be something that I'm missing but I can't find any solutions that work.
from PIL import Image
img = Image.open("base64 (1).png")
print(img.size)
print(img.format)
img.show()
I have this set up to load an image, print its size and format, and then show the image. When I run the code, I get this:
(1920, 1080)
PNG
Traceback (most recent call last):
File "C:/Users/##########/PycharmProjects/weatherCanvas/imageTest.py", line 7, in <module>
img.show()
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1736, in show
_show(self, title=title, command=command)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2449, in _show
_showxv(image, **options)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2454, in _showxv
ImageShow.show(image, title, **options)
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 51, in show
if viewer.show(image, title=title, **options):
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 75, in show
image = image.convert(base)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 250, in load
raise_ioerror(e)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The same thing happens if I replace img.show() with saving it. I know that PIL and Pillow save a temporary file in order to do the show() method, so I think the problem is somewhere in that.
Is there something I'm missing here?
Related
I recently updated Pillow from version 6.1.2 to version 7.1.2 and tried to load a 16-bit tiff image, which I mainly work with on a daily basis. Prior to the update, I could load these images fine, but now it raises an error:
q = Image.open(file)
q
Traceback (most recent call last):
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\IPython\core\formatters.py", line 345, in __call__
return method()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 671, in _repr_png_
self.save(b, "PNG")
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 2100, in save
self._ensure_mutable()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 617, in _ensure_mutable
self._copy()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 610, in _copy
self.load()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
Out[21]: <PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1024x1024 at 0x180FD99E438>
From looking at other answers, I see that Pillow has had issues with 16-bit images before but I thought they had been resolved. Is there a way to work around this error and access the image data? Or do I need to downgrade to my previous version?
I tried to execute ImageFont.truetype.
You can download the woff file here.
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype('89874f10.woff', 40)
However, I received the following errors.
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
font = ImageFont.truetype('89874f10.woff', 10)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 642, in truetype
return freetype(font)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 639, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 187, in __init__
self.font = core.getfont(
OSError: invalid argument
What is the reason of error?
How to solve the problem?
Thank you very much.
I think you either need to install the freetype library to handle that, or convert the font to conventional Truetype using a converter such as this.
I'm using windows 10, I'm using tesseract latest version for text recognition, below is the sample code I'm using now. But sometimes for some images, it gives the following.
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-
OCR\tesseract.exe'
config = ("-l eng --oem 1 --psm 7")
text=pytesseract.image_to_string(cv2.imread(r'C:\Users\Kesavan\Desktop\project\text Recogniton\opencv-text-recognition\images\cat.jpg'),config=config)
print(text)
Traceback (most recent call last):
File "C:/Users/Kesavan/PycharmProjects/Project/text recogniton/tester.py", line 6, in <module>
text=pytesseract.image_to_string(cv2.imread(r'C:\Users\Kesavan\Desktop\project\text Recogniton\opencv-text-recognition\images\cat.jpg'),config=config)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 350, in image_to_string
}[output_type]()
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 349, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 265, in run_and_get_output
return output_file.read().decode('utf-8').strip()
File "C:\Users\Kesavan\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 119, in __exit__
next(self.gen)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 177, in save
cleanup(f.name)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 134, in cleanup
raise e
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 131, in cleanup
remove(filename)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Kesavan\\AppData\\Local\\Temp\\tess_jjp7wfoq.txt'
This is the image for which I'm facing the issue
Thanks in advance
try to upgrade to the latest pytesseract version or follow this workaround:
https://groups.google.com/d/msg/tesseract-ocr/IyPpisQ1E4U/tl3IP3gqAwAJ
When I try to JPEG-Decompress (old-style JPEG compression, not JPEG-LS and not JPEG2000) the RAW data, I get following error:
Traceback (most recent call last):
File "raw-reader.py", line 766, in <module>
raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 206, in imread
reader = read(uri, format, 'i', **kwargs)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 129, in get_reader
return format.get_reader(request)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 168, in get_reader
return self.Reader(self, request)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 217, in __init__
self._open(**self.request.kwargs.copy())
File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 398, in _open
pilmode=pilmode, as_gray=as_gray)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 122, in _open
self._im = factory(self._fp, '')
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 780, in jpeg_factory
im = JpegImageFile(fp, filename)
File "/home/ian/.local/lib/python3.6/site-packages/PIL/ImageFile.py", line 102, in __init__
self._open()
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 339, in _open
handler(self, i)
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 166, in SOF
raise SyntaxError("cannot handle %d-bit layers" % self.bits)
SyntaxError: cannot handle 14-bit layers
The RAW data in the image is 14-bit JPEG data, and imageio isn't able to read it. When I tried using pillow, it didn't even recognize the data as JPEG. My question now is: How can I decompress the data without writing my own JPEG decompressor, while keeping in mind that the data is 14 bits?
My code:
import io
import imageio
allbytes = open("raw_data.dat", "rb").read()
raw_packed_image_data = allbytes
raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))
The file raw_data.dat is a file containing purely the RAW-Image data compressed with JPEG. Link to raw_data.dat
raw_data.dat is a JPEG Lossless, Nonhierarchical file with 2 frames and a precision > 8-bit, a very rare format.
The imagecodecs package can read the file (assuming that the _imagecodecs Cython extension is present):
>>> from imagecodecs import jpegsof3_decode
>>> data = open('raw_data.dat', 'rb').read()
>>> image = jpegsof3_decode(data)
>>> image.shape
(3528, 2640, 2)
>>> image.dtype
dtype('uint16')
The LEADTOOLS SDK should also be able to read the file (not tested).
I believe the issue can be solved using a different library to load the image. Similar underlying issue has been posted here How to combine 3 high range JPEG2000 images into single RGB one?. You can use something along these lines:
import matplotlib.image as mpimg
img_red = mpimg.imread('raw_data.dat')
Then you can use the read bitmap for further manipulation as if it was loaded via PIL/Pillow/imageio.
I'm trying to reduce read/write on my sd card by writing image files to DropBox directly. I captured the image into stream, but I don't know how to write it to Dropbox. This is with the raspberry pi.
dbx = dropbox.Dropbox('token')
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (1280,960)
camera.capture(stream,format='jpeg')
stream.seek(0)
im = Image.open(stream)
dbx.files_upload(im,"/test.jpg",mute=True)
i get the following error, but not sure what it means or how to fix it.
Traceback (most recent call last):
File "./camera.py", line 25, in <module>
dbx.files_upload(im,"/test.jpg",mute=True)
File "/usr/local/lib/python2.7/dist-packages/dropbox/base.py", line 1225, in files_upload
f,
File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 249, in request
timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 341, in request_json_string_with_retry
timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 385, in request_json_string
type(request_binary))
TypeError: expected request_binary as binary type, got <type 'instance'>