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?
Related
I'am trying to print a QR code in the XmlReceipt (ESCPOS printed receipt) template but I'm getting this error printed in the actual receipt:
Traceback (most recent call last):
File "/home/pi/odoo/addons/hw_escpos/controllers/main.py", line 169, in run
printer.receipt(data)
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 717, in receipt
raise e
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 704, in receipt
print_elem(stylestack,serializer,root)
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 594, in print_elem
print_elem(stylestack,serializer,child)
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 594, in print_elem
print_elem(stylestack,serializer,child)
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 680, in print_elem
self.print_base64_image(bytes(elem.attrib['src'], 'utf-8'))
File "/home/pi/odoo/addons/hw_escpos/escpos/escpos.py", line 445, in print_base64_image
img_rgba = Image.open(f)
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2687, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x6b132f00>
I'm copying the exact same solution as Saudi Arabia module https://github.com/odoo/odoo/blob/14.0/addons/l10n_sa_pos/, and I already check that is rendering correctly at this step https://github.com/odoo/odoo/blob/12.0/addons/point_of_sale/static/src/js/screens.js#L1653
print_xml: function() {
var receipt = QWeb.render('XmlReceipt', this.get_receipt_render_env());
this.pos.proxy.print_receipt(receipt);
this.pos.get_order()._printed = true;
},
the image is sent as base64 svg+xml format and I already installed the iotboxv21_04 version. So I suspect that probably the pillow version 5.4.1 (which I already check that comes with that IoTbox version) can't open the SVG file type that was sent. Should I send it in PNG? how can I achieve that?
I've been doing the pytorch tutorial (https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html) and have been getting this error that I don't know how to fix. The full error is below:
Traceback (most recent call last):
File "main.py", line 146, in <module>
main()
File "main.py", line 138, in main
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
File "/engine.py", line 26, in train_one_epoch
for images, targets in metric_logger.log_every(data_loader, print_freq, header):
File "/utils.py", line 180, in log_every
for obj in iterable:
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 521, in __next__
data = self._next_data()
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 1203, in _next_data
return self._process_data(data)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 1229, in _process_data
data.reraise()
File "/usr/local/lib/python3.6/dist-packages/torch/_utils.py", line 425, in reraise
raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataset.py", line 311, in __getitem__
return self.dataset[self.indices[idx]]
File "main.py", line 64, in __getitem__
img, target = self.transforms(img, target)
File "/transforms.py", line 26, in __call__
image, target = t(image, target)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "/transforms.py", line 50, in forward
image = F.to_tensor(image)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py", line 129, in to_tensor
np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True)
TypeError: __array__() takes 1 positional argument but 2 were given
I believe it means somewhere I'm using an array with 2 arguments which isn't allowed, but I don't really know where abouts that is happening - perhaps in one of their pre written libraries?
I can share the code in full if desired, but thought its a bit unwieldy. Does anyone know what might be causing this error?
PyTorch has already considered this issue. It does not seem to be a PyTorch problem.
As xwang233 mentioned in the issue, we can fix it by downgrading pillow:
pip install pillow==8.2.0
This issue could be fixed as well by upgrading Pillow from version 8.3.0 to 8.3.1. I had the same issue with
torch==1.9.0+cu111
torchvision==0.10.0+cu111
Pillow==8.3.0
After Pillow was upgraded to 8.3.1 (with no change to torch and torchvision) as below, the issue is gone:
pip install --upgrade pillow
Thanks to DRTorresRuiz for providing the clue about Pillow.
I had the same error when using:
torch==1.9.0
torchvision==0.10.0
In my requirements.txt file I downgraded the torch library, which forced me to downgrade torchvision, and that fixed the error for me. The library versions I ended up using that did not raise the error were:
torch==1.8.1
torchvision==0.9.1
change your code:
np.array(pic ,np.float32)
to:
np.array(pic).astype('float32')
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.
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.
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?