AutoCAD raster image object with pyautocad

While working with AutoCAD, sometimes we need to insert external objects such as images. That is why in this blog post we will discuss how to insert an image in a AutoCAD template using the pyautocad module.

Setting up environment using pyautocad (Python)

As usual, we will set up our work environment to integrate Python and AutoCAD by importing the pyautocad module.

from pyautocad import Autocad, APoint, aDouble

acad = Autocad()

We can use a direct approach of integrating AutoCAD and Python using the pywin32 and pythoncom packages. A detailed introduction on using the same is discussed in a couple of previous blog posts in this pyautocad series of blog posts.

Adding image to AutoCAD template using AddRaster method

To add an image from our files we just have to use a method called AddRaster.

We have to use the AddRaster method against the template block and pass the image path and some other parameters.

The syntax for the AddRaster method is as follows:

object.AddRaster(ImageFileName, InsertionPoint, ScaleFactor, RotationAngle)

Here, the Insertion point is a 3D array of doubles. The scale factor and rotational angle is set to 1 and 0 respectively by default.

We can change the values of scale factor and rotational angle to the values we want.

Let’s add the image using the AddRaster method.

ri = acad.model.AddRaster("C:\\Users\\91998\\OneDrive\\Desktop\\physics-science-school-blueprint-G35FEF.jpg", APoint(100, 100, 0), 1, 0)
Figure 1.1: Added image to AutoCAD template using AddRaster method

As we can see from Figure 1 the image has been added. We can scale the image up or down by passing the parameter into the main command, or by using the scale method.

Properties of the raster image object

As with other AutoCAD objects, properties of the raster image can be fetched or changed.

Below I check out some of the properties of this image:




So this is how we can deal with the raster image object in AutoCAD using the pyautocad module.

You May Also Like

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.