PiShrink an SD Card backup

I have a Windows PC ( laptop ) and needed to shrink the size of my 8Gb disk image file.

It is a known fact that not all SD cards of equal ‘size’ are actually the same identical size, so an img file of one card may not fit on another card.

This solution allows you to shrink the size of the primary partition in the SD card image file, and it will automatically expand itself to whatever size card you write the img file back on to.

So, in my case, the 8 Gb SD card is shrunk to 1.9 Gb.
When written to a 16 Gb SD card and loaded in the Pi, the partition automatically expands to fill the 16 Gb.
When written to a 4 Gb SD card and loaded in the Pi, the partition automatically expands to fill the 4 Gb.

The disk image file was created using Win32DiskImager on the windows pc.

I downloaded and installed VirtualBox on Windows

I downloaded the Ubuntu 17.10 iso disk image from here :

NOTE : do NOT use the version 16 – I tried that and wasted a day. Use the version 17 – just trust me on this.

Started VirtualBox
Add a New machine
Type : Linux
Version : Ubuntu (64 bit)
Set Memory to around 1500Mb
Create a Virtual Disk
File Location : Ubuntu1710 ( 7.10 is Ubuntu version that I downloaded )
File Size 20 Gb
Dynamically Allocated
Click on Settings
System – turn off the floppy disk
Display – set video memory to around 32Mb
Storage :
IDE change ’empty’ to ‘choose’
set the ISO storage to the uBuntu disk image that you downloaded
Network : change NAT to Bridged — important, otherwise you won’t be able to see your shared folders to transfer the files to and from Linux.

Start the Virtual Machine

It will start and begin the installation of Linux from the iso disk image

Follow the prompts
Install Ubuntu
Install Now
Time Zone – continue
English – continue
Your name ubpi
user ubpi
pass berry
login automatically – continue

Long wait while installation happens

on my PC, I have a network shared folder : F:\MyFiles\Documents

Use Win32DiskImager to ‘read’ the SD card to a local file in the shared folder ( in my case, the Documents folder )

The file size form my 8Gb SD card was 7.6 Gb

Save the following script lines to a txt file and save or copy in to the shared ( Documents ) folder.


Install pishrink ( if not already installed )
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

Run pishrink to shrink the img file :
sudo pishrink.sh 20180316_octopi_DaveOB.img


When Linux installation is complete, click on Restart

Remove the Installation medium ( need to unlink the iso installation disk image )
Choose Disk Image
Press enter to continue

Wait for the Linux desktop

Once loaded, we need to copy the txt file and the SD img file to the Linux machine :
on the left, go to Files
Other Locations
Windows Network
Dave_Laptop ( that’s my laptops name on the network – yours would be different )
Login with your windows username and windows login password
Documents ( or whatever you named the folder that you shared in Windows )
If asked for a keyring pass, enter : berry

You should now see the txt and the img files.

Right Click on the txt file and Click on Copy.
Then change location, on the left panel, to the Linux ‘Downloads’ folder.
Right Click and Paste

You should now have a copy of the txt file in the Linux Downloads folder.
Do the same process for the img file

NOTE : the 8Gb image file does take quite a while to copy. watch the progress bar that appears on top of the ‘files’ icon on the left panel. Wait for it to complete. Mine took a good 30 minutes to copy.

Next, you need to download and install the PiShrink app ( once off operation ).

Right Click on the txt file and open it in the default text reader / notepad
Find the line that starts with ‘wget’, highlight the line, right click and Copy.
Click on the File icon to display the file manager again.
Right Click on the blank area below the txt and img files, and select ‘Open in Terminal’
The terminal will open ( looks like a DOS prompt Cmd screen )
Right Click and Paste
The wget line will be pasted – hit Enter to execute the line.
Repeat this for each of the other 2 lines from the txt file.

Once done, you’re ready to shrink the img file.

from the txt file, highlight and copy the line
sudo pishrink.sh 20180316_octopi_DaveOB.img ( change the ‘20180316_octopi_DaveOB’ to the file name of your img file )
Press enter to execute the script.

Once it is done, close the Terminal window and refresh ( F5 ) the file manager window.
Right Click on the img file, properties, and you should see the new size of the img file.
Close the properties window.

Last thing is to copy the smaller img file back to your windows folder.
If you want to keep the original file, you can rename the smaller img file before copying it back.
Right Click on the file and Copy
On the left panel, go to the shared Documents folder, right Click and Paste.
Give it time to complete the copy process.

After you use Win32DiskImager to write the img file to your new SD card and insert in to the Pi and boot up, give it additional time for the first run, as it has to then expand the primary partition to fill the disk.


Changed the video feed brightness, contrast and settings for my Logitech USB video

Changed the video feed brightness, contrast and settings for my Logitech USB video feed to Octoprint control in my browser.


With the kind help of Fredrik Agert, I got this working perectly on my Pi and would like to share for others that may be looking for this solution.

Please feel free to add, update or comment if I have missed anything ( I have no Linux experience )

I am using Putty on Win 8 to open an SSH Terminal window to the Pi.

Current setup is :
Octoprint 1.3.6
OctoPi 0.14.0

Connect to the Pi using the SSH Terminal window ( Putty ).

First step was to install uvcdynctrl in the Pi with the following terminal command :
sudo apt-get install uvcdynctrl

Next, try a manual change to the video settings with :
uvcdynctrl -s ‘Brightness’ 55 ‘Contrast’ 15 ‘Saturation’ 80 ‘Sharpness’ ‘Focus, Auto’

The changes should show immediately in the Octoprint video feed.

Adjust the values to get the picture quality that you want.
My settings ( due to a very bright background ) are :
uvcdynctrl -s ‘Brightness’ 150 ‘Contrast’ 35 ‘Saturation’ 50 ‘Sharpness’ ‘Focus, Auto’

Next, create the script file that will run and change the settings :

create the pi directory if it does not exist
mkdir -p /home/pi

create the bin directory if it does not exist
mkdir -p /home/pi/bin

start the Pi text editor ( nano ) using :
nano /home/pi/bin/cam_init.sh

In that script file, paste or type the settings line ( with your choice of values ) :
uvcdynctrl -s ‘Brightness’ 55 ‘Contrast’ 15 ‘Saturation’ 80 ‘Sharpness’ ‘Focus, Auto’

ctrl-X to close and Y to Save the file, and Enter to commit the Save.

Next, you need to give that file permission to run as an executable file with :
chmod a+x /home/pi/bin/cam_init.sh

Now you need to tell the Server when to run that script file.
I could not get this to work using the ‘Startup’ event, but it works perfectly using the ‘ClientOpened’ event, which occurs every time you connect to the Server from your PCs web browser.

Edit the config file ( WARNING : I was advised to NOT do this when a print job is running ) :
nano /home/pi/.octoprint/config.yaml

and add to the bottom of the file :

  - event: ClientOpened
    command: /home/pi/bin/cam_init.sh
    type: system

( aparently the spaces at the start of each line are important )

This tells the Server for every time the ‘ ClientOpened ‘ event occurs ( you connected to the Server ), to run the script file cam_init.sh

Restart the Pi and once your server is running, and you connect from your browser, the video feed settings will be changed.

If you want to change the settings, go back to :
sudo nano /home/pi/bin/cam_init.sh
and edit the settings.
Save the file, then close the browser tab for Octoprint and open a new tab – the ClientOpened event is triggered and the new settings will be in place.