I was interested in creating a .img disk image for the SD card on the pi so that I could distribute my custom software to multiple devices quickly (by writing the image to all my SD cards).

Here’s how to accomplish this using Ubuntu.

  1. Before you create the image, shrink your raspberry pi’s existing partitions as much as possible to reduce the .img size (shrink most free space).
  2. Take the SD card you want to image and put it in a card reader. Plug it into your computer.
  3. Run gparted (sudo gparted) and look for the device. It should be in a dropdown that has /dev/sda, /dev/sdb, …, /dev/sdX. options. You want to find the device that has your image.
  4. After selecting the correct device, right click the last partition you want to copy and find the Last sector number. Copy it.
  5. Run: sudo dd if=/dev/<sdx> of=<desired path of your new .img file> bs=512 count=<last sector number copied from gparted> Here’s an example: sudo dd if=/dev/sde of=/home/samliu/sammyprinter.img bs=512 count=5838847
  6. Ubuntu has a handy utility installed called “Startup Disk Creator”. You can load the image there and choose a SD card plugged into your machine to write it to. EDIT September 2022: “Raspberry Pi Imager” also supports custom .img writing.

Optional: once you boot into raspbian using the newly created card, you can re-expand the partitions to full size by using sudo raspi-config and the “Expand Filesystem” option.