• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Selection of All White Pixel Clusters >1x800 and Fill in New Channel

Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

I have a TIFF file with dimensions 18320x24440 pixels and a resolution of 508 ppi. The image consists only of black and white pixels, with no gray tones. I would like to create a script that does the following:

  1. Select all the white areas with a width or height greater than 1x800 pixels.
  2. Combine these areas into one selection.
  3. Fill a layer or channel within this selection with black.

Can someone help me with this?

TOPICS
Actions and scripting , macOS

Views

497

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Aug 08, 2024 Aug 08, 2024

Hi, 

Save the following in a .psjs file and File > Scripts > Browse...

It should output a selection of the clusters you need. Caveat: it doesn't work on Bitmap, please convert the image to Grayscale first.

 

const photoshop = require("photoshop");
const { core, imaging, app } = photoshop;

async function getDocumentPixelData(documentID) {
  const document = app.activeDocument;
  const width = document.width;
  const height = document.height;

  try {
    // Get the pixel data of the document
    con
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

Using Work Path in ESTK would provide an option. 

Though maybe UXP Scripting offers more convenient (and speedier) approaches. 

 

What exactly do you mean by »1x800 pixels«? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

I'll try to explain. Normally, we use industry software designed for this purpose, but it no longer receives updates and only runs on Windows. I work for a textile pattern design company, and we need to check designs against certain specifications. One of the specs is that there cannot be vertical sections of white that are 800 pixels or longer at a resolution of 508. All larger sections need to have a sort of bridge. So we need to look up those parts and then draw some lines in the dessin style to overcome these sections

A will attache a screenshot of a tiff file that was generated by the other software.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

But the 800px apply in both directions? 

Can you provide a sample file? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

No, if you have a document in portait mode from 18320x24440 in a 508 ppi reso, the max white part allowed for this "check-script" 1x799. So every part 1x800 or bigger must be selected... I`ll attache a sample file. I saved the tiff as psd files. Normally we have to work in tiff within this other software. The 3d.psd is the design that has been checked. And the 4cm controle.psd is the outcome of the check software.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

The Script ran almost 17min to create this, so hardly useful. 

Screenshot 2024-08-05 at 16.30.38.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

Thank for trying to. But 17 mins is a long time to do just a check. And it doesn’t even has the same outcome. I can see it also selected smaller parts. You van see the difference with the 4cm controle file. Thanks anyway! Much appreciated 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

quote

Thank for trying to. But 17 mins is a long time to do just a check. And it doesn’t even has the same outcome. I can see it also selected smaller parts. You van see the difference with the 4cm controle file. Thanks anyway! Much appreciated 


By @WMETS

I misunderstood – I though any white area of more than 799px height was to be marked. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

You didn't misunderstood. Bit in a document with a reso 508 ppi (200 p p cm), 799 = 4cm.

There are parts marked in your example that are smaller then 799 px.

You can compare the outcome with the 4cm controle.pdf file i attached before.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

Each of the areas marked in red is higher than 799px. 

But apparently this isn’t about the areas but the pixel columns. 

Edit: That was my misunderstanding. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

Exactly. So, essentially, what the script needs to do is scan each column that is 1 pixel wide, similar to how a flatbed scanner works. It should check if there are parts that are 800 white pixelsor more in the column. If so, these parts should be selected and combined into one selection. That later can be filled in a new channel or layer

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2024 Aug 06, 2024

Copy link to clipboard

Copied

Unfortunately that (while possible) takes even longer with an ESTK-Script in Photoshop … though maybe someone else has a better approach. 

 

Have you followed up on @Lumigraphics’ recommendation? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2024 Aug 06, 2024

Copy link to clipboard

Copied

I have signed up to their forum. Will post a subject later. Thanks for trying pfaffenbichler

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

Oh, and by the way, thanks for the quick response

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 05, 2024 Aug 05, 2024

Copy link to clipboard

Copied

You might look at ImageJ, it has a lot of plugins and scripts and probably someone has a way to accomplish this. Post in their forums for help.

 

imagej.net

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2024 Aug 06, 2024

Copy link to clipboard

Copied

For instance, this?

https://imagej.net/ij/plugins/line-analyzer.html

 

That's really cool, first I've hear of ImageJ. Thanks @Lumigraphics 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2024 Aug 07, 2024

Copy link to clipboard

Copied

Wayne Rasband is the original developer of what was then NIHImage and is now ImageJ. Definitely a great tool for image analysis.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2024 Aug 07, 2024

Copy link to clipboard

Copied

Thanks for the suggestion. Just did some research and I think it can help us. Ran some custom script trials (via chatGPT) and it surprised us with the results. Maybe we can figure this out all alone, or maybe we have to ask the forum. But for now it looks hopefull.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

Hi, 

Save the following in a .psjs file and File > Scripts > Browse...

It should output a selection of the clusters you need. Caveat: it doesn't work on Bitmap, please convert the image to Grayscale first.

 

const photoshop = require("photoshop");
const { core, imaging, app } = photoshop;

async function getDocumentPixelData(documentID) {
  const document = app.activeDocument;
  const width = document.width;
  const height = document.height;

  try {
    // Get the pixel data of the document
    const imageObj = await imaging.getPixels({
      documentID: document.id,
      colorSpace: "Grayscale",
    });
    return imageObj.imageData;
  } catch (error) {
    console.error("Error getting document pixel data:", error);
    throw error;
  }
}

async function findWhiteSegments(imageData, width, height, minLength) {
  try {
    const pixelData = await imageData.getData();
    const whiteSegments = [];
    const components = imageData.components;

    // Scan columns
    for (let x = 0; x < width; x++) {
      let whiteLength = 0;
      let startY = -1;
      for (let y = 0; y < height; y++) {
        const index = (y * width + x) * components;
        const gray = pixelData[index];

        if (gray === 255) {
          if (whiteLength === 0) startY = y;
          whiteLength++;
        } else {
          if (whiteLength >= minLength) {
            whiteSegments.push({ x, startY, length: whiteLength });
          }
          whiteLength = 0;
          startY = -1;
        }
      }
      if (whiteLength >= minLength) {
        whiteSegments.push({ x, startY, length: whiteLength });
      }
    }

    return whiteSegments;
  } catch (error) {
    console.error("Error finding white segments:", error);
    throw error;
  }
}

async function createSelectionFromSegments(segments, width, height) {
  try {
    const selectionData = new Uint8Array(width * height).fill(0);

    segments.forEach((segment) => {
      for (let i = 0; i < segment.length; i++) {
        selectionData[(segment.startY + i) * width + segment.x] = 255;
      }
    });

    const selectionBuffer = {
      typedArray: selectionData,
      width,
      height,
      components: 1,
      colorSpace: "Grayscale",
      colorProfile: "Gray Gamma 2.2",
    };

    return selectionBuffer;
  } catch (error) {
    console.error("Error creating selection from segments:", error);
    throw error;
  }
}

async function putSelectionFromBuffer(documentID, selectionBuffer) {
  try {
    console.log("Creating image data from buffer...");
    const imageData = await imaging.createImageDataFromBuffer(
      selectionBuffer.typedArray,
      {
        width: selectionBuffer.width,
        height: selectionBuffer.height,
        components: selectionBuffer.components,
        colorSpace: selectionBuffer.colorSpace,
        // colorProfile: selectionBuffer.colorProfile,
        chunky: true,
      }
    );

    console.log("Putting selection...");
    try {
      await imaging.putSelection({
        imageData,
      });
    } catch (error) {
      console.error("Error putting selection:", error);
      throw error;
    }

    console.log("Selection added successfully.");
  } catch (error) {
    console.error("Error putting selection from buffer:", error);
    throw error;
  }
}

async function main() {
  try {
    await core.executeAsModal(
      async () => {
        const documentID = app.activeDocument.id;
        const imageData = await getDocumentPixelData(documentID);
        const width = imageData.width;
        const height = imageData.height;
        const minLength = 800;

        const whiteSegments = await findWhiteSegments(
          imageData,
          width,
          height,
          minLength
        );
        const selectionBuffer = await createSelectionFromSegments(
          whiteSegments,
          width,
          height
        );

        await putSelectionFromBuffer(documentID, selectionBuffer);
      },
      { commandName: "Get Document Pixels" }
    );
  } catch (error) {
    console.error("Error in main function:", error);
  }
}

main().catch(console.error);

 

It should give you a good starting point.

Best,

 

 

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

Wow, this looks great. And it's fast at the first trial we did. I'm going to do some more tests next week. I'll keep you informed. Maybe we need your help/knowledge for some extra things if you like that. For now, many thanks!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

Please remember to mark the correct answer as such (after exhaustive testing). 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

offcourse!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

PS what is the correct way to do. If the answer of Davide Barranca was correct, but I want to ask some extra's done by the script, do I need to start a new topic and vote the first answer from him as correct? Or can I follow up with extra questions in this topic?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

You can do either, if you start a new thread you can tag him by typing »@Davide_Barranca« and he should be notified. 

Screenshot 2024-08-08 at 17.01.05.png

If you follow up on this thread you can, should he provide more pertinent answers/code, mark more than one post »Correct Answer«. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

Thanks @c.pfaffenbichler 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines