Error in readJPEG: JPEG decompression error: Not a JPEG file: starts with 0x7b 0x22

I am trying to read an image to my RShiny application. The image is hosted in an api in Rstudio connect. The image has the content-type: image/jpeg

I used the following code to load the image:

library(jpeg)
ui<-function()

server<-function(){
val <- GET(url ="url for the image",
                   add_headers("Authorization" = "Key my-api-key"))
jpeg(temp <- tempfile(fileext = ".jpeg")); 
        bytes <-val$content
        img<- readJPEG(bytes)
        plot.new()
        rasterImage(img, 0, 0, 1, 1)     
        
        dev.off()
        
        library(RCurl)
        tt <- base64Encode(readBin(temp, "raw", file.info(tf1)[1, "size"]), "txt")
        html <- sprintf('<img src="data:image/jpeg;base64,%s">', tt)
}
shinyApp(ui, server)

This is working fine when I run the app. However, when I deploy the app to the server it gives the following error;

Error in readJPEG: JPEG decompression error: Not a JPEG file: starts with 0x7b 0x22

I am confused because when I checked everything is in the jpeg format.

Any help is appreciated.

The error "Not a JPEG file: starts with 0x7b 0x22" indicates that the file you are trying to open or process is not a valid JPEG file[1]. The error message refers to the first two bytes of the file, which are represented in hexadecimal notation as 0x7b and 0x22. These bytes do not correspond to the expected "magic number" or file signature for a JPEG file.

A "magic number" is a sequence of bytes at the beginning of a file that helps identify its format[4]. For example, a JPEG file typically starts with the bytes 0xFF 0xD8, while a PNG file starts with the bytes 0x89 0x50[1][3]. The error message suggests that the file you are trying to open might be of a different format, possibly with an incorrect file extension.

To resolve this issue, you can try the following steps:

  1. Verify the file format: Check the actual format of the file using a tool like file on Linux or an online file format identification service. If the file is not a JPEG, you might need to change the file extension to match its actual format.

  2. Use a different image viewer or library: Some image viewers or libraries might be more forgiving and automatically detect the correct file format even if the file extension is incorrect. Try opening the file with a different image viewer or using a different library in your code.

  3. Check the source of the file: If the file was downloaded or obtained from an external source, it might be corrupted or have an incorrect file extension. You can try downloading the file again or contacting the source to obtain a correct version of the file.

Citations:
[1] image - Why am I getting the error: "Not a JPEG file: starts with 0x89 0x50" - Stack Overflow
[2] Error in readJPEG: JPEG decompression error: Not a JPEG file: starts with 0x7b 0x22
[3] eog - .jpg image error "Error interpreting JPEG image file (Not a JPEG file: starts with 0x52 0x49)" - Ask Ubuntu
[4] [SOLVED] Not a JPEG file: starts with 0x00 0x00
[5] Solution for “Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)” – Ibrahim El Merehbi
[6] https://www.reddit.com/r/SonyAlpha/comments/zv2j2g/error_interpreting_jpeg_image_file_starts_with/

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.