Resize or compress Azure Blob images using Azure Functions

Ramesh Mukka 45 Reputation points
2024-01-11T09:58:18.01+00:00

I have a Power Automate flow which is adding images into the Azure blob. These images should finally be embedded into Excel reports. The size of the images added to the blob is too large to add to Excel later. So being new to pro code I have done some research to understand that Azure functions will be helpful here to resize and compress images. However, the code is not getting me the desired results. Here is the code I got. I am a noob and have never done pro code in my life. Please let me know what changes in the code are required to bring in the desired images. run.csx

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
public static void Run(Stream myBlob, string name,ILogger log, Stream outputBlob)
{
    log.LogInformation($"C# Blog trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    using (var image =Image.Load(myBlob))
    {
        image.Mutate(C => C.Resize(500,500));
        image.SaveAsPng(outputBlob);
    }
}

function.proj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
    </ItemGroup>
</Project>

Input Image (2000 x 2000 px) 018732568

Desired Output (500 x 500 px) 018732568 Resized

Actual output from Azure Function 018732568 (2)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,076 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,217 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,916 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,336 Reputation points
    2024-01-12T22:22:58.33+00:00

    Ramesh Mukka Thanks for posting your question in Microsoft Q&A. From the description, I assume you are trying to reduce size of the file using SixLabors.ImageSharp package using C# script in portal and follow code snippet similar in https://github.com/Azure-Samples/function-image-upload-resize/blob/master/ImageFunctions/Thumbnail.cs.

    I was able to test this function code with package version 1.0.3 and you are right that this is not as expected. Since this seems to be issue with SixLabors.ImageSharp package and its version or runtime (not actually Azure Function), please refer https://docs.sixlabors.com/articles/imagesharp/resize.html about this operation and if it doesn't help, please reach out to https://github.com/SixLabors/ImageSharp/issues repo for assistance (experts from the package team can help).

    In general, we don't recommend using C# script (only for convenient) and consider using In-process or Isolated model for production apps. I used the latest version of this package using in process model in Visual Studio and didn't observe the same behavior. Checkout the following:

    Function1.cs

    using System.IO;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.Formats.Png;
    using SixLabors.ImageSharp.Processing;
    using Image = SixLabors.ImageSharp.Image;
    
    namespace CSXSample
    {
        public class Function1
        {
            [FunctionName("Function1")]
            public void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebStorage")]Stream myBlob, string name, ILogger log, [Blob("outcontainer/{name}", FileAccess.Write)] Stream imageSmall)
            {
                using (var image = Image.Load(myBlob))
                {
                    image.Mutate(C => C.Resize(500, 500));
                    image.SaveAsPng(imageSmall, new PngEncoder());
                }
    
                log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            }
        }
    }
    

    CSXSample.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Azure.Storage.Blobs" Version="12.13.1" />
        <PackageReference Include="Azure.Storage.Files.Shares" Version="12.1.0" />
        <PackageReference Include="Azure.Storage.Queues" Version="12.11.1" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
        <PackageReference Include="Microsoft.Extensions.Azure" Version="1.5.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
        <PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    
    

    So, I suggest you validate the code in in-process or isolated model with the latest version and adjust resizing for your scenario. I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.