none
Jpeg to Tiff compressed file size is not in incremental order. RRS feed

  • Question

  • Hi

    I have 3 JPG files 150, 200, 300 DPI resolution with Encoder.Quality level is 10, 30, 50.
    The sizes of these 3 JPG files are in incremental oder (824 KB, 1.162 KB, 1.393 KB).

    I have tried to convert the above JPG files to TIFF using below code.

    BitmapFrame bitFrame = null;
    Stream original = File.Open("jpgFile", FileMode.Open, FileAccess.Read);                   
    bitFrame = BitmapFrame.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
    tiffEncoder.Compression = TiffCompressOption.Default;
    tiffEncoder.Frames.Add(bitFrame);
    FileStream FsStream = new FileStream(@"TiffFile,tif", FileMode.Create);
    tiffEncoder.Save(FsStream);

    My problem is that the "TiffFile,tif" size is not in incremental order.
    i.e 150Dpi.jpg file is converted to TIFF with 5.081 KB
    200Dpi.jpg file is converted to TIFF with 4.981 KB
    150Dpi.jpg file is converted to TIFF with 4.643 KB

    Eventhough the JPG file sizes are in incremental order (824 < 1.162 < 1.393) why the output TIFF file is not in incremental order.

    I want to achieve the output TIFF size like this 150Dpi.jpg to TIFF < 200Dpi.jpg to TIFF < 300Dpi.jpg to TIFF.

    Kindly help to resolve this issue.

    Regards

    Ashok



    Friday, November 23, 2018 8:51 AM

Answers

  • Your confusion is in trying to use real-world units.  You cannot think in terms of centimeters when you are working with bitmaps.  That term has no meaning.

    As I said, "SetResolution" does not change the image pixels in any way.  All that does is change one field in the header that is used by tools that might want to print the image.  The actual contents of the image are ALWAYS measured in pixels.

    You say "image width=127.01 cm and height = 166.69 cm", but that doesn't mean anything to the computer.  It doesn't measure images and bitmaps in any real-world measurement.  All it knows is how many pixels there are.  It neither knows nor cares how large each pixel is.  It turns out that your image must have been created with a resolution value of 96 DPI.  Once we know that, we can figure out that your image is 4800 pixels x 6300 pixels.  THAT size never changes.  The pixels are always the same.  This is why camera manufacturers always talk about megapixels, and not about inches.

        127.01 cm  /  2.54 cm/in = 50 in x 96 DPI = 4800 pixels
        166.69 cm  /  2.54 cm/in = 65.625 in x 96 DPI = 6300 pixels

    When you change your resolution to 200 DPI, you still have 4800 x 6300 pixels, but now you're telling other applications that those pixels are physically smaller.  It doesn't change how the image is stored in memory, or how it looks on screen, and it will take the same number of bits to store, but with that DPI assumption, the physical size would change:

        4800 pixels / 200 DPI = 20 inches x 2.54 cm/in = 60.96 cm
        6300 pixels / 200 DPI = 31.5 inches x 2.54 cm/in = 80.02 cm

    - - - -

    So, let me address your actual question.  Why do you want to change the resolution?  What do you expect that to accomplish?  You CAN do it, but you have to scale the image to increase the number of pixels  For 200 DPI:

        127.01 cm / 2.54 cm/in = 50 in x 200 DPI = 10,000 pixels
        166.69 cm / 2.54 cm/in = 65.625 in x 200 DPI = 13,125 pixels

    So, instead of reading into a BitmapFrame, you would have to read it into a Bitmap, then rescale that to 10,000x13,125 (which you can do with "new Bitmap"), then encode that new bitmap as your new image.

    However, there is no way to add information to that original image.  The only way to take 4,800 pixels and turn them into 10,000 pixels is to interpolate the pixels in between.  That's going to make your large image look fuzzy.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, November 28, 2018 9:57 PM

All replies

  • Hi ashok.kumar1974,

    Is 1.162 KB 1162 KB?  5.081 KB is 5081 KB?

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 26, 2018 9:47 AM
    Moderator
  • Hi Zhanglong,

    Yes, your are correct. It is 1162 KB & 5081 KB. Kindly help us to resolve this issue.

    Regards,

    Ashok

    Tuesday, November 27, 2018 4:27 AM
  • Why on earth are you worried about this?

    The DPI value has zero impact on the pixels themselves.  It's just a note telling users of the image how the image is intended to be displayed.  I'm assuming that your three JPG files all have exactly the same number of pixels, width and height.  The JPG files differ in size because of the compression quality, not because of the DPI.  When you use a lower quality in JPG, you are actually losing information.  The resulting pixels are different.  TIFF files do not have an adjustable "compression quality", because TIFF uses lossless compression.  It's always 100% quality.  Thus, if all three of your JPG images have the same width and height, as I suspect, then TIFF is storing basically the same information in all three cases.  The pixel values will vary because of the JPG compression losses, and that causes the TIFF lossless compression to produce slightly different results.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, November 27, 2018 7:00 PM
  • Hi Tim,

    Thank you for your reply.

    I have another doubt. Why bitmap.SetResolution API changes the aspect ratio of the image i.e. width and height of the image is changed when resolution is changed.

    For ex:

    Originally I have an image with width=127.01 cm and height=166.69 cm.

    When I change the resolution using

    bitmap.SetResolution(200,200); it is changed to 60.97 x 80.02 cm.

    or

    bitmap.SetResolution(300,300); it is changed to 40.65 x 53.35 cm.

    Why?

    I want to keep the original image width and height eventhough if I change the resolution using bitmap.SetResolution() API. Please help.

    Regards,

    Ashok

    Wednesday, November 28, 2018 10:22 AM
  • Your confusion is in trying to use real-world units.  You cannot think in terms of centimeters when you are working with bitmaps.  That term has no meaning.

    As I said, "SetResolution" does not change the image pixels in any way.  All that does is change one field in the header that is used by tools that might want to print the image.  The actual contents of the image are ALWAYS measured in pixels.

    You say "image width=127.01 cm and height = 166.69 cm", but that doesn't mean anything to the computer.  It doesn't measure images and bitmaps in any real-world measurement.  All it knows is how many pixels there are.  It neither knows nor cares how large each pixel is.  It turns out that your image must have been created with a resolution value of 96 DPI.  Once we know that, we can figure out that your image is 4800 pixels x 6300 pixels.  THAT size never changes.  The pixels are always the same.  This is why camera manufacturers always talk about megapixels, and not about inches.

        127.01 cm  /  2.54 cm/in = 50 in x 96 DPI = 4800 pixels
        166.69 cm  /  2.54 cm/in = 65.625 in x 96 DPI = 6300 pixels

    When you change your resolution to 200 DPI, you still have 4800 x 6300 pixels, but now you're telling other applications that those pixels are physically smaller.  It doesn't change how the image is stored in memory, or how it looks on screen, and it will take the same number of bits to store, but with that DPI assumption, the physical size would change:

        4800 pixels / 200 DPI = 20 inches x 2.54 cm/in = 60.96 cm
        6300 pixels / 200 DPI = 31.5 inches x 2.54 cm/in = 80.02 cm

    - - - -

    So, let me address your actual question.  Why do you want to change the resolution?  What do you expect that to accomplish?  You CAN do it, but you have to scale the image to increase the number of pixels  For 200 DPI:

        127.01 cm / 2.54 cm/in = 50 in x 200 DPI = 10,000 pixels
        166.69 cm / 2.54 cm/in = 65.625 in x 200 DPI = 13,125 pixels

    So, instead of reading into a BitmapFrame, you would have to read it into a Bitmap, then rescale that to 10,000x13,125 (which you can do with "new Bitmap"), then encode that new bitmap as your new image.

    However, there is no way to add information to that original image.  The only way to take 4,800 pixels and turn them into 10,000 pixels is to interpolate the pixels in between.  That's going to make your large image look fuzzy.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, November 28, 2018 9:57 PM
  • Hi Tim,

    I have understood your point. This clears all my doubts and able to resolve my problem.

    Thank you for your detailed explanation.

    Regards,

    Ashok

    Thursday, November 29, 2018 9:58 AM