-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
image/jpeg: add options to partially decode or tolerantly decode invalid images? #10447
Comments
It seems to decode fine for me, on tip. I know your Go version is only a few days old, but can you "git sync" and re-try? |
Strange, I updated to package main
import (
"image/jpeg"
"net/http"
"fmt"
)
func main() {
res, err := http.Get("https://streamcoimg-a.akamaihd.net/000/340/810/9ae536dd97d2d92fc17a6590509a51c0.jpg")
if err != nil {
panic(err)
}
defer res.Body.Close()
img, err := jpeg.Decode(res.Body)
if err != nil {
panic(err)
}
fmt.Println(img.Bounds())
} And here's the output:
|
Your program works for me with tip. Are you sure you're using Go tip?
Add a fmt.Println(runtime.Version()) to your program and make sure you're
not accidentally using an older version.
|
Added
Anyway, as long as this is fixed on tip I'm happy to close this. |
Hello! |
I'm not sure what the problem is, but it's not a regression: I see the same error on the stable release (Go 1.6). We're in code freeze for the upcoming 1.7 release; I'll take a look at it once the tree opens again for 1.8. |
I'm uploaded image with problem to github for safety, because hoster can remove her. |
I met the same problem. It raise "OSError: image file is truncated (53 bytes not processed)" when I use python to download a jpeg file, the binary data can be save to file with ImageFile.LOAD_TRUNCATED_IMAGES=True, but the truncated pixel will set to black. with go, all pixel process well and not truncate but it will raise "invalid JPEG format: short Huffman data" when decode it. the jpeg show full in safari but truncated in chrome. maybe go need truncate the jpeg as python does |
I have the same issue with pictures from my phone, I attach one in case it helps $ go version |
package main
import (
"fmt"
"image/jpeg"
"net/http"
)
func main() {
urls := []string{
"https://streamcoimg-a.akamaihd.net/000/340/810/9ae536dd97d2d92fc17a6590509a51c0.jpg",
"https://cloud.githubusercontent.com/assets/6634115/19217480/2c442a36-8ddc-11e6-8392-4b45725b49ef.jpg",
}
for _, u := range urls {
res, err := http.Get(u)
if err != nil {
panic(err)
}
defer res.Body.Close()
img, err := jpeg.Decode(res.Body)
if err != nil {
panic(err)
}
fmt.Println(img.Bounds())
}
} I got fail in the second @elektroid mentioned.
first.
second.
|
This comment has been minimized.
This comment has been minimized.
@elektroid I'll try to find some time next week to look at it, but FWIW, I get e-mail for every comment on this issue, and somewhere along the mail pipeline, or in my browser's JPEG decoder, that attachment doesn't look like a valid JPEG. I've attached a screenshot from my mail, where I've added a pink ring to emphasize where it breaks down. |
@elsonwu can you give more details than "some special jpg will cause this problem"? Can you attach an example? Do other programs (e.g. web browsers, Photoshop) handle those special JPEGs OK or do they also reject them? |
@nigeltao, what's the status here? |
I switched to "gopkg.in/gographics/imagick.v1/imagick" hoping it would cope with my improper files but it fails to load them too. |
Sorry, I didn't find the time to make a detailed investigation, and there have been no recent changes to Go's image/jpeg package, but it sounds like non-Go software is also reporting errors with some or all of these cases. |
Yeah, but I can open it in Chrome. I thought we tried to match whatever browsers do. |
All of the failing testcases here and others that I've found are truncated. They don't have complete SOS segments, don't contain an EOI, and raise warnings with other decoders. In @elsonwu's case, it's fixed by appending Still, there is a bug in that there's no way to decode truncated images, which seem relatively common and are readable with most other decoders. My first thought is to return a partially decoded image (if there is one) along with the error; see special@c7a05f3. I don't mind adding docs/tests and submitting that if the approach is ok. Otherwise, it seems slightly inappropriate to silence legitimate decoding errors, and there's no API for decoding options, so I'm not sure what else to do. |
We could possibly change jpeg.Decode (and the other image codecs) to return (non-nil Image, non-nil error) with partial results if it encounters an error, although that's unusual in general for functions returning (T, err), and certainly not going to happen for Go 1.8. As for matching whatever browsers do, what browsers do influences how far down the Postel's law slope we slip, but I'm wary of the slippery slope, and according the JPEG spec, these are invalid images. |
Just did a quick look at this for my reasons and summarized it like this:
I'm saying the files are not truncated by examining the bottom right block and seeing pixels - as opposed to parking garage which is clearly truncated a couple of lines of blocks early. I didn't check metadata vs lines of pixels however, so whole lines of blocks could be missing. The ICC_PROFILE may be something that's accepted in JPEG formats, or the format descriptions I was looking at weren't showing the working standard (as opposed to the published standard). It clearly decodes in other decoders in any case. Anyways, I don't have a solution to your quandary about returning an error and an image, I suppose you could put the decoded image in the error (ack), but my thought is that the issue might better be described as instead of "image/jpeg: Unable to decode valid JPEG image" as "image/jpeg: Unable to decode invalid/truncated JPEG image". Cheers! |
I have another example of invalid JPEG image. The problem with the file is that the second half of the file is filled with garbage bytes:
|
@bradfitz can we change the title of this issue? This is now about doing something for invalid images, not valid ones. |
@rasky, done. |
still running into this issue in |
Currently experiencing an error on the go side with image decoding from flickr. See golang/go#10447
@sheerun's example looks like a progressive JPEG that's truncated. I only say that because it's so blurry and has that JPEG blocky look to it and I assume another pass would sharpen it. I didn't look at its structure. |
yes indeed. I guess it's out of scope of go-lang to nicely handle such case? I think ideal API would return both partially loaded image and flag that tells that image is corrupted |
Yeah, I'd still say what I said a couple of years ago: |
go version go1.13.4 windows/amd64 |
Would it be reasonable to have a few error types in this package so that a user can selectively identify and deal with a specific error such as |
I just thought I'd leave this here, in the hopes of being helpful.
|
So to recap:
Great job! |
In case, anybody does care, I have forked now the jpeg package and fixed some of the issues with the help of the code of @whorfin |
go version devel ce43e1f Mon Apr 13 23:27:35 2015 0000 linux/amd64
Attempted to use
jpeg.Decode
on the below image:https://streamcoimg-a.akamaihd.net/000/340/810/9ae536dd97d2d92fc17a6590509a51c0.jpg
Expected the image to decode successfully, as it displays in a browser.
Actual result:
invalid JPEG format: short Huffman data
The text was updated successfully, but these errors were encountered: