Group Abstract Group Abstract

Message Boards Message Boards

Convert .jp2 image to .png increase image size significantly

Due to an API I am using doesn't accept .jp2 format images, I will have to convert those images into .png.

I wrote a simple script to maintain the original data's directory:

JP2ToPNG[fileName_]:= Module[
    	{newFileName, filePath},
    	filePath = StringSplit[fileName, "/"] /."NcomSampleData"-> "PNG";
    	filePath[[-1]] = StringReplace[filePath[[-1]], ".jp2" -> ".png"];
    	newFileName = StringJoin[Riffle[filePath[[3;;]], "/"]];
    	filePath = StringJoin[Riffle[StringSplit[newFileName, "/"][[3;;-2]], "/"]];
    	If[
    		DirectoryQ[filePath], 
    		FileConvert[fileName -> newFileName],
    		CreateDirectory[filePath]; 
    		FileConvert[fileName -> newFileName]
    	]
    ]

But then I found that the new .png file are 2-4 times larger than the original ones. I am guessing the reason is the original data, every pixel is 1 Byte, 8 bit integer range from 0-255. But Mathematica read it as grayscale and save it as a float number 4 bytes from (0,1)?

Can anyone confirm with my guess? If I am guessing correctly, how to fix this problem?

Thanks!

Attachment

Attachments:
POSTED BY: Wenzhen Zhu
POSTED BY: Piotr Wendykier
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard