Update text layers in PSD and create or export WebP to other image formats

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Imaging for Java logo We are pleased to announce the release of Aspose.Imaging for Java 3.3.0. This release allows you to Create WebP image, export WebP image to other image formats and convert frame of a GIF image to WebP image. Support to update text layer in the PSD file and read / write XMP data to image has also been incorporated in this release.

Create WebP Image

Using Aspose.Imaging for Java API, you can create WebP image. Following is the code demonstrating the use of WebPOptions class to create a WebP image.


// Create an instance of WebPOptions class
com.aspose.imaging.imageoptions.WebPOptions imageOptions = new com.aspose.imaging.imageoptions.WebPOptions();

// Set LossLess property
imageOptions.setLossless(true);

// Set Source property with the path and image file name where you want to create the WebP image.
imageOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("Test_create.webp", false));

// Create an instance of image class by using WebOptions instance that you have just created.
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(imageOptions, 500, 500);
        
// Save the image.
image.save();

Exporting WebP to Other Image Formats

Using Aspose.Imaging for Java API, you can convert WebP image to other image formats. Here is the code to convert WebP image to BMP format.


// Load WebP image into the instance of image class.
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load("D:\\save.webp");
        
// Save the image in WebP format.
image.save("D:\\save.bmp", new com.aspose.imaging.imageoptions.BmpOptions());

Convert GIFF Image Frame to WebP Image

Using Aspose.Imaging for Java API, you can extract a frame from any existing GIFF image and convert it to WebP format. Following is the code demonstration of GifImage.Blocks property to extract a GIFF frame and WebPFrameBlock class to add WebP frame to WebP image block list to create a WebP image.


// Load GIFF image into the instance of image class.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("input\\animation.gif");

// Create an instance of GIFF image class.
com.aspose.imaging.fileformats.gif.GifImage gif = (com.aspose.imaging.fileformats.gif.GifImage)image;

if (gif == null) return;

// Load an existing WebP image into the instance of WebPImage class.
com.aspose.imaging.fileformats.webp.WebPImage webp = new com.aspose.imaging.fileformats.webp.WebPImage(image.getWidth(), image.getHeight(), null);

try
{
            // Loop through the GIFF frames
            for (int i = 0; i < gif.getBlocks().length; i++)
            {

                if (!(gif.getBlocks()[i] instanceof com.aspose.imaging.fileformats.gif.blocks.GifFrameBlock))
                {
                  continue;
                }

                // Convert GIFF block to GIFF Frame
                com.aspose.imaging.fileformats.gif.blocks.GifFrameBlock gifBlock = (com.aspose.imaging.fileformats.gif.blocks.GifFrameBlock)gif.getBlocks()[i];

                // Create an instance of WebP Frame instance by passing GIFF frame to class constructor.
                com.aspose.imaging.fileformats.webp.WebPFrameBlock block = new com.aspose.imaging.fileformats.webp.WebPFrameBlock(gifBlock);
                block.setTop((short) gifBlock.getTop());
                block.setLeft((short) gifBlock.getLeft());
                block.setDuration((short) gifBlock.getControlBlock().getDelayTime());

                // Add WebP frame to WebP image block list
                webp.addBlock(block);
            }

            // Set Properties of WebP image.
            webp.getOptions().setAnimBackgroundColor(0xff); //black
            webp.getOptions().setAnimLoopCount(0); //infinity
            webp.getOptions().setQuality(50);
            webp.getOptions().setLossless(false);

            // Save WebP image.
            webp.save("output\\saveAnimation.webp");
}
finally
{
    webp.dispose();
}

Update Text Layer in PSD File

Aspose.Imaging provides functionality to update text layer in the PSD file. TextLayer.UpdateText method of the TextLayer class can be used to update text in the text layer of a PSD file. TextLayer class can be found under package com.apose.imaging.fileformats.psd.Layers. Method TextLayer.UpdateText takes a string that is to be added, top left coordinates as starting point (com.aspose.imaging.Point), font size and color (com.aspose.imaging.Color) as parameters. Following is the code demonstration of the said functionality.


String sourceFileName = "input\\fewTextLayers.psd";
String outputFile = "output\\result.psd";

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(sourceFileName);
try
{
    com.aspose.imaging.fileformats.psd.PsdImage psdImage = (com.aspose.imaging.fileformats.psd.PsdImage)image;
    com.aspose.imaging.fileformats.psd.layers.TextLayer textLayer1 = (com.aspose.imaging.fileformats.psd.layers.TextLayer)psdImage.getLayers()[1];
    com.aspose.imaging.fileformats.psd.layers.TextLayer textLayer2 = (com.aspose.imaging.fileformats.psd.layers.TextLayer)psdImage.getLayers()[2];

    textLayer2.updateText("test update", new com.aspose.imaging.Point(100, 100), 72.0f, com.aspose.imaging.Color.getPurple());

    com.aspose.imaging.imageoptions.PsdOptions psdOpt = new com.aspose.imaging.imageoptions.PsdOptions();
    psdOpt.setCompressionMethod(com.aspose.imaging.fileformats.psd.CompressionMethod.RLE);
     psdImage.save(outputFile);
}
finally
{
    image.dispose();
}

Read and Write XMP Data to Images

Using Aspose.Imaging for Java API, you can read or write XMP metadata to images. xmp package can be used for XMP data manipulation. Following is the complete code demonstrating how to use the XmpHeaderPi, XmpTrailerPi, XmpMeta, XmpPacketWrapper, PhotoshopPackage and DublinCorePackage packages contained in xmp to read and write XMP metadata into the image.



        // Specify the size of image by defining a Rectangle 
        com.aspose.imaging.Rectangle rect = new com.aspose.imaging.Rectangle(0, 0, 100, 200);

        TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRGB);
        tiffOptions.setPhotometric(TiffPhotometrics.MinIsBlack);
        tiffOptions.setBitsPerSample(new int[] { 8 });

        // create the brand new image just for sample purposes
        com.aspose.imaging.fileformats.tiff.TiffImage image 
                = new com.aspose.imaging.fileformats.tiff.TiffImage(
                        new com.aspose.imaging.fileformats.tiff.TiffFrame(tiffOptions, rect.getWidth(), rect.getHeight()));
        
        // create an instance of XMP-Header
        com.aspose.imaging.xmp.XmpHeaderPi xmpHeader = new com.aspose.imaging.xmp.XmpHeaderPi();
        
        // create an instance of Xmp-TrailerPi
        com.aspose.imaging.xmp.XmpTrailerPi xmpTrailer = new com.aspose.imaging.xmp.XmpTrailerPi(true);

        // create an instance of XMPmeta class to set different attributes
        com.aspose.imaging.xmp.XmpMeta xmpMeta = new com.aspose.imaging.xmp.XmpMeta();
        xmpMeta.addAttribute("Author", "Mr Smith");
        xmpMeta.addAttribute("Description", "The fake metadata value");

        // create an instance of XmpPacketWrapper that contains all metadata
        com.aspose.imaging.xmp.XmpPacketWrapper xmpData = new com.aspose.imaging.xmp.XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta);

        // create an instacne of Photoshop package and set photoshop attributes
        com.aspose.imaging.xmp.schemas.photoshop.PhotoshopPackage photoshopPackage = new com.aspose.imaging.xmp.schemas.photoshop.PhotoshopPackage();
        photoshopPackage.setCity("London");
        photoshopPackage.setCountry("England");
        photoshopPackage.setColorMode(com.aspose.imaging.xmp.schemas.photoshop.ColorMode.Rgb);
        photoshopPackage.setCreatedDate(new java.util.Date());

        // add photoshop package into XMP metadata
        xmpData.addPackage(photoshopPackage);

        // create an instacne of DublinCore package and set dublinCore attributes
        com.aspose.imaging.xmp.schemas.dublincore.DublinCorePackage dublinCorePackage = new com.aspose.imaging.xmp.schemas.dublincore.DublinCorePackage();
        dublinCorePackage.setAuthor("Charles Bukowski");
        dublinCorePackage.setTitle("Confessions of a Man Insane Enough to Live With the Beasts");
        dublinCorePackage.addValue("dc:movie", "Barfly");

        // add dublinCore Package into XMP metadata
        xmpData.addPackage(dublinCorePackage);

        com.aspose.imaging.system.io.MemoryStream ms = new com.aspose.imaging.system.io.MemoryStream();
        
        // update XMP metadata into image
        image.setXmpData(xmpData);
        
        // Save image on the disk or in memory stream
        image.save(ms.toOutputStream());

        ms.seek(0, com.aspose.imaging.SeekOrigin.Begin);

        // Load the image from moemory stream or from disk to read/get the metadata
        com.aspose.imaging.fileformats.tiff.TiffImage img 
                = (com.aspose.imaging.fileformats.tiff.TiffImage)com.aspose.imaging.Image.load(ms.toInputStream());
        
        // getting the XMP metadata
        com.aspose.imaging.xmp.XmpPacketWrapper imgXmpData = img.getXmpData();
        for (int i=0; i < imgXmpData.getPackagesCount();i++)
        {
          // use package data ...
        }

Enhancements

Following enhancements have been introduced in this release.

  • Process of JPEG2000 image conversion JP2 has been improved.

  • Process of TIFF image conversion to other image format has been improved.

  • Functionality of EXIF data in TIFF image has been improved.

  • DWG file to PDF conversion process has been improved.

  • Process of converting raster images has been improved.

  • JPEG image encoding algorithm has been upgraded.

  • Functionality of getHorizontalResolution & getVerticalResolution properties has been improved.

  • Resizing GIF image functionality has been improved.

Please refer to the release notes of Aspose.Imaging for Java 3.3.0 for a full view of improvements along with sample code snippets for newly added features. If you are planning to upgrade the API to the latest revision, we strongly suggest you to check the Public API Changes section to know what has been changed in the public API since your current version.

Aspose.Imaging for Java Resources

The resources, you may need to accomplish your tasks:

As always we appreciate your feedback so if you ever have anything to tell us about this release or anything else, please head to the Aspose.Imaging forum for a chat.