Search code examples
javascriptphotoshopphotoshop-scriptphotoshop-cs3

Photoshop Script to create text in a bitmap image in Photoshop


I have very large size 1-bit images that I need to write arrays of text to in Photoshop. I can do this in javascript by converting the images to grayscale and then creating a new layer for each block of text, but I would like to be able to write text directly onto the 1-bit bitmap to save time. Is there a way to do this in javascript?


Solution

  • You can create text with scripting. You will need to be in grayscale (or RGB) to do so. Here's a basic text function. You will have to position the text after it is created as there is no way of getting it's size before it's creation. Hope this helps.

    createText("Arial-BoldMT", 48, 0,128,0, "Hello World", 100, 50)
    activeDocument.activeLayer.name = "Text";
    activeDocument.activeLayer.textItem.justification = Justification.CENTER
    
    function createText(fface, size, colR, colG, colB, content, tX, tY)
    {
      // Add a new layer in the new document
      var artLayerRef = app.activeDocument.artLayers.add()
    
      // Specify that the layer is a text layer
      artLayerRef.kind = LayerKind.TEXT
    
      //This section defines the color of the hello world text
      textColor = new SolidColor();
      textColor.rgb.red = colR;
      textColor.rgb.green = colG;
      textColor.rgb.blue = colB;
    
      //Get a reference to the text item so that we can add the text and format it a bit
      textItemRef = artLayerRef.textItem
      textItemRef.font = fface;
      textItemRef.contents = content;
      textItemRef.color = textColor;
      textItemRef.size = size
      textItemRef.position = new Array(tX, tY) //pixels from the left, pixels from the top
    }