Search code examples
pythonimageformatjpegpython-imageio

Grayscale image not a jpeg


I created a greyscale image like this

def create_new_image(size, luminance):
    width, height = size
    black_frame = int(luminance) * np.ones((width, height, 1), dtype=np.uint8)
    return black_frame

Where luminance is element of [0, 255]

I have saved the image using imageio

def save_image(image, output_path):
    imageio.imwrite(output_path, image)

Where the output_path is something like /valid_path/img.jpg

Now I want to load my grayscale image back:

img = imageio.imread(file, format ='jpg')

But what I get is a syntax error.

raise SyntaxError("not a JPEG file")
  File "<string>", line None
SyntaxError: not a JPEG file

If I don't specify the format, I get another error.

    "Could not find a format to read the specified file in %s mode" % modename
ValueError: Could not find a format to read the specified file in single-image mode

Why? Thanks


Solution

  • You can try :

    def save_image(image, output_path):
        imageio.imwrite(output_path, format= "jpg", image)
    

    to explicitly state that it is a jpg file.