Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

Container.ReadImage

Reads and convert image.

Component Version macOS Windows Linux Server iOS SDK
Container 8.4 ✅ Yes ✅ Yes ❌ No ✅ Yes, on macOS and Windows ✅ Yes
MBS( "Container.ReadImage"; Container { ; ImageType; FileName } )   More

Parameters

Parameter Description Example Flags
Container The container field or value to inspect. MyTable::MyContainerField
ImageType The type of image to return.
Either JPEG, PNG, GIF or BMP. Default is JPEG.
"JPEG" Optional
FileName The filename to use for the picture.
Default is "image" with the extension matching the image type.
"test.jpg" Optional

Result

Returns image container or error.

Description

Reads and convert image.
This function is useful to read all the image formats supported by Apple's frameworks and convert them to a common format like JPEG or PNG.
Use this function to convert old PICT images in containers.
Works as well for HEIC and HEIF images.

Added Windows support in v13.0.

Reads webp files on macOS.

Examples

Convert PICT to PNG:

Set Field [ Test::output ; MBS("Container.ReadImage"; Test::OldImage; "PNG"; "image.png") ]

Read file from disk and read as image:

Set Field [ Batch Emailer::Inline Graphics ; MBS("Container.ReadImage"; MBS("Container.ReadFile"; "/Users/cs/Desktop/test.heic")) ]

Read HEIC file:

Set Variable [ $path ; Value: "/Users/cs/Desktop/IMG_3552.HEIC" ]
Set Variable [ $container ; Value: MBS( "Container.ReadFile"; $path) ]
If [ MBS("IsError") = 0 ]
    Set Variable [ $image ; Value: MBS( "Container.ReadImage"; $container; "PNG"; "image.png") ]
    If [ MBS("IsError") = 0 ]
        Set Field [ Untitled::image ; $image ]
    End If
End If

Convert a PSD or HEIF image to PNG:

Set Field [ Contacts::Output ; MBS( "Container.ReadImage"; Contacts::Input; "PNG"; "test.png" ) ]

Insert picture from device and then convert HEIF

# let user take a picture
Insert from Device [ Contacts::Photo Container ; Type: Camera ; Camera: Back; Resolution: Full ]
#
# check filename
Set Variable [ $name ; Value: GetAsText(Contacts::Photo Container) ]
Set Variable [ $extension ; Value: Right ( $name ; 5 ) ]
If [ $extension = ".heif" or $extension = ".heic" ]
    # we got a HEIF image
    #
    # get new file name
    Set Variable [ $name ; Value: Substitute($name; ".heif"; ".png") ]
    Set Variable [ $name ; Value: Substitute($name; ".heic"; ".png") ]
    #
    # now convert to PNG
    Set Variable [ $image ; Value: MBS( "Container.ReadImage"; Contacts::Photo Container; "PNG"; $name ) ]
    If [ MBS("IsError") = 0 ]
        # save on success to container
        Set Field [ Contacts::Photo Container ; $image ]
    Else
        Show Custom Dialog [ "Read Image Failed?" ; $image ]
    End If
End If

Read image and scale down as thumbnail:

# Try plugin to read more formats known by OS
Set Variable [ $imageData ; Value: MBS( "Container.ReadImage"; $input; "PNG"; "image.png") ]
If [ MBS("IsError") = 0 and not IsEmpty ( $imageData ) ]
    # scale down?
    Set Variable [ $image ; Value: MBS( "GMImage.NewFromContainer"; $imageData) ]
    If [ MBS("IsError") = 0 ]
        # limit size to 1000 pixel maximum
        Set Variable [ $scale ; Value: MBS("GMImage.Scale";$Image; "1000>") ]
        # remove metadata
        Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "IPTC"; "" ) ]
        Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "EXIF"; "" ) ]
        Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "XMP"; "" ) ]
        # save
        Set Variable [ $NewImage ; Value: MBS("GMImage.WriteToJPEGContainer"; $Image) ]
        If [ MBS("IsError") = 0 ]
            Set Field [ Get Preview::Preview ; $NewImage ]
            Set Field [ Get Preview::Made using ; "ReadImage" ]
            Exit Script [ Text Result: ]
        End If
        Set Variable [ $r ; Value: MBS("GMImage.Release";$Image) ]
    End If
    #
    Set Field [ Get Preview::Preview ; $imageData ]
    Exit Script [ Text Result: ]
End If

See also

Release notes

Example Databases

Blog Entries

This function is free to use.

Created 29th July 2018, last changed 15th December 2023


Container.ReadFile - Container.ReadImageFile