Author Topic: Autolisp to get bitmap pixel values  (Read 8159 times)

0 Members and 2 Guests are viewing this topic.

Ben Clark

  • Newt
  • Posts: 94
Autolisp to get bitmap pixel values
« on: August 07, 2019, 04:57:50 PM »
What is the best way to get a bitmap's pixels (monochrome in this instance) into a lisp list. I'm wanting to be able to select an image file, extract it's pixel values (0-255 monochrome for each pixel)  into a list, and use them for input into a neural network (don't ask why  :wink:)

any help would be much apprecitated.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Autolisp to get bitmap pixel values
« Reply #1 on: August 07, 2019, 05:15:19 PM »
I've written a program to perform this task (and used it to produce this program, and these icons); I intend to publish it to my site at some point, but I haven't had the time to finish the code.

Ben Clark

  • Newt
  • Posts: 94
Re: Autolisp to get bitmap pixel values
« Reply #2 on: August 07, 2019, 05:17:59 PM »
Nice, Lee!

Is it in any sort of form where you could share a rough version? I'd be spinning my wheels for a while to try and figure it out.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #3 on: August 07, 2019, 05:37:12 PM »
In addition to Lee's (usual) great stuff there's also this and this if you wanted to (mostly) "roll yer own". If I had the time or need I'd already have penned such a function. Need may rear it's head soon, who knows. Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ben Clark

  • Newt
  • Posts: 94
Re: Autolisp to get bitmap pixel values
« Reply #4 on: August 08, 2019, 06:07:13 PM »
Thanks for those links MP. I'll see what I can string together.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #5 on: August 08, 2019, 09:37:08 PM »
You’re welcome Ben. Image processing is a cool area. I have done some modest pixel abuse (rotating images etc) thru powershell (generating and invoking a powershell script thru lisp). I’ll rummage thru my code and see if there’s anything useful / shareable.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #6 on: August 08, 2019, 10:40:40 PM »
Try this -- quick and dirty -- not bulletproof yet --

Code: [Select]
(defun _Get_File_Properties_Ex ( file / result shell name_space items item )

    ;;  See original post: http://tinyurl.com/yxa7ngfz
    ;;
    ;;  Get a file's extended properties (Title, Subject, Category ...)
    ;;  returning the result as a list of cons pairs:
    ;;
    ;;      (   ("Name" . "Drawing.dwg")
    ;;          ("Size" . "42 KB")
    ;;          ("Type" . "DWG File")
    ;;          ("Date Modified" . "2011/03/21 10:57 AM")
    ;;          ...
    ;;      )

    (vl-catch-all-apply
        (function
            (lambda ( / folder_name file_name i count )   
                (setq
                    folder_name (vl-filename-directory file)
                    file_name   (strcat (vl-filename-base file) (vl-filename-extension file))
                    shell       (vlax-create-object "Shell.Application")
                    name_space  (vlax-invoke shell 'NameSpace folder_name)
                    items       (vlax-invoke name_space 'Items)
                    item        (vlax-invoke name_space 'ParseName file_name)
                    i           (setq count 300)
                )
                (if item
                    (repeat count
                        (setq result
                            (cons
                                (cons
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        items
                                        (setq i (1- i))
                                    )
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        item
                                        i
                                    )
                                )   
                                result
                            )
                        )
                    )
                )   
            )               
        )       
    )
   
    (vl-catch-all-apply 'vlax-release-object (list item))
    (vl-catch-all-apply 'vlax-release-object (list items))
    (vl-catch-all-apply 'vlax-release-object (list name_space))
    (vl-catch-all-apply 'vlax-release-object (list shell))
   
    (vl-remove-if
        (function (lambda (item) (eq "" (car item))))
        result
    )   
   
)

Code: [Select]
(defun _Get_Image_Dimensions ( file )
    (   (lambda ( props foo bar keys ) (if props (mapcar 'bar keys)))
        (mapcar (function (lambda (p) (cons (strcase (car p)) (cdr p)))) (_Get_File_Properties_Ex file))
        (lambda (p) (cons (car p) (vl-string-subst "" "?" (cdr p))))
        (lambda (key) (foo (assoc key props)))
       '("HEIGHT" "WIDTH")
    )
)

(_Get_Image_Dimensions "H:\\Help\\mbicon.bmp")

(   ("HEIGHT" . "45 pixels")
    ("WIDTH" . "35 pixels")
)


Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

myloveflyer

  • Newt
  • Posts: 152
Re: Autolisp to get bitmap pixel values
« Reply #7 on: August 09, 2019, 04:05:47 AM »
Try this -- quick and dirty -- not bulletproof yet --

Code: [Select]
(defun _Get_File_Properties_Ex ( file / result shell name_space items item )

    ;;  See original post: http://tinyurl.com/yxa7ngfz
    ;;
    ;;  Get a file's extended properties (Title, Subject, Category ...)
    ;;  returning the result as a list of cons pairs:
    ;;
    ;;      (   ("Name" . "Drawing.dwg")
    ;;          ("Size" . "42 KB")
    ;;          ("Type" . "DWG File")
    ;;          ("Date Modified" . "2011/03/21 10:57 AM")
    ;;          ...
    ;;      )

    (vl-catch-all-apply
        (function
            (lambda ( / folder_name file_name i count )   
                (setq
                    folder_name (vl-filename-directory file)
                    file_name   (strcat (vl-filename-base file) (vl-filename-extension file))
                    shell       (vlax-create-object "Shell.Application")
                    name_space  (vlax-invoke shell 'NameSpace folder_name)
                    items       (vlax-invoke name_space 'Items)
                    item        (vlax-invoke name_space 'ParseName file_name)
                    i           (setq count 300)
                )
                (if item
                    (repeat count
                        (setq result
                            (cons
                                (cons
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        items
                                        (setq i (1- i))
                                    )
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        item
                                        i
                                    )
                                )   
                                result
                            )
                        )
                    )
                )   
            )               
        )       
    )
   
    (vl-catch-all-apply 'vlax-release-object (list item))
    (vl-catch-all-apply 'vlax-release-object (list items))
    (vl-catch-all-apply 'vlax-release-object (list name_space))
    (vl-catch-all-apply 'vlax-release-object (list shell))
   
    (vl-remove-if
        (function (lambda (item) (eq "" (car item))))
        result
    )   
   
)

Code: [Select]
(defun _Get_Image_Dimensions ( file )
    (   (lambda ( props foo bar keys ) (if props (mapcar 'bar keys)))
        (mapcar (function (lambda (p) (cons (strcase (car p)) (cdr p)))) (_Get_File_Properties_Ex file))
        (lambda (p) (cons (car p) (vl-string-subst "" "?" (cdr p))))
        (lambda (key) (foo (assoc key props)))
       '("HEIGHT" "WIDTH")
    )
)

(_Get_Image_Dimensions "H:\\Help\\mbicon.bmp")

(   ("HEIGHT" . "45 pixels")
    ("WIDTH" . "35 pixels")
)


Cheers.

MP, win10 CAD2020 test did not return results? :-(
Never give up !

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #8 on: August 09, 2019, 08:49:40 AM »
Sorry, not a lot to go on. Work inside out to determine the location of the failure (assuming you passed the correct path to a bitmap image), starting with the statement (vlax-create-object "Shell.Application").
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Autolisp to get bitmap pixel values
« Reply #9 on: August 09, 2019, 09:18:00 AM »
Quick test here and it worked fine:
Quote
_$

_GET_FILE_PROPERTIES_EX
_GET_IMAGE_DIMENSIONS
(("HEIGHT" . "1320 pixels") ("WIDTH" . "1020 pixels"))
; 3 forms loaded from #<editor "<Untitled-2> loading...">
_$

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #10 on: August 09, 2019, 09:20:42 AM »
Thanks for testing RJP. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Autolisp to get bitmap pixel values
« Reply #11 on: August 09, 2019, 10:58:37 AM »
Thanks for testing RJP. :)
Anytime :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Ben Clark

  • Newt
  • Posts: 94
Re: Autolisp to get bitmap pixel values
« Reply #12 on: August 09, 2019, 06:13:10 PM »
Worked for me also

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Autolisp to get bitmap pixel values
« Reply #13 on: August 09, 2019, 06:36:10 PM »
Cool, thx for letting me know.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

myloveflyer

  • Newt
  • Posts: 152
Re: Autolisp to get bitmap pixel values
« Reply #14 on: August 09, 2019, 09:56:17 PM »
Quick test here and it worked fine:
Quote
_$

_GET_FILE_PROPERTIES_EX
_GET_IMAGE_DIMENSIONS
(("HEIGHT" . "1320 pixels") ("WIDTH" . "1020 pixels"))
; 3 forms loaded from #<editor "<Untitled-2> loading...">
_$
I still tried unsuccessfully, "Parameter type error: (or stringp symbolp): nil"
Never give up !