How to set image background tranparent color with AutoLisp

How to set image background tranparent color with AutoLisp

lpr8PZGA
Contributor Contributor
6,144 Views
32 Replies
Message 1 of 33

How to set image background tranparent color with AutoLisp

lpr8PZGA
Contributor
Contributor

Hello,

 

how do I set image (TIFF) background transparent color (to white)?

 

- In the picture attached you can see how to do it via Properties Bar:

1. turn image transparency ON,

2. select transparency color [Select <] ... and pick e.g. white color in the image.

 

I observed that this may add some "AcMapRasterExTrClr" x-data, but not only this...

 

So - how to set the white color as transparent for the image with AutoLisp (or VisualLisp)?

 

TIA.

 

Lukas

0 Likes
6,145 Views
32 Replies
Replies (32)
Message 2 of 33

LDShaw
Collaborator
Collaborator

Many moons ago I had to do something like this. 
This is what I ended up with. 

 

(setq acadobject (vlax-get-acad-object))
(setq acadpref (vlax-get-property acadobject 'preferences))
(setq acaddisp (vlax-get-property acadpref 'display))
(vlax-put-property acaddisp 'GraphicsWinmodelBackgrndColor 1423) ;;Model space background
(vlax-put-property acaddisp 'GraphicsWinLayoutBackgrndColor  0) ;;command area
(vlax-put-property acaddisp 'ModelCrosshairColor 16777215) ;; crosshairs
(vlax-put-property acaddisp 'TextWinBackgrndColor 16777215) ;;command area

 

 

Here is the Thread on it.
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/changing-page-layout-paper-backgroun...

There is the true color to rgb lisp

(defun RGBToTrueColor (rgb)
 (setq r (lsh (car rgb) 16))
 (setq g (lsh (cadr rgb) 8))
 (setq b (caddr rgb))
 (setq tcol (+ (+ r g) b)) 
) 




 

 

0 Likes
Message 3 of 33

lpr8PZGA
Contributor
Contributor

Hello LDSha,

I guess your code doesn't change entity property (= transparency color for an image) but some colors of the ACad window, like model background, command window background etc.

But I don't see a way to use the code with an drawing entity (or entity of IMAGE type/class).

(I had some ideas which didn't work...)

0 Likes
Message 4 of 33

LDShaw
Collaborator
Collaborator

You are right. I thought you wanted to change the background. Not a tiff. Sorry. 
Next sorry, I have to get the stupid questions out of the way first. It always bites me when I don't.

Can you set the transparency manually? 
Have you looked at for examples? 

Can you attach the tiff so I can try something? I don't typically work with Transparent backgrounds but this sounds like a fun challenge. 

0 Likes
Message 5 of 33

lpr8PZGA
Contributor
Contributor

Hello,

I attached a sample TIFF.

And yes, I can set the background color manually with Properties Bar to a single file.

Once I select more TIFFs the "Image Background Color" (picker) in the Properties Bar disappears, so I can only set the background color per a single IMAGE.

And in my situation I have several hundreds of TIFFs (photogrammetry) to set transparent bkg color to white manually!

So I'd like to write a simple Lisp which will walk through a selection set of IMAGEs and set the bkg trans color to white for each of them...

0 Likes
Message 6 of 33

paullimapa
Mentor
Mentor

I'm not seeing that option on my versions of AutoCAD from 2020 through 2025.

It's not there in the Ribbon nor in the Properties window to set the Transparency Background Color of a Tif Image once the Transparency setting is set to Yes:

paullimapa_0-1732730999943.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 33

paullimapa
Mentor
Mentor

Here's the same screenshot but now with your Tif attached.

Still no option to select Transparency Background Color

paullimapa_0-1732731137609.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 33

CodeDing
Mentor
Mentor

@paullimapa ,

 

I see it in Acad '21 (untested in others):

image.png

0 Likes
Message 9 of 33

paullimapa
Mentor
Mentor

odd because still not there for me

paullimapa_0-1732732921079.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 33

LDShaw
Collaborator
Collaborator

I believe this is a rastercad\civil command. 
rbackground 



https://help.autodesk.com/view/RSTR/2022/ENU/?guid=GUID-777653CA-62A3-44DC-8834-4D307EAA9E9C
I won't swear to it. 

Message 11 of 33

CodeDing
Mentor
Mentor

@lpr8PZGA ,

 

Your image is not inherently transparent; it has a White background.

1) To set the 'transparency' it asks you to select a color...

2) Select the White color on your image and it will make all pixels of that same color transparent.

3) But be aware that other White pixels will become transparent also; like the ones in your paint stripe..

 

1)

image.png

 

2) (select OK after picking white pixel)

image.png

 

3)

image.png

 

Best,

~DD

0 Likes
Message 12 of 33

paullimapa
Mentor
Mentor

I see..so not in my vanilla autocad then


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 13 of 33

CodeDing
Mentor
Mentor

@LDShaw  @paullimapa ,

 

I am running "Civil 3D as AutoCAD 2021". Didn't know it would matter lol

 

image.png

 

image.png

Message 14 of 33

LDShaw
Collaborator
Collaborator

I have raster but not loaded. I was debating if it was worth it. I'd think the lisp would be pretty simple. Especially if  lpr8PZGA can type the commands out he's using to do it instead of using icons.  Pretty sure a rbackground can be used in a set. 

0 Likes
Message 15 of 33

paullimapa
Mentor
Mentor

so I wonder in Civil3d if you run this line of code and select the Tif IMAGE would there be a propertyvalue that you can change for the Transparency Background Color?

 

(dumpallproperties (car (entsel)))

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 16 of 33

CodeDing
Mentor
Mentor

@paullimapa ,

 

There's nothing in (dumpallproperties ...) that seems to apply. But we can see an assigned color in the App portion of entity definition data:

 

((-1 . <Entity name: 20a91eef9b0>) (0 . "IMAGE") (330 . <Entity name: 20a2814c1f0>) (5 . "10496D3")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "_WORKING") (100 . "AcDbRasterImage") (90 . 0)
(10 3.17639e+06 1.68987e+06 0.0) (11 0.00722791 0.0 0.0) (12 4.42582e-19 0.00722791 0.0) (13 8350.0 8345.0 0.0)
(340 . <Entity name: 20a91eef990>) (70 . 15) (280 . 0) (281 . 50) (282 . 50) (283 . 0) (290 . 0)
(360 . <Entity name: 20a91eef9a0>) (71 . 1) (91 . 2) (14 -0.5 -0.5 0.0) (14 8349.5 8344.5 0.0)
(-3 ("AcMapRasterExTrClr" (1071 . 16777215))))

 

Here's dump if you want to check:

------- BEFORE Transparency Color Applied -------
Select object: Begin dumping object (class: AcDbRasterImage)
Annotative (type: bool)  (LocalName: Annotative) = Failed to get value
AnnotativeScale (type: AcString)  (RO)  (LocalName: Annotative scale) = Failed to get value
BlockId (type: AcDbObjectId)  (RO) = 20a2814c1f0
Brightness (type: unsigned char)  (LocalName: Brightness) = 50
CastShadows (type: bool) = 0
ClassName (type: AcString)  (RO) =
ClipBoundaryType (type: AcDbRasterImage::ClipBoundaryType)  (RO) = 1
CollisionType (type: AcDb::CollisionType)  (RO) = 1
Color (type: AcCmColor)  (LocalName: Color) = BYLAYER
Contrast (type: unsigned char)  (LocalName: Contrast) = 50
ExtensionDictionary (type: AcDbObjectId)  (RO) = 0
Fade (type: unsigned char)  (LocalName: Fade) = 0
Handle (type: AcDbHandle)  (RO) = 10496d3
HasFields (type: bool)  (RO) = 0
HasSaveVersionOverride (type: bool) = 0
Height (type: double)  (LocalName: Height) = 60.316877
Hyperlinks (type: AcDbHyperlink*)
ImageDefId (type: AcDbObjectId)  (RO) = 20a91eef990
ImageDefId/SourceFileName (type: AcString)  (RO)  (LocalName: Saved Path) = C:\Users\ddeterding\Downloads\ZM_Obl2_96.tif
ImageHeight (type: double)  (RO) = 8345.000000
ImageName (type: AcString)  (RO)  (LocalName: Name) = ZM_Obl2_96
ImageTransparency (type: bool)  (LocalName: Background transparency) = 1
ImageWidth (type: double)  (RO) = 8350.000000
IsA (type: AcRxClass*)  (RO) = AcDbRasterImage
IsAProxy (type: bool)  (RO) = 0
IsCancelling (type: bool)  (RO) = 0
IsClipped (type: bool)  (LocalName: Show clipped) = 1
IsEraseStatusToggled (type: bool)  (RO) = 0
IsErased (type: bool)  (RO) = 0
IsModified (type: bool)  (RO) = 0
IsModifiedGraphics (type: bool)  (RO) = 0
IsModifiedXData (type: bool)  (RO) = 0
IsNewObject (type: bool)  (RO) = 0
IsNotifyEnabled (type: bool)  (RO) = 0
IsNotifying (type: bool)  (RO) = 0
IsObjectIdsInFlux (type: bool)  (RO) = 0
IsPersistent (type: bool)  (RO) = 1
IsPlanar (type: bool)  (RO) = 0
IsReadEnabled (type: bool)  (RO) = 1
IsReallyClosing (type: bool)  (RO) = 1
IsTransactionResident (type: bool)  (RO) = 0
IsUndoing (type: bool)  (RO) = 0
IsWriteEnabled (type: bool)  (RO) = 0
LayerId (type: AcDbObjectId)  (LocalName: Layer) = 20a1e2e64a0
LineWeight (type: AcDb::LineWeight)  (LocalName: Lineweight) = -1
LinetypeId (type: AcDbObjectId)  (LocalName: Linetype) = 20a2814c150
LinetypeScale (type: double)  (LocalName: Linetype scale) = 1.000000
LocalizedName (type: AcString)  (RO) = Raster Image
MaterialId (type: AcDbObjectId)  (LocalName: Material) = Failed to get value
MergeStyle (type: AcDb::DuplicateRecordCloning)  (RO) = 1
ObjectId (type: AcDbObjectId)  (RO) = 20a91eef9b0
OwnerId (type: AcDbObjectId)  (RO) = 20a2814c1f0
PixelToModelTransform (type: AcGeMatrix3d)  (RO) = ((0.007228 0.000000 0.000000 0.000000)(-0.000000 -0.007228 0.000000 0.000000) (0.000000 0.000000 1.000000 0.000000) (3176389.345288 1689930.849196 0.000000 1.000000))
PlotStyleName (type: AcString)  (RO)  (LocalName: Plot style) = ByColor
Position/X (type: double)  (LocalName: Position) = 3176389.341674
Position/Y (type: double) = 1689870.535933
Position/Z (type: double) = 0.000000
ReactorId (type: AcDbObjectId) = 20a91eef9a0
ReceiveShadows (type: bool) = 0
Rotation (type: double)  (LocalName: Rotation) = 0.000000
Scale (type: double)  (LocalName: Scale) = 60.353017
ShadowDisplay (type: AcDb::ShadowFlags)  (LocalName: Shadow Display) = Failed to get value
ShowImage (type: bool)  (LocalName: Show image) = 1
Transparency (type: AcCmTransparency)  (LocalName: Transparency) = 0
Visible (type: AcDb::Visibility) = 0
Width (type: double)  (LocalName: Width) = 60.353017
End object dump


------- AFTER Transparency Color Applied -------
Select object: Begin dumping object (class: AcDbRasterImage)
Annotative (type: bool)  (LocalName: Annotative) = Failed to get value
AnnotativeScale (type: AcString)  (RO)  (LocalName: Annotative scale) = Failed to get value
BlockId (type: AcDbObjectId)  (RO) = 20a2814c1f0
Brightness (type: unsigned char)  (LocalName: Brightness) = 50
CastShadows (type: bool) = 0
ClassName (type: AcString)  (RO) =
ClipBoundaryType (type: AcDbRasterImage::ClipBoundaryType)  (RO) = 1
CollisionType (type: AcDb::CollisionType)  (RO) = 1
Color (type: AcCmColor)  (LocalName: Color) = BYLAYER
Contrast (type: unsigned char)  (LocalName: Contrast) = 50
ExtensionDictionary (type: AcDbObjectId)  (RO) = 0
Fade (type: unsigned char)  (LocalName: Fade) = 0
Handle (type: AcDbHandle)  (RO) = 10496d3
HasFields (type: bool)  (RO) = 0
HasSaveVersionOverride (type: bool) = 0
Height (type: double)  (LocalName: Height) = 60.316877
Hyperlinks (type: AcDbHyperlink*)
ImageDefId (type: AcDbObjectId)  (RO) = 20a91eef990
ImageDefId/SourceFileName (type: AcString)  (RO)  (LocalName: Saved Path) = C:\Users\ddeterding\Downloads\ZM_Obl2_96.tif
ImageHeight (type: double)  (RO) = 8345.000000
ImageName (type: AcString)  (RO)  (LocalName: Name) = ZM_Obl2_96
ImageTransparency (type: bool)  (LocalName: Background transparency) = 1
ImageWidth (type: double)  (RO) = 8350.000000
IsA (type: AcRxClass*)  (RO) = AcDbRasterImage
IsAProxy (type: bool)  (RO) = 0
IsCancelling (type: bool)  (RO) = 0
IsClipped (type: bool)  (LocalName: Show clipped) = 1
IsEraseStatusToggled (type: bool)  (RO) = 0
IsErased (type: bool)  (RO) = 0
IsModified (type: bool)  (RO) = 0
IsModifiedGraphics (type: bool)  (RO) = 0
IsModifiedXData (type: bool)  (RO) = 0
IsNewObject (type: bool)  (RO) = 0
IsNotifyEnabled (type: bool)  (RO) = 0
IsNotifying (type: bool)  (RO) = 0
IsObjectIdsInFlux (type: bool)  (RO) = 0
IsPersistent (type: bool)  (RO) = 1
IsPlanar (type: bool)  (RO) = 0
IsReadEnabled (type: bool)  (RO) = 1
IsReallyClosing (type: bool)  (RO) = 1
IsTransactionResident (type: bool)  (RO) = 0
IsUndoing (type: bool)  (RO) = 0
IsWriteEnabled (type: bool)  (RO) = 0
LayerId (type: AcDbObjectId)  (LocalName: Layer) = 20a1e2e64a0
LineWeight (type: AcDb::LineWeight)  (LocalName: Lineweight) = -1
LinetypeId (type: AcDbObjectId)  (LocalName: Linetype) = 20a2814c150
LinetypeScale (type: double)  (LocalName: Linetype scale) = 1.000000
LocalizedName (type: AcString)  (RO) = Raster Image
MaterialId (type: AcDbObjectId)  (LocalName: Material) = Failed to get value
MergeStyle (type: AcDb::DuplicateRecordCloning)  (RO) = 1
ObjectId (type: AcDbObjectId)  (RO) = 20a91eef9b0
OwnerId (type: AcDbObjectId)  (RO) = 20a2814c1f0
PixelToModelTransform (type: AcGeMatrix3d)  (RO) = ((0.007228 0.000000 0.000000 0.000000)(-0.000000 -0.007228 0.000000 0.000000) (0.000000 0.000000 1.000000 0.000000) (3176389.345288 1689930.849196 0.000000 1.000000))
PlotStyleName (type: AcString)  (RO)  (LocalName: Plot style) = ByColor
Position/X (type: double)  (LocalName: Position) = 3176389.341674
Position/Y (type: double) = 1689870.535933
Position/Z (type: double) = 0.000000
ReactorId (type: AcDbObjectId) = 20a91eef9a0
ReceiveShadows (type: bool) = 0
Rotation (type: double)  (LocalName: Rotation) = 0.000000
Scale (type: double)  (LocalName: Scale) = 60.353017
ShadowDisplay (type: AcDb::ShadowFlags)  (LocalName: Shadow Display) = Failed to get value
ShowImage (type: bool)  (LocalName: Show image) = 1
Transparency (type: AcCmTransparency)  (LocalName: Transparency) = 0
Visible (type: AcDb::Visibility) = 0
Width (type: double)  (LocalName: Width) = 60.353017
End object dump
0 Likes
Message 17 of 33

paullimapa
Mentor
Mentor

Thanks for the screen dump...so the typical ImageTransparency property is there:

ImageTransparency (type: bool)  (LocalName: Background transparency) = 1

 But the color is definitely xdata and probably could be obtained also with this code:

; getXData retrieves xdata from selected entity
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/seek-a-lisp-to-change-revclound-to-polygon-with-specific-layer/m-p/13147507/highlight/true#M474693
(defun c:getXData ( / ename edata )
  (if (setq ename (car (entsel "\nSelect entity to read xdata: ")))
    (progn
      (setq edata (entget ename '("*")))
      (if (setq xdata (cdr (assoc -3 edata)))
        (foreach item xdata
          (print item)
        )
        (princ "\nEntity has no xdata.")
      )
    )
  )
  (princ)
)

Should be able to create new xdata code to replace the color...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 18 of 33

CodeDing
Mentor
Mentor

@lpr8PZGA ,

 

Ok so I can not find a way to apply them 100%. So you have to do 2 things:

1) Select the Transparency Color ONCE on ONE image (as shown in my message 11)

2) Run this code, selecting ALL of your images, and it should apply the white color automatically as background for all of them:

;; Raster Image Transparency
(defun c:RIT ( / clr ss cnt len e eg xd)
  (setq clr 16777215)
  (if (setq ss (ssget '((0 . "IMAGE"))))
    (progn
      (if (null (tblsearch "APPID" "AcMapRasterExTrClr"))
        (regapp "AcMapRasterExTrClr")
      );if
      (setq cnt (sslength ss) len (itoa cnt))
      (repeat cnt
        (setq e (ssname ss (setq cnt (1- cnt))))
        (setpropertyvalue e "ImageTransparency" 1)
        (setq eg (entget e))
        (if (setq xd (assoc -3 eg))
          (setq eg (subst (append xd (list (list "AcMapRasterExTrClr" (cons 1071 clr)))) xd eg))
          (setq eg (append eg (list (list -3 (list "AcMapRasterExTrClr" (cons 1071 clr))))))
        );if
        (entmod eg)
      );repeat
      (alert (princ (strcat "\n" len " image transparencies applied.")))
    );progn
  ;else
    (prompt "\nNo Raster Images selected. Try again.")
  );if
  (princ)
);defun

 

Best,

~DD

0 Likes
Message 19 of 33

lpr8PZGA
Contributor
Contributor

Hello CodeDing,

thanks for the code. It's similar enough to that I arrived to. Unfortunately, it doesn't work. Did you test it?

BTW, I tried it in Civil 3D as "vanilla ACad" doesn't allow to pick color for the bkg transparency for an IMAGE - and also I placed this question to C3D forum, but it was moved by an admin into this branch later.

0 Likes
Message 20 of 33

CodeDing
Mentor
Mentor

@lpr8PZGA ,

 

Sorry for late reply; been busy over the holiday here.

 


@lpr8PZGA wrote:

I tried it in Civil 3D as "vanilla ACad" doesn't allow to pick color for the bkg transparency for an IMAGE


You do not need to do it in vanilla Acad; you can run it in Civil 3D.

 


@lpr8PZGA wrote:

Unfortunately, it doesn't work. Did you test it?


Yes, I did test it; all is working on my end (see below gif). What does your command history say when you run it?

 

RIT.gif

 

Best,

~DD

0 Likes