NEF to PNG (PNG 6x larger file size)

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?".
syntax
Posts: 1
Joined: 2012-01-17T00:20:40-07:00
Authentication code: 8675308

NEF to PNG (PNG 6x larger file size)

Post by syntax »

While converting Nikon RAW format .NEF files (taken with D40) to PNG with ImageMagick, I get PNG files that are roughly 6 times larger than the original "raw" images. That doesn't make any sense to me. The camera lists the mode as .NEF (RAW) and does not specify lossy/lossless or compressed/uncompressed. Theoretically lossless compression (PNG) should be smaller than RAW format images, unless I'm missing something.

Code: Select all

> convert DSC_0001.NEF DSC_0001.png
> ls -lah
-rw-r--r-- 1  5.4M 2011-12-03 17:21 DSC_0001.NEF
-rw-rw-r-- 1  29M 2012-01-17 00:29 DSC_0001.png
I've also tried using ufraw-batch to convert .NEF to .PPM. I then convert PPM to PNG and get more reasonable file sizes. However, they are still 1-2 MB larger than the original raw .NEF files. Does anyone have any insight into why this is occurring?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: NEF to PNG (PNG 6x larger file size)

Post by glennrp »

syntax wrote:While converting Nikon RAW format .NEF files (taken with D40) to PNG with ImageMagick, I get PNG files that are roughly 6 times larger than the original "raw" images. That doesn't make any sense to me. The camera lists the mode as .NEF (RAW) and does not specify lossy/lossless or compressed/uncompressed. Theoretically lossless compression (PNG) should be smaller than RAW format images, unless I'm missing something. ... Does anyone have any insight into why this is occurring?
Wikipedia (see http://en.wikipedia.org/wiki/Raw_image_format) says "When converting from the four sensor 2x2 Bayer matrix raw form into single RGB pixels the original 4x12 (or 14) bit data is converted to 4x3x8 bit"

That doubles it. If you happen to write 16-bit PNG files, that doubles it again. You could try using the "-depth 8" option to avoid the second doubling (which is the same as what happens when you convert to the intermediate PPM format).
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

I'm also having the same problem. That explains a few things. But I'm unsure about the remaining 2x increase is size. I converted a 11MB NEF to PNG and to my surprise the size became approx. 60MB.. What is causing this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: NEF to PNG (PNG 6x larger file size)

Post by fmw42 »

If you are on Q16 IM, then try adding -depth 8
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: NEF to PNG (PNG 6x larger file size)

Post by snibgo »

PNG doesn't give good compression for photographs. It's actually quite poor for photos.

Another possible factor: NEF files can be compressed or not, and the compression can be lossy or not. A lossy NEF file will certainly be smaller than a PNG made from it.

I use 16-bit tiff files with zip compression, and these are generally about 4x the size of the NEF files. I did a load of tests to determine the best lossless compression, and found that zip tiff was the best.
snibgo's IM pages: im.snibgo.com
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

So now it became 16.2 MB from 11 MB. It is still more than the Raw file. :( :(
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

How can I know whether a NEF is lossy or not? And I wanted a loseless compression from NEF and thats the reason I converted it into PNG. Yep I understood with whatever that you were trying to tell. PNG will be more if you choose a 16 bit depth from a 8 bit NEF, but that should only double the size as far as I understood. What it is causing to go up 6 times than NEF?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: NEF to PNG (PNG 6x larger file size)

Post by snibgo »

Exiftool or Nikon's ViewNX 2 can tell you if the NEF is lossy or lossless.

Code: Select all

D:\pictures\20130713>exiftool -Nikon:NEFCompression aga_1434.nef
NEF Compression                 : Lossless
PNG compression wasn't designed for photos but for graphics. NEF compression was designed for photos.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: NEF to PNG (PNG 6x larger file size)

Post by fmw42 »

You could try to convert to tiff with compression. It supports a number of compression modes.
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

I got your point. But I'm not trying to apply compression techniques on images. I'm just curious to know why the size is increasing here. As you told earlier NEF is loseless and PNG is loseless too. And as per expectation the Raw image should have more details than any image formats(PNG,GIF,JEPG,whatever) can possibly have. But why PNG files are more larger than NEF?
The project which I'm doing needs to prove that PNG, JPEG, WEBP are smaller than Raw image. But I'm stuck here on not sure what to submit in report!! :( :? :? :?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: NEF to PNG (PNG 6x larger file size)

Post by fmw42 »

PNG and NEF may be lossless, they probably use different lossless compression algorithms and likely the NEF compression is larger than the PNG compression, so PNG files are larger, since they are not compressed as much. Just my guess!

P.S. You can change the PNG compression amount -- see http://www.imagemagick.org/script/comma ... hp#quality. I know nothing about NEF.

see http://www.coolutils.com/Formats/NEF. Perhaps you have 12-14 bit data, but when converted to PNG it becomes 16-bits if on Q16 IM. Also possibly there are some meta data in the NEF that is compressed as well and not compressed in the PNG result.

Again these are all guesses on my part.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: NEF to PNG (PNG 6x larger file size)

Post by glennrp »

The D40 Nikon is a 6MP camera. Your NEF file is 5.4Mbytes, which is less than 8 bits per pixel so your "raw"
image is actually compressed. Converting a lossy image to lossless is a losing proposition. You are better off
converting to high-resolution JPEG instead.
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

what happens when a nef is converted to jpeg? Will Bayers filter affect that as well?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: NEF to PNG (PNG 6x larger file size)

Post by snibgo »

Convert to JPEG and you will lose data. (a) JPEG is only 8-bit so you lose nearly half the bits. (b) JPEG is lossy. For low quality, it is very lossy, but needs not much disk space. You can experiment with different "-quality" settings. Very low quality will be smaller than NEF.

I don't see the point of capturing a NEF file and simply converting it to JPEG. Why not tell the camera to save it as JPEG?
snibgo's IM pages: im.snibgo.com
nitingeetasagar
Posts: 6
Joined: 2013-12-06T10:29:19-07:00
Authentication code: 6789

Re: NEF to PNG (PNG 6x larger file size)

Post by nitingeetasagar »

I was just experimenting In conversion from RAW format to jpeg,png and webp. So now only png is greater among the three. I just want to find out the reasons!! :?

So as PNG will be affected by Bayers filters which causes the mapping of 2 RGBG to a single RGB. Why its not doing the same in JPEG and WEBP?
Post Reply