# Distorted image created with createImageDataFromBuffer function

**URL:** <https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270>\
**Category:** UXP Plugin API\
**Tags:** uxp\
**Created:** [December 22, 2023, 6:03pm UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270 "2023-12-22T18:03:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![akash\_panigrahi](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@akash\_panigrahi](https://forums.creativeclouddeveloper.com/u/akash_panigrahi)\
**Post date:** [December 22, 2023, 6:03pm UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270/1 "2023-12-22T18:03:24Z")

</div>

I have a simple buffer from a response that I want to show in a layer. But the image generated is distorted. Kindly help.

```auto
try {
    const { app, imaging, core, constants } = require("photoshop");
    const { storage } = require("uxp");

    await core.executeAsModal(async () => {
        const { data: transformedImageBuffer } = await axios(
            "https://cdn.pixelbin.io/v2/black-sound-ffec6c/original/__photoshop/pexels-19565250-iWTwyapIq.jpeg",
            { responseType: "arraybuffer" }
        );

        const transformedImageLayer = await app.activeDocument.createPixelLayer(
            { name: "River" }
        );

        const outputImageData = await imaging.createImageDataFromBuffer(
            new Uint8Array(transformedImageBuffer),
            {
                width: 432,
                height: 768,
                components: 4,
                colorSpace: "RGB",
                chunky: false,
                // colorProfile: "sRGB IEC61966-2.1",
            }
        );

        await imaging.putPixels({
            layerID: transformedImageLayer._id,
            imageData: outputImageData,
        });

        outputImageData.dispose();
    });
} catch (err) {
    console.error(err);
}

```

* * *

Distorted image

 ![distorted](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/a/a610324f6e02813f5fa16d45acfa4d5af9dae572.jpeg)

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [December 22, 2023, 6:23pm UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270/2 "2023-12-22T18:23:49Z")

</div>

You have to convert JPG into bitmap in first place. Now you are just putting in raw data.

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [December 22, 2023, 8:47pm UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270/3 "2023-12-22T20:47:19Z")

</div>

To @Jarda’s point it looks like you’re passing the raw data of a JPG file (including file header, etc.) to a function that expects a plain array of pixel data. That said, before you try interpreting the JPG file and grabbing/converting its pixel data I would suggest a different route: saving the JPG image into the data or plugin folder (or any other folder that you have access to) and using Photoshop to open it (as a temp document). Photoshop is pretty darn good at opening all sorts of data formats and dealing with their quirks 😉

---

<div class="post-metadata">

**Author:** ![akash\_panigrahi](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@akash\_panigrahi](https://forums.creativeclouddeveloper.com/u/akash_panigrahi)\
**Post date:** [December 23, 2023, 6:08am UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270/4 "2023-12-23T06:08:25Z")

</div>

@dotproduct Thanks for the suggestion. I was able to display the image in a new document. But i would like the image to be placed at a specific position also. Below is the code.

```auto
const currentDocument = app.activeDocument;
const newDocument = await app.open(file);

const transformedImageLayer =
    await newDocument.activeLayers.at(0).duplicate(
        currentDocument
    );

await newDocument.close(
    constants.SaveOptions.DONOTSAVECHANGES
);

```

Is there any way to open at a given position?

---

<div class="post-metadata">

**Author:** ![akash\_panigrahi](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@akash\_panigrahi](https://forums.creativeclouddeveloper.com/u/akash_panigrahi)\
**Post date:** [December 23, 2023, 11:45am UTC](https://forums.creativeclouddeveloper.com/t/distorted-image-created-with-createimagedatafrombuffer-function/7270/5 "2023-12-23T11:45:25Z")

</div>

I thought of a solution to simply move the layer using the below code. I actually would have preferred to do it via any direct APIs, but anyway 🤷🏿‍♂️.

```auto
async function changeLayerPosition(sourceLayer, targetBounds) {
    await require("photoshop").app.batchPlay(
        [
            {
                _obj: "select",
                _target: [
                    {
                        _ref: "layer",
                        _name: sourceLayer.name,
                    },
                ],
                makeVisible: false,
                layerID: [sourceLayer.id],
                _isCommand: false,
            },
            {
                _obj: "move",
                _target: [
                    {
                        _ref: "layer",
                        _enum: "ordinal",
                        _value: "targetEnum",
                    },
                ],
                to: {
                    _obj: "offset",
                    horizontal: {
                        _unit: "pixelsUnit",
                        _value: targetBounds.left,
                    },
                    vertical: {
                        _unit: "pixelsUnit",
                        _value: targetBounds.top,
                    },
                },
                _options: {
                    dialogOptions: "dontDisplay",
                },
            },
            {
                _obj: "selectNoLayers",
                _target: [
                    {
                        _ref: "layer",
                        _enum: "ordinal",
                        _value: "targetEnum"
                    }
                ],
                _options: {
                    dialogOptions: "dontDisplay"
                }
            }

        ],
        {}
    );
}

```
