3

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

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • please provide the **full script** to re-create the problem (including the python version and the library version used). – Leos313 Mar 22 '20 at 11:21
  • @Jürgen K. Can you show the exact value of `output_path`? – DarK_FirefoX Mar 22 '20 at 22:25
  • @DarK_FirefoX, `output_path` is simply a path somewhere in your machine. Example: `output_path = '/home/DarKFirefox/Document/your/path/'`. It is just a string. – Leos313 Mar 23 '20 at 23:54
  • @Leos313, I know it is supposed to be. I am just asking to check, because the problem he is having may as well be due to a wrong path to the image file. Because, as stated on the answers below: it worked for me just fine, even without specifying de `format=jpg`. `imageio` documentation states that it will choose the correct decoder/encoder according to the file (maybe the file extension). So @Jürgen K, I encourage you to `print(output_path)` and check if it is the correct path to the file. – DarK_FirefoX Mar 25 '20 at 17:16
  • @DarK_FirefoX, good point! right! – Leos313 Mar 25 '20 at 17:58

4 Answers4

3

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.

Meto
  • 638
  • 7
  • 18
1

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).

Leos313
  • 5,152
  • 6
  • 40
  • 69
  • ok, can you provide the code that you used to create the image? insert the whole script so I can try to have a look at the entire program. – Leos313 Mar 20 '20 at 19:41
0

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')
Amol
  • 1,084
  • 10
  • 20
-1

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')
nkvns
  • 580
  • 3
  • 5