Grayscale image not a jpeg

Viewed 657

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

4 Answers

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.

JPEG files (compressed images) start with an image marker that always contains the marker code hex values FF D8 FF. It does not have a length of the file embedded, thus we need to find JPEG trailer, which is FF D9.

See the documentation using the link at this page.

As en example, opening a jpeg image with a hexadecimal viewer (for example Hex Viewer), you should see something like this:

enter image description here

Solution: In other words, try to add the header to the file before saving it as JPEG, you should solve your problem.

The page with the API's documentation can be found here. Following the doc, you should locate the right instruction that makes you specify the format for saving (as point out by @Meto in the answer).

Concluding: the solution is just specifying the format when you physically write the image in the hard disk:

imageio.imwrite(uri, im, format=None, **kwargs)

in your case format=jpg.

Moreover,

 imageio.show_formats()

Show a nicely formatted list of available formats.

Concluding, just try to replace

imageio.imwrite(output_path, image)

with

imageio.imwrite(output_path, image, format ='jpg' )

Please note that the solution is always the same in every answer. I have just added what happens specifying a format (i.e., just writes the right header).

You need to make sure if your file is really saved as a JPG file. On Linux/Mac you can use file command to verify that.

For example, below command confirms fireside.jpg is a JPEG file:

# file fireside.jpg
fireside.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 2048x1365, components 3

If the file is not saved as JPG, try specifying file format="jpg" as

imageio.imwrite(output_path, image, format ='jpg')

This works (Verified in jupyter notebook)

import numpy as np
import imageio

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

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

img = create_new_image((256, 256), 125)
save_image(img, "test.jpg")
img1 = imageio.imread("test.jpg", format ='jpg')
Related