Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

Tools like PNGOUT blithely strip out color profiles (the iCCP chunk), which arguably makes the tools lossy because it changes the appearance of non-sRGB images in nearly all modern browsers.

I've found a solution. Gimp will offer to autoconvert to sRGB when opening an affected image. Saving and using PNGOUT is then perfectly fine. The original and Gimp-assisted recompressed images will give identical screenshots from a browser to the extent that every pixel will have the same RGB value with both image files. This is exactly what I want.

However, I'm dealing with hundreds of images and opening them in Gimp this way and then saving them is not an automated process. How can I automatically properly convert PNGs to the sRGB color profile prior to recompressing them?

Note: I've tried ImageMagick's convert with various sRGB.icc profiles, but every time I've tried it's a bit lossy (like giving #cccdcc instead of #cccccc) and thus ruins the ability to use PNG's greyscale storage for grey images, both of which are undesired.

share|improve this question

migrated from webmasters.stackexchange.com Feb 24 '13 at 16:56

This question came from our site for pro webmasters.

2 Answers 2

up vote 3 down vote accepted

I finally solved it.

I'd used the ICC profiles available on the ICC's website, which was strangely enough a mistake, since they introduce the errors I mentioned in the question.

GraphicsMagick (slightly lossy)

GraphicsMagick does this in a very slightly lossy method (brighter colors are reduced by one, like from #cccccc to #cbcbcb). However, this doesn't ruin greys and allows for better recompression.

Since this is likely the easier method to apply if strict perfection isn't needed, I've included it. Get a proper sRGB ICC profile, like the one in the assorted profiles pack offered by Little CMS or the one included with Ghostscript, and run the following:

gm convert in.png -profile /path/to/srgb.icc -strip out.png

Gimp automation (non-lossy)

After I found that GraphicsMagick was inexact, I worked out a Script-Fu script which uses Gimp's exact method. To prepare to use it, save the following contents to ~/.gimp-2.8/scripts/png-to-srgb.scm

(define (png-to-srgb pattern)
  (let* (
      (filelist (cadr (file-glob pattern 1)))
    )
    (while (not (null? filelist))
      (let* (
          (filename (car filelist))
          (image (car (file-png-load RUN-NONINTERACTIVE filename filename)))
        )
        (begin
          (plug-in-icc-profile-apply-rgb RUN-NONINTERACTIVE image 0 0)
          (file-png-save2 RUN-NONINTERACTIVE
              image (car (gimp-image-get-active-drawable image))
              filename filename
              0 9 0 0 0 0 0 0 0)
          (gimp-image-delete image)
          (set! filelist (cdr filelist))
        )
      )
    )
  )
)

Then the conversion can be accomplished with the following:

gimp-console -i -d -f -b '(png-to-srgb "file.png")' -b '(gimp-quit 0)'

This does not allow separate input and output filenames because it can be replaced with *.png if desired.

share|improve this answer

In your question, you write:

"Tools like PNGOUT blithely strip out color profiles (the iCCP chunk), which arguably makes the tools lossy because it changes the appearance of non-sRGB images in nearly all modern browsers."

While pre-converting the images to sRGB is indeed one solution to this problem, another is to tell PNGOUT to keep the color profile using the -k switch, either like this:

pngout -k1 image.png

or, more specifically:

pngout -kiCCP,gAMA image.png

(I'm assuming that if you want to keep the color profile, you probably also care about the gamma setting. Also, -k works on the Linux version of PNGOUT; if you're on Windows, replace it with /k.)

share|improve this answer
    
This is a good alternative, so I've upvoted it. One effect of it that got me when I tried it was that the color profiles were about a kilobyte each, so on the smaller files that are a few hundred bytes when the color profiles are removed, this method will increase their size considerably. –  Olathe Feb 25 '13 at 19:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.