dcsimg
Results 1 to 12 of 12

Thread: Resizing picture files

  1. #1

    Thread Starter
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    373

    Resizing picture files

    I need to resize pictures of any format to get them below 4Mb. This is even a bigger problem today with new cameras taking 10 to 30 MB pictures.
    I have studied this thread, but think there might be better solutions nine years later.
    Thanks
    PK


    http://www.vbforums.com/showthread.p...cing-dimension

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,643

    Re: Resizing picture files

    Photos are difficult to compress without a lot of loss, making JPEG still one of the best options if you want broad support. Scaling the image dimensions down first will also help a lot if that's acceptable. Doing much else gets you into exotic territory.

    With XP dead now things have vastly improved. From Vista onward WIA 2.0 comes as a preinstalled, serviced, and protected part of the OS.

    What are you starting with? Large JPEG files? What can you afford to lose? Dimensions? Quality?

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,643

    Re: Resizing picture files

    Since few people seem to have downloaded the WIA 2.0 SDK with the documentation here's an example of what I was saying:

    Code:
    Option Explicit
    
    Private ImageProcess As WIA.ImageProcess
    Private ImageFile As WIA.ImageFile
    
    Private Sub Main()
        'Create and configure an ImageProcess and an ImageFile:
        Set ImageProcess = New WIA.ImageProcess
        With ImageProcess
            .Filters.Add .FilterInfos.Item("Scale").FilterID
            With .Filters(1)
                .Properties("MaximumWidth").Value = 1024
                .Properties("MaximumHeight").Value = 1024
                .Properties("PreserveAspectRatio").Value = True
            End With
            .Filters.Add .FilterInfos.Item("Convert").FilterID
            With .Filters(2)
                .Properties("FormatID").Value = wiaFormatJPEG
                .Properties("Quality").Value = 50 '1 to 100 percent, higher is better.
            End With
        End With
        Set ImageFile = New WIA.ImageFile
    
        'Now use them one or more times:
        ImageFile.LoadFile "P1000010.JPG"
        Set ImageFile = ImageProcess.Apply(ImageFile)
        On Error Resume Next
        Kill "S1000010.JPG"
        On Error GoTo 0
        ImageFile.SaveFile "S1000010.JPG"
    End Sub
    Or if you enjoy pain you can always use the GDI+ Flat API yourself.


    I found the WIA 2.0 documentation on a Microsoft web site, just not a downloadable CHM as we had in the past. It's now marked "old content" but they seem to be gradually doing that to everything we use:

    Windows Image Acquisition Automation Layer

    At least the documentation exists though.
    Last edited by dilettante; Aug 9th, 2019 at 06:09 PM.

  4. #4

    Thread Starter
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    373

    Re: Resizing picture files

    Dilettante,
    I worked successfully with WIA after you introduced me to it in 2017 under the Scanning thread and I will accept this for my purpose now.
    I do not mind and would prefer resizing from my normal 8k to 1920x1024

    PK

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,405

    Re: Resizing picture files

    FWIW, here is the RC5-code for the same task:

    Code:
    Private Sub ResizeToHDRes(SrcFile As String, DstFile As String, Optional ByVal Qual& = 80)
        Cairo.ImageList.AddImage("", SrcFile, 1920, 1080).WriteContentToJpgFile DstFile, Qual
    End Sub
    Just tested this with an 8K-JPG (7680x4320) - writing it to a scaled-down HDRes-jpg (1920x1080) -
    and the whole operation was about 4 times as fast as with WIA (75msec vs. 300msec).

    HTH

    Olaf

  6. #6

    Thread Starter
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    373

    Re: Resizing picture files

    Thank you so much Olaf!

  7. #7
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,581

    Re: Resizing picture files

    https://docs.microsoft.com/bg-bg/win...es-howto-scale

    I think a VB6 typelib for WIC can be short and sweet, and implementing an image resizer will (probably) beat Cairo by a large margin w/ those GPU accelerated operations on Win10.

    cheers,
    </wqw>

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,643

    Re: Resizing picture files

    I'd avoid resizing to a specific size unless you manually take the aspect ratio of your originals into account. Of course if they are all the same then that's less of an issue. But now a lot of cameras offer several aspect ratios and autocorrect for 90 degree camera rotation to get portrait images.

    You might be better off providing max x/y and aspect ratio preservation as my example above does.

  9. #9
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,405

    Re: Resizing picture files

    Quote Originally Posted by dilettante View Post
    I'd avoid resizing to a specific size unless you manually take the aspect ratio of your originals into account.
    Well, to cover that, the routine I've posted could be extended this way (without any speed-loss):
    Code:
    Sub ResizeToHDRes(SrcFile$, DstFile$, Optional ByVal SzMax& = 1920, Optional ByVal Qual& = 80)
      Dim B() As Byte: B = New_c.FSO.ReadByteContent(SrcFile)
      Dim W&, H&: New_c.Jpg.GetJPGDimensions VarPtr(B(0)), UBound(B) + 1, W, H
    
      Cairo.CalcAspectFit W / H, SzMax, SzMax, 0, 0, W, H
      Cairo.ImageList.AddImage("", B, W, H).WriteContentToJpgFile DstFile, Qual
    End Sub
    @wqweto
    A faster (GPU-based) resizing would not increase the speed very much in the given use-case,
    because the resizing-part will only account for about 10-15% or so of the over-all-timing, which is:
    - JPG-decoding
    - resizing
    - JPG-encoding

    In other words, it's the JPG-lib that's used underneath, which is accountable for the most part(s) of the total time needed.

    And when using RC5, the SSE-boosted libjpg-turbo is engaged in that case (statically contained in vb_cairo_sqlite.dll)...

    Furthermore - libjpg-turbo also supports "Power-Of-Two-downscale-factors" already in the JPG-decoding-stage - and the above call:
    Cairo.ImageList.AddImage makes use of that feature underneath, finding and applying the best matching JPG-downscale-factor
    automatically in a first (intermediate) stage, before a final resize is performed from that intermediate jpg-decoding-result -
    to match-up with the given DesiredWidth and DesiredHeight-Params of the returned cairo-surface.

    So, unless WIC supports GPU-accelerated JPG-decoding/encoding (which might be available via DirectX/DirectShow over a Media-Codec-setting),
    it would not be of much use to increase the overall-speed in this scenario.

    HTH

    Olaf

  10. #10

    Thread Starter
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    373

    Re: Resizing picture files

    Thank you all,
    It was implicit in my question that I do not wish to change the aspect ratio, but only the file size to as small as practical still maintaining a high quality.
    These are mainly pictures taken on a building construction site verifying the integrity of the building.
    It must be general purpose to allow for higher qualities if needed.
    When I send pictures via email I am given a choice of four sizes I can resize to.

    Pk

  11. #11

    Thread Starter
    Hyperactive Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    373

    Re: Resizing picture files

    Wqweto,
    I am not good at C++ and wish to try your method as well. Can you maybe help me translate it into vb6.
    Thanks

    PK

  12. #12
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,581

    Re: Resizing picture files

    Quote Originally Posted by Schmidt View Post
    So, unless WIC supports GPU-accelerated JPG-decoding/encoding (which might be available via DirectX/DirectShow over a Media-Codec-setting),
    it would not be of much use to increase the overall-speed in this scenario.
    Yes, JPEG decoding is supported in GPU although I'm not quite sure it's that faster than libjpeg-turbo SIMD optimized impl.

    https://blogs.msdn.microsoft.com/ie/...t-explorer-11/

    IMO 75ms for 8K-image-decoding + resampling + HD-encoding is very close to GPU speeds.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width