# Creating an Image¶

## Problem¶

You want to create a new blank image with a width of 640 pixels and a height of 480 pixels.

## Solution¶

import Draft

img = Draft.Image.CreateImage( 640, 480 )

# save the image for later use
img.WriteToFile( 'blank image.jpg' )


## Discussion¶

The first line (import Draft) imports the functionality of Draft. The second line asks Draft to create an image with the given width (640 pixels) and height (480 pixels), and saves it in the variable which we have chosen to call img. Note that we can also save the width and height values in variables, and then pass the variables to CreateImage():

newWidth = 1024
newHeight = 768

newImage = Draft.Image.CreateImage( newWidth, newHeight )


If, instead of starting with a blank image, you want to start with an image you have saved, you can use Draft’s ReadFromFile() method instead of CreateImage(). Instead of specifying a width and height, we now specify the filename. If the image file is not in the current directory, we must include the path to the image as part of the filename. For example:

myImg = Draft.Image.ReadFromFile( r'X:\project\shot\Patches with ball.jpg' )


Note that we have used quotes around the filename. Quotation marks are used to indicate the start and end of non-numeric data literals, called strings (short for “string of characters”). We can use either single quotes (‘) or double quotes (”), as long as the quotes at the beginning and end match. We have also used the letter r before the first quote to indicate that the backslashes and characters following the backslashes are to be treated as separate characters, not as an escape sequence. (Escape sequences are used to include special characters in strings.)

The final line of the solution saves our image to a file, in this case, ‘blank image.jpg’ in the current working directory. (Again, if you wish the image to be saved elsewhere, you must specify the path as part of the directory.) Draft automatically detects the format for the image from the filename extension.