If you are editing images, you may want to add text at a particular set of coordinates. If so, you can use built-in JES functions such as addText() or addTextWithStyle(). These functions allow you to configure where the text appears, the color of the text, and other styling options.
How to Add Text Onto an Image
You can add text onto an image in JES by using the addText() function. Since JES uses the Jython programming language, make sure you brush up on your Python syntax with some basic Python examples.
Open the JES application on your computer. In the programming window, create a new function called textOnImage: def textOnImage(): Inside the function, use the pickAFile() function to ask the user to select a local file on their computer: file = pickAFile() Create a picture object using the makePicture() function: pic = makePicture(file) Declare the string that you would like to add to the image: str = “This is a test. " Use the addText() function to add the text onto the image. For the first argument, enter the image. For the second and third arguments, enter the coordinates where you want the text to show. Pass the string variable as the final argument: addText(pic, 50, 50, str) By default, the color of the text is black. You can change the color of the text by passing the name of a color as the fifth argument: addText(pic, 100, 100, str, blue) Show the picture using the show() function to render the image in JES onto the screen: show(pic) Click on the Load Program button, located underneath the programming area. If prompted to save the file, click on Yes. Enter the textOnImage() function into the command line to run it: Wait for the function to display the image with text.
How to Add Styled Text
You can also add styled text onto an image in JES using the addTextWithStyle() function.
Create a new function called StyledTextOnImage: def StyledTextOnImage(): Inside the function, import “java. awt. Font”. This will give you access to certain styles such as bold font. import java. awt. Font as Font Use the pickAFile() function to ask the user to select a local file on their computer: file = pickAFile() Use the makePicture() function to create a picture object: pic = makePicture(file) Declare the string that you would like to add to the image: str = “This is a test. " Use the makeStyle() function to create a font style object. The makeStyle() function allows you to change the font family and size. You can also use it to make text bold, italicized, or underlined. myFont = makeStyle(“Arial”, Font. BOLD, 64) Use the addTextWithStyle() function to add the text onto the image. As before, the arguments include the image itself, the x and y coordinates of the text location, and the text string. Additionally, input the custom style as the fifth argument: addTextWithStyle(pic, 50, 50, str, myFont) As with addText(), the text is black by default, but you can specify a different color with an extra, sixth parameter: addTextWithStyle(pic, 100, 100, str, myFont, blue) Show the image: show(pic) Click on the Load Program button, located underneath the programming area. If prompted to save the file, click on Yes. Enter the StyledTextOnImage() function into the command line to run it: StyledTextOnImage() Wait for the function to display the image with text.
Adding Text to Images Using JES
Now you’ve seen how to add text to images, you can check out some of the other interesting things you can do to images with JES. This powerful environment lets you crop and scale images, apply filters, and carry out color transformations.