Using Adjustment Layer for PSD Enhancements
Overview
In this article, we will explore the editing of adjustment layers in Aspose.PSD for Python. Adjustment layers are a powerful feature in image editing that allow you to make non-destructive changes to your images. Aspose.PSD provides a comprehensive set of adjustment layer classes that you can use to modify various aspects of your images.
To demonstrate the editing of adjustment layers, we will use a sample code snippet at the end of page that loads a PSD image and applies different adjustments to its layers.
In the below code snippet, we start by loading the PSD image using the PsdImage.load() method. We then set up the options for saving the output PNG files. The PngOptions class allows us to specify the color type for the output image.
Next, we iterate through each layer in the PSD image and check its type using the is_assignable() method. If the layer is of a specific type, we cast it to that type using the cast() method and apply the desired adjustment. For example, we adjust the brightness and contrast of a BrightnessContrastLayer, modify the levels of a LevelsLayer, add a curve point to a CurvesLayer, and so on.
You can add additional code to apply other adjustments to their respective layers. Aspose.PSD provides a wide range of adjustment layer classes, such as ExposureLayer, HueSaturationLayer, ColorBalanceAdjustmentLayer, BlackWhiteAdjustmentLayer, PhotoFilterLayer, ChannelMixerLayer, InvertAdjustmentLayer, PosterizeLayer, ThresholdLayer, SelectiveColorLayer, and more.
Finally, we save the modified image using the save() method of the PsdImage class.
This code provides a basic overview of how to edit adjustment layers in Aspose.PSD for Python. You can customize the adjustments according to your requirements and explore the various options available in the API documentation.
Please check full example.
Example
from aspose.psd import Color | |
from aspose.psd.fileformats.png import PngColorType | |
from aspose.psd.fileformats.psd import PsdImage | |
from aspose.psd.fileformats.psd.layers.adjustmentlayers import BrightnessContrastLayer, LevelsLayer, CurvesLayer, \ | |
ExposureLayer, HueSaturationLayer, ColorBalanceAdjustmentLayer, BlackWhiteAdjustmentLayer, PhotoFilterLayer, \ | |
ChannelMixerLayer, RgbMixerChannel, InvertAdjustmentLayer, PosterizeLayer, ThresholdLayer, SelectiveColorLayer, \ | |
CmykCorrection, SelectiveColorsTypes | |
from aspose.psd.fileformats.psd.layers.layerresources import CurvesContinuousManager | |
from aspose.psd.imageoptions import PngOptions | |
from aspose.pycore import cast, is_assignable | |
def AdjustmentLayerEnhancementTest(): | |
sourcePsd = "AllAdjustments.psd" | |
outputOrigPng = "AllAdjustments_orig.png" | |
outputModPng = "AllAdjustments_mod.png" | |
pngOpt = PngOptions() | |
pngOpt.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
with PsdImage.load(sourcePsd) as image: | |
psdImage = cast(PsdImage, image) | |
psdImage.save(outputOrigPng, pngOpt) | |
layers = psdImage.layers | |
for layer in layers: | |
if is_assignable(layer, BrightnessContrastLayer): | |
br = cast(BrightnessContrastLayer, layer) | |
br.brightness = -br.brightness | |
br.contrast = -br.contrast | |
if is_assignable(layer, LevelsLayer): | |
levels = cast(LevelsLayer, layer) | |
levels.master_channel.output_shadow_level = 30 | |
levels.master_channel.input_shadow_level = 5 | |
levels.master_channel.input_midtone_level = 2 | |
levels.master_channel.output_highlight_level = 213 | |
levels.master_channel.input_highlight_level = 120 | |
if is_assignable(layer, CurvesLayer): | |
curves = cast(CurvesLayer, layer) | |
manager = curves.get_curves_manager() | |
curveManager = cast(CurvesContinuousManager, manager) | |
curveManager.add_curve_point(2, 150, 180) | |
if is_assignable(layer, ExposureLayer): | |
exp = cast(ExposureLayer, layer) | |
exp.exposure = exp.exposure + 0.1 | |
if is_assignable(layer, HueSaturationLayer): | |
hue = cast(HueSaturationLayer, layer) | |
hue.hue = -15 | |
hue.saturation = 30 | |
if is_assignable(layer, ColorBalanceAdjustmentLayer): | |
colorBal = cast(ColorBalanceAdjustmentLayer, layer) | |
colorBal.midtones_cyan_red_balance = 30 | |
if is_assignable(layer, BlackWhiteAdjustmentLayer): | |
bw = cast(BlackWhiteAdjustmentLayer, layer) | |
bw.reds = 30 | |
bw.greens = 25 | |
bw.blues = 40 | |
if is_assignable(layer, PhotoFilterLayer): | |
photoFilter = cast(PhotoFilterLayer, layer) | |
photoFilter.color = Color.azure | |
if is_assignable(layer, ChannelMixerLayer): | |
channelMixer = cast(ChannelMixerLayer, layer) | |
channel = channelMixer.get_channel_by_index(0) | |
if is_assignable(channel, RgbMixerChannel): | |
rgbChannel = cast(RgbMixerChannel, channel) | |
rgbChannel.green = 120 | |
rgbChannel.red = 50 | |
rgbChannel.blue = 70 | |
rgbChannel.constant += 10 | |
if is_assignable(layer, InvertAdjustmentLayer): | |
# Here is nothing to do ¯\_(ツ)_/¯ If this layer is presented than all colors will be inverted under it. | |
# Also, you can add it to the new image | |
# img = PsdImage(100, 100) | |
# img.AddInvertAdjustmentLayer() | |
pass | |
if is_assignable(layer, PosterizeLayer): | |
post = cast(PosterizeLayer, layer) | |
post.levels = 3 | |
if is_assignable(layer, ThresholdLayer): | |
threshold = cast(ThresholdLayer, layer) | |
threshold.level = 15 | |
if is_assignable(layer, SelectiveColorLayer): | |
selectiveColor = cast(SelectiveColorLayer, layer) | |
correction = CmykCorrection() | |
correction.cyan = 25 | |
correction.magenta = 10 | |
correction.yellow = -15 | |
correction.black = 5 | |
selectiveColor.set_cmyk_correction(SelectiveColorsTypes.CYANS, correction) | |
psdImage.save(outputModPng, pngOpt) |