mikera / imagez

Image processing library for Clojure
301 stars 38 forks source link

writing jpg appears to invert colors #33

Open bburdette opened 8 years ago

bburdette commented 8 years ago

Playing around with imagez today; this one-liner unexpectedly inverts the jpg colors.

imagemeh.core=> (require '[mikera.image.core :as mic])
nil
imagemeh.core=> (mic/write (mic/load-image "download.jpg") "inverted.jpg" "jpg")
nil

source and result images attached:

download inverted

Writing to PNG produces a normal looking image.

mercul3s commented 7 years ago

I'm also experiencing this issue when resizing and writing jpgs. Any suggestions? This is a snippet from the code I'm having trouble with:

(defn image-resize [image width height]
    (let [loaded-image (core/load-image image)
          resized-image (core/resize loaded-image width height)
          new-image-name (format-image-name image width height)]
    (core/save resized-image new-image-name :quality 1.0 :progressive true)))
mikera commented 7 years ago

Might be related to this:

http://stackoverflow.com/questions/9340569/jpeg-image-with-wrong-colors

Don't really use JPEGs myself but happy to take a PR if anyone can work out what is going wrong...

mercul3s commented 7 years ago

I did some digging, and it looks like jpg colorspace is being converted from RGB to CMYK as the Java Image Writer lib adds an alpha channel. On save, it writes as CMYK because of the 4th channel. Could potentially be fixed by stripping that 4th channel before save:
https://github.com/rkalla/imgscalr/issues/82