question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot write array of shape (H, W, 1) to JPEG image with v3 API

See original GitHub issue

Switching from the v2 API to the v3 API, I noticed the following difference:

This works

import numpy as np
import imageio

x = np.zeros((10, 30, 1), dtype=np.uint8)
imageio.v2.imwrite("test.jpg", x)

while the same with imageio.v3.imwrite fails with the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kpkx626\AppData\Local\Programs\Python\Python38\lib\site-packages\imageio\v3.py", line 161, in imwrite
    encoded = img_file.write(image, **kwargs)
  File "C:\Users\kpkx626\AppData\Local\Programs\Python\Python38\lib\site-packages\imageio\plugins\pillow.py", line 322, in write
    primary_image.save(self._request.get_file(), **save_args)
  File "C:\Users\kpkx626\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL\Image.py", line 2305, in save
    save_handler = SAVE_ALL[format.upper()]
KeyError: 'JPEG'

This happens with Pillow 9.1.1 and 9.2.0 and Python 3.8.10 on Windows.

Is this considered an intended change, or should PillowPlugin.write squeeze out the third dimension?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
slanzmichcommented, Jul 26, 2022

@FirefoxMetzger I created #849 to address the tifffile plugin issue.

0reactions
FirefoxMetzgercommented, Jul 22, 2022

Another related thing I just noticed is that writing a TIFF image from an array of shape (256, 256, 3) writes multipage TIFF with 256 pages. (This uses the legacy tifffile plugin.)

@slanzmich Oh? This sounds quite right. Could you make a new issue for this? tifffile is an entirely different plugin so it uses a different code path than what I shared above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving a Numpy array as an image - python - Stack Overflow
An answer using PIL (just in case it's useful). given a numpy array "A": from PIL import Image im = Image.fromarray(A) im.save("your_file.jpeg").
Read more >
Importing Image Data into NumPy Arrays | Pluralsight
Loading and displaying an image using Matplotlib, OpenCV and Keras API. Converting the loaded images to the NumPy array and back. Conducting ...
Read more >
Arrays and Array Images - Earth Engine - Google Developers
An array image of 1-D vectors created in this manner has no concept of 2-D shape. To perform 2-D only operations such as...
Read more >
Reading/Writing images and videos - PyTorch
They are currently specific to reading and writing video and images. ... Reads a JPEG or PNG image into a 3 dimensional RGB...
Read more >
Source code for monai.data.image_reader
This function returns two objects, first is numpy array of image data, ... sr = min(max(direction.shape[0], 1), 3) affine: np.ndarray = np.eye(sr +...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found