Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 05:41
Show Gist options
  • Save aspose-com-gists/f5a0ac6f93bd0aec0c737546f8b900de to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f5a0ac6f93bd0aec0c737546f8b900de to your computer and use it in GitHub Desktop.
Crop or Rotate PSD Image File Programmatically using Java
String sourceFile = "sample.psd";
String destName = "Cropping-PSD_out.jpg";
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.load(sourceFile);
// Cache the image for better performance
if (!rasterImage.isCached())
{
rasterImage.cacheData();
}
// Create an instance of Rectangle class with desired size.
Rectangle rectangle = new Rectangle(20, 20, 20, 20);
// Perform the crop operation on object of Rectangle class
rasterImage.crop(rectangle);
// Save the results to disk
rasterImage.save(destName, new JpegOptions());
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.load("Test.psd");
// Before cropping, the image should be cached for better performance
if (!rasterImage.isCached())
{
rasterImage.cacheData();
}
// Define shift values for all four sides
int leftShift = 10;
int rightShift = 10;
int topShift = 10;
int bottomShift = 10;
// Based on the shift values, apply the cropping on image.
// Crop method will shift the image bounds toward the center of image and Save the results to disk
rasterImage.crop(leftShift, rightShift, topShift, bottomShift);
// Save output in JPEG,PSD or any other format.
rasterImage.save("output.jpg", new JpegOptions());
rasterImage.save("output.psd", new PsdOptions());
String sourceFile = "sample.psd";
String destName = "Rotate-PSD_out.jpg";
// Load input PSD image file
RasterImage image = (RasterImage)Image.load(sourceFile);
// Before rotation, the image should be cached for better performance
if (!image.isCached())
{
image.cacheData();
}
// Rotate the PSD image on 20 degree angle while keeping the image size proportional with red background color
image.rotate(20f, true, Color.getRed());
// Save the result to a new file
image.save(destName, new JpegOptions());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment