Png getting corrupted

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
gegupta
Posts: 30
Joined: 2010-12-03T12:14:09-07:00
Authentication code: 8675308

Png getting corrupted

Post by gegupta »

Hello All,

Here is the link to an 189X189 png image which gets corrupted if you try to process through IM. Take this image
http://origin-media.onecall.com/Image_P ... detail.png
and process(resize and change format to Jpeg) this image using say "convert Panasonic-RP-HS200-K-Black-vfr-detail.png -resize 50% x.jpg"

I did "identify" to see if there is any corruption in the image but things look fine. I do not see any problem with other similar(same size 32 bit colorspace etc) Png format images.
Is there anything which I am missing?

Thanks
Geetanjali

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

The image probably contains an alpha channel (transparency), which is supported by png but not jpeg. I'll check.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

Is this caused by -resize or the png->jpeg conversion?

Try:
convert in.png -background white -flatten out.jpg

gegupta
Posts: 30
Joined: 2010-12-03T12:14:09-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by gegupta »

Yes it does contain Alpha transparency I am assuming . This is a snapshot of one portion of identify

Code: Select all

 Channel depth:
   red: 8-bit
   green: 8-bit
   blue: 8-bit
   alpha: 8-bit
 Channel statistics:
   Red:
     min: 0 (0)
     max: 255 (1)
     mean: 185.141 (0.726043)
     standard deviation: 71.9342 (0.282095)
     kurtosis: -0.572878
     skewness: -0.73367
   Green:
     min: 2 (0.00784314)
     max: 255 (1)
     mean: 186.039 (0.729563)
     standard deviation: 70.8937 (0.278015)
     kurtosis: -0.583379
     skewness: -0.728894
   Blue:
     min: 4 (0.0156863)
     max: 255 (1)
     mean: 186.921 (0.733022)
     standard deviation: 70.1594 (0.275135)
     kurtosis: -0.567485
     skewness: -0.738369
   Alpha:
     min: 0 (0)
     max: 255 (1)
     mean: 36.6207 (0.143611)
     standard deviation: 87.2251 (0.342059)
     kurtosis: 2.18987
     skewness: -2.02794
 Image statistics:
   Overall:
     min: 0 (0)
     max: 255 (1)
     mean: 194.12 (0.761254)
     standard deviation: 75.384 (0.295624)
     kurtosis: 0.20161
     skewness: -1.1291
 Alpha: rgba(255,255,255,0)   #FFFFFF00
And I think you are right. When i tried just the resize operation(not simultaneous covert to jpeg), there is no corruption.

Thanks for the reply :)

gegupta
Posts: 30
Joined: 2010-12-03T12:14:09-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by gegupta »

GreenKoopa wrote:Is this caused by -resize or the png->jpeg conversion?

Try:
convert in.png -background white -flatten out.jpg

So the above command you gave essentially removes that alpha transparency?

Thanks

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Png getting corrupted

Post by fmw42 »

In what way is the image corrupted by your processing?

gegupta
Posts: 30
Joined: 2010-12-03T12:14:09-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by gegupta »

I used this
"convert Panasonic-RP-HS200-K-Black-vfr-detail.png -resize 50% x.jpg"

Actually there are couple of variation with different resize parameter I guess. It distorts it, or make whole image gray black or inserts some artifacts ..

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Png getting corrupted

Post by fmw42 »

what kind of artifacts?

perhaps you should post a link to your input and output images for others to test. what version of IM are you using and what platform?

If you are using an old IM, note that PNG processing is being upgraded over the last few releases, so you might want to upgrade.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

Transparent over the blue background:
Image
Converting to jpeg, you will forever choose one background color.

The 'corruption' may not be resize or ImageMagick at all. The fully transparent pixels have a color, even if it doesn't show. For many images the transparent areas have a uniform color, but there is no requirement. When you remove the alpha layer, the transparent areas (blue above) could be anything.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

The left image is as given. The second is with the alpha layer thrown out. The right is with the image overlaid on a white background.
Image
convert Panasonic-RP-HS200-K-Black-vfr-detail.png -alpha off middle.png
convert Panasonic-RP-HS200-K-Black-vfr-detail.png -background white -flatten right.png

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Png getting corrupted

Post by fmw42 »

First it is hard to process your combined image. It would have been better to have uploaded 3 separate images. But given what you have, I see no issues with it. (Even going back to his original post, that I apologize for missing, I get the same result). The background image under the transparency is odd, but I don't think that comes from IM processing, but what is actually in your image. Once you process it to flatten it against a background or composite it when displayed as here, you effectively remove those bad parts of the original image anyway.

How was the original (Panasonic-RP-HS200-K-Black-vfr-detail.png) created? Do you have the steps you performed and the original image before any alpha channel was added? Could the uploading have cause the background corruption (I really doubt that, but it is possible).

Perhaps I don't understand the problem. If so, please clarify further.

However, when I resize the Panasonic-RP-HS200-K-Black-vfr-detail.png it looks fine with the alpha channel, but when I turn off the alpha channel that odd part of the background turns black.

So something odd is happening, but I am not sure just yet what. As I said there have been some changes to PNG processing and this could be a side effect. Hopefully glennrp can look at this.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

gegupta, does adding -background white -flatten give you what you need? I think we have a short solution, surrounded by much explanation.
fmw42 wrote:First it is hard to process your combined image. It would have been better to have uploaded 3 separate images.
Sorry, I was trying to demonstrate what was going on to the original poster. I did provide all the code used to generate them if you would like to recreate either.

gegupta
Posts: 30
Joined: 2010-12-03T12:14:09-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by gegupta »

Yes GreenKoopa mostly it does. But I experimented with few other images they work fine without removing/replacing background. Alpha statistics of one of them is like
Alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
I think the individual components also matters. I apologise, I dont have expert experience with transparencies.

fmw42,
I am using ImageMagick-6.2.8 which is little old. But this happens with OpenCV as well. I dont think it is processing fault exactly but image decoding fault as all libraries use same png decoder.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Png getting corrupted

Post by GreenKoopa »

Again, I don't believe that there is any fault/bug. That 'corruption' is in the original image. You moved to a format with no alpha support without instructing ImageMagick how you wanted that handled.

Having an alpha channel gives an image the ability to do transparency. If an image has every pixel at 255 (no transparency), then the image is simply not using that ability, so the middle and right images would be identical for that case. Most images will look similar, many like the one you gave won't. The edges, which often contain semi-transparency, are the first place to look for differences. The overlay method of the right image is safer in that it handles all images, and most flexible in that it lets you choose the background color. fmw42 has far more experience than me, and so can probably direct you to more options if you need something different. Do you need something different, or are you simply curious to understand?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Png getting corrupted

Post by fmw42 »

The input image is what it is. Turning off the alpha channel leave behind what is there. However, I am concerned that when resizing by 50%, the output looks fine until you turn off the alpha channel. Then all the funny striped area, i.e. everything under the transparent areas turn black. I don't think that should happen. This is happening with IM 6.6.6.2 Q16 (hdri) Mac OSX Tiger.

Locked