<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aaradhya Vaze</title>
    <description>The latest articles on DEV Community by Aaradhya Vaze (@vez).</description>
    <link>https://dev.to/vez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F428503%2Fcaaae807-1deb-4505-8e87-176db9153ba0.jpeg</url>
      <title>DEV Community: Aaradhya Vaze</title>
      <link>https://dev.to/vez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vez"/>
    <language>en</language>
    <item>
      <title>shirt color = desktop background in 10 lines of code</title>
      <dc:creator>Aaradhya Vaze</dc:creator>
      <pubDate>Tue, 14 Jul 2020 12:03:50 +0000</pubDate>
      <link>https://dev.to/vez/shirt-color-desktop-background-in-10-lines-of-code-b22</link>
      <guid>https://dev.to/vez/shirt-color-desktop-background-in-10-lines-of-code-b22</guid>
      <description>&lt;p&gt;here's a shower-thought:&lt;/p&gt;

&lt;p&gt;imagine firing up your laptop, and the background changes to your dress color! and it actually ended up being doable in Ubuntu (18.04) but this should hopefully work fine in any Linux distribution. &lt;/p&gt;

&lt;p&gt;And boy, does it put a smile on my face &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/437604553" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;the idea is straightforward : make the webcam click a picture, crop to where a shirt generally is (in a picture), find the dominant color in it and set it to the background.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Make the webcam click a picture&lt;br&gt;
i use mplayer for this, and i've found vlc and ffmpeg to work fine as well. Here's the one liner for mplayer that clicks three pictures:&lt;br&gt;
&lt;code&gt;mplayer tv:// -tv driver=v4l2:device=/dev/video0:width=100:height=100 -frames 3 -vo jpeg:outdir=/home/vaze/vez&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This puts three images, by the name 00000001.jpg, 00000002.jpg, 00000003.jpg in the specified &lt;strong&gt;outdir&lt;/strong&gt; directory. Sweet!&lt;br&gt;
(Replace the &lt;code&gt;/home/vaze/vez&lt;/code&gt; with your directory name)&lt;/p&gt;

&lt;p&gt;-frames is set to 3 because it gives the camera enough time to start and not mess up the lighting. Experiment with the width and height as well; i set it to a hundred but i think the final image still had a size of 176x144, so that's probably a limit or something&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Crop to where the shirt is&lt;br&gt;
i've done this in Python, using a library called Pillow. Here's the &lt;a href="https://pillow.readthedocs.io/en/stable/installation.html"&gt;Installation guide&lt;/a&gt;&lt;br&gt;
It makes handling images super easy. Let's leave this step here for now, we'll come back to it in a moment&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Find the dominant color in the image&lt;/p&gt;

&lt;p&gt;This is the interesting bit! there are so many ways you could approach this, depending on what kind of result you want.&lt;br&gt;
i resized the cropped image (which consisted only the shirt color) just to &lt;em&gt;one&lt;/em&gt; pixel (again, using Pillow), and that method approximated the true color real quick. More ideas from &lt;a href="https://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image"&gt;this&lt;/a&gt; nice StackOverflow answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Setting an image to the the background&lt;/p&gt;

&lt;p&gt;this command takes the file name(the three '///' is important) as input and sets it to the background. I use gnome, so that's the name of my desktop background. If you use, say Mint, replace the 'gnome' with 'cinnamon' and so on.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gsettings set org.gnome.desktop.background picture-uri file:///home/vaze/vez/back.jpg&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Cool, we have the pieces ready. Let's put them together in a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/python3
from PIL import Image;
import subprocess as s

com = 'mplayer tv:// -tv driver=v4l2:device=/dev/video0:width=100:height=100 -frames 3 -vo jpeg:outdir=/home/vaze/vez'
comb = 'gsettings set org.gnome.desktop.background picture-uri file:///home/vaze/vez/back.jpg'
path = '/home/vaze/vez/00000001.jpg'
save = '/home/vaze/vez/back.jpg'

s.call(com, shell=True)
im = Image.open(path)
w, h = im.size
im = im.crop((50, h - 10, w-50, h)).resize((1, 1), resample=0)
im.save(save)
s.call(comb, shell=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's go over this line by line:&lt;br&gt;
&lt;code&gt;#!/usr/bin/python3&lt;/code&gt;&lt;br&gt;
this is important as we're soon going to be making this file &lt;em&gt;executable&lt;/em&gt;. The kernel needs to know what language to execute the script in (python3 in this case, the she-bang is some kind of a 'hey look! the language to read this in is : ')&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import subprocess as s&lt;/code&gt;&lt;br&gt;
the subprocess module can be used to call terminal commands right from python, using &lt;code&gt;subprocess.call("command goes here")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;s.call(com, shell=True)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;first we're calling the command 'com' (i assigned it to the mplayer-fire-the-webcam thingy)&lt;br&gt;
Also commands didn't work without the &lt;code&gt;shell=True&lt;/code&gt; bit and i'm not quite sure why yet.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;im = Image.open(path)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On this next line, i import the image that was just clicked (i can do that because i know what name it will be saved with. neat!)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;w, h = im.size&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;sets the width of the image to the variable w and height to h&lt;/p&gt;

&lt;p&gt;&lt;code&gt;im = im.crop((50, h - 10, w-50, h)).resize((1, 1), resample=0)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we crop it with the tuple &lt;code&gt;(50, h-10, w-50, h)&lt;/code&gt; which corresponds to top left corner and bottom right corner of whatever region you're cropping in pixels. This seemed to work in the location i sit it,for my shirt is generally in that region, but if you're a fan of hats and want the background to be your hat color, crop to the top bit :)&lt;/p&gt;

&lt;p&gt;next we chain it with the resize method, and make it one single pixel, using the nearest neighbour filter (resample = 0 sets it to this (that is because it accepts a whole bunch of &lt;a href="https://kite.com/python/docs/PIL.Image.Image.resize"&gt;filters&lt;/a&gt;, feel free to experiment!)).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;im.save(save)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;save this image to wherever you want to, i saved it in the same directory as this script&lt;/p&gt;

&lt;p&gt;&lt;code&gt;s.call(comb, shell=True)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;finally, we call the background-setter code! (see that the 'comb' variable is set to the same location as the 'save')&lt;/p&gt;

&lt;p&gt;now all that's left to do is to make this executable. First save it as some file (say script.py) and go into the terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd directory_where_the_script_is_saved
chmod +x script.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now anytime you want to use this script, go in the terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./directoryName/script.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;i made a handy alias for this (as seen in the video) called color, and you can make this by going into the &lt;code&gt;.bash_aliases&lt;/code&gt; file and adding this line: (note the dot)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano .bash_aliases
(text editor opens up, go to the last line and type: )
alias color='./directoryName/script.py'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can make this script run automatically on boot very easily in Ubuntu:&lt;/p&gt;

&lt;p&gt;Go to Startup Applications (just type it out in the app drawer) and put our &lt;code&gt;./directoryName/script.py&lt;/code&gt; in the section for adding commands.(i'm not sure why but inserting the alias wasn't working)&lt;br&gt;
like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw97fvlkxzbb5adp8ybo5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw97fvlkxzbb5adp8ybo5.png" alt="startup script" width="668" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There we have it! on boot-up your webcam should wink with a little flash and dress itself like you :)&lt;/p&gt;

</description>
      <category>linux</category>
      <category>python</category>
    </item>
  </channel>
</rss>
