kloeckner.com.ar

Backup of part of my webpage
Index Commits Files Refs README LICENSE
blog/imagemagick-use-case-process-ltspice-schematic-images/imagemagick-use-case-process-ltspice-schematic-images.md (6202B)
   1 %%
   2 title: "ImageMagick Use Case: Process LTspice Schematic Images"
   3 date: "19-Nov-2024"
   4 %%
   5 
   6 # ImageMagick Use Case: Process LTspice Schematic Images
   7 
   8 In this post I explain how I managed to simplify to a 'one-liner' the series of
   9 steps involved in exporting and processing the image of an LTSpice schematic for
  10 further use in a report paper or any similar document.
  11 
  12 > ***TL;DR*** 
  13 > 
  14 > Copy to clipboard the current view of the schematic in LTSpice: `right click >
  15 > view > copy image to clipboard`, then, using xclip, pipe the content of the
  16 > clipboard to magick and save the image with name `schematic.png`, in black and
  17 > white (`-colorspace grey`) and cropped to the contents of the image (`-trim`)
  18 > 
  19 > ```console
  20 > $ xclip -selection clipboard -t image/bmp -o | magick - -colorspace grey -trim schematic.png
  21 > ```
  22 > <!--`-->
  23 
  24 ## Getting an image of the schematic from LTSpice
  25 
  26 First we copy the current schematic view in LTSpice to the clipboard with:
  27 `right click on an empty space of the schematic > view > copy image to
  28 clipboard`.
  29 
  30 ![Copying the image to clipboard from LTSpice](copy_to_clipboard_cropped2.png)
  31 
  32 Others may just take a screenshot of the schematic, this is also a valid way of
  33 getting an image of the schematic but as we'll see later it's more practical to
  34 just copy the image to clipboard since it doesn't require the creation of a new
  35 temporary file.
  36 
  37 ## Previous Procedure
  38 
  39 <!--
  40 First we copy the current schematic view of LTSpice to clipboard with: `right
  41 click on an empty space of the schematic > view > copy image to clipboard`.
  42 
  43 ![Copying the image to clipboard from LTSpice](copy_to_clipboard_cropped2.png)
  44 
  45 Other may just take a screenshot of the schematic, this is also a valid way of
  46 getting an image of the schematic but in my experience it's better, and simpler,
  47 to copy the image to clipboard
  48 -->
  49 
  50 After copying the image to the clipboard from LTSpice as seen in the previous
  51 section, just using a GUI program like gimp, paste the image from the clipboard,
  52 crop the image, apply black and white filters, etc; finally export the image.
  53 
  54 This procedure looks simple, but when doing it with multiple schematics it turns
  55 tedious. In the current procedure the steps after copying the image shrinks to a
  56 one-liner (a single line containing one or more chained commands).
  57 
  58 ## Current Procedure
  59 
  60 <!--
  61 Copying the schematic image from LTSpice is the same as the previous way, the
  62 steps that change are the following ones which involve getting the image from
  63 the clipboard, processing it and finally exporting it to a file. In this new
  64 procedure we use a series of chained command line commands to obtain that.
  65 -->
  66 
  67 Instead of using a GUI program after copying the image to the clipboard from
  68 LTSpice, we use a series of command line tools to process and save the image, in
  69 particular, xclip to manage the clipboard contents (X session) and ImageMagick
  70 to process the image.
  71 
  72 ### Getting the image from the clipboard
  73 
  74 To get the image from the clipboard we use xclip (xclip works for an X session,
  75 if you are using Wayland you may use [wl-clipboard](https://github.com/bugaevc/wl-clipboard)
  76 which it may have similar commands to the ones seen on this post).
  77 
  78 <!--
  79 Then we can query the format of the copied image by specifying the `TARGETS`
  80 target to xclip, as follows:
  81 
  82 After copying the image from LTSpice, you may query the supoorted image formats
  83 of that image, you can do that 
  84 -->
  85 
  86 After copying the image from LTSpice, you may query the supported image formats
  87 of the clipboards contents, you can do that by providing the `TARGETS` to the
  88 target flag `-t` as show next.
  89 
  90 ```console
  91 $ xclip -selection clipboard -t TARGETS -o
  92 PIXMAP
  93 image/bmp
  94 TARGETS
  95 MULTIPLE
  96 TIMESTAMP
  97 ```
  98 
  99 Notice that we provide the `-selection clipboard` flag, this selects the
 100 clipboard named `clipboard` (there is also `primary` and `secondary`). In this
 101 case we use LTSpice running on wine so it may use a different clipboard for
 102 other installations.
 103 
 104 Specifying the `image/bmp` target to xclip and no filename to the output flag
 105 `-o`, will result in xclip printing the binary data to standard output (this
 106 will probably mess the terminal). By piping the binary data to a file, as shown
 107 next, we can make sure if the data is a bitmap formatted image.
 108 
 109 ```console
 110 $ xclip -selection clipboard -t image/bmp -o > schematic_raw
 111 ```
 112 
 113 Executing the `file` command on the new file `schematic_raw` reveals that
 114 effectively it is a bitmap file.
 115 
 116 ```console
 117 $ file schematic_raw
 118 schematic_raw: PC bitmap, Windows 3.x format, 1916 x 966 x 32, 3 compression, image size 7403424, cbSize 7403490, bits offset 66
 119 ```
 120 
 121 The raw schematic image, in bitmap format, looks exactly as shown in the LTSpice
 122 schematic view.
 123 
 124 ![Copied Image](schematic_raw.png)
 125 
 126 The next step is to convert it to black and white, trim the white margins and
 127 finally convert it to png format.
 128 
 129 ### Processing the image using ImageMagick
 130 
 131 Instead of piping the output of the xclip command to a file, we pipe the binary
 132 data to ImageMagick appending a single dash `-` to tell magick to read from
 133 standard input. To apply the desired effects on the image we also append the
 134 flag `-colorspace grey`, to make the image black and white, the flag `-trim`,
 135 to crop the image to the content, and finally we save the resulting image in png
 136 format by appending the extension to the output file name (for example
 137 `schematic.png`).
 138 
 139 ```console
 140 $ xclip -selection clipboard -t image/bmp -o | magick - -colorspace grey -trim schematic.png
 141 ```
 142 
 143 The final image results as follows
 144 
 145 ![Resulting Image](schematic.png)
 146 
 147 ImageMagick can also read from a file, for example if you took a screenshot of
 148 the schematic, you can process it by specifying the file name as input, instead
 149 of reading from standard input.
 150 
 151 ```console
 152 $ magick schematic_screenshot.png -colorspace grey -trim schematic.png
 153 ```
 154 
 155 <!--
 156 Simply pipe the output of the clipboard to ImageMagick and add the flag
 157 `-colorspace grey`, to make the image black and white, and the flag `-trim` to
 158 crop the image to the content.
 159 
 160 output of the magick command is the following image, which is in black and
 161 white plus the margins are cropped, resulting in an image ready for inserting in
 162 a paper.
 163 -->