Showing posts with label python3. Show all posts
Showing posts with label python3. Show all posts

Monday, September 4, 2023

Remove background from picture using command line

Removing a picture's background is sometimes a necessity, especially if you need the picture to integrate into other document or other picture. Here I will show how we can achieve that in linux using a python application called rembg.


First, make sure we have python installed. Most linux will have it installed by default, but just check to make sure
$ python -V






In order avoid installing python libraries globally, we will create a virtual environment to keep the rembg application files. You can read more about virtual environment, by clicking here. So we will create a virtual environment called rembg
$ python3 -m venv rembg

Activate the virtual environment
$ cd rembg
$ source bin/activate

Update pip
(rembg) $ pip install --upgrade pip

Install rembg[cli] package
(rembg) $ pip install rembg[cli]

Once installed, we can use "--help" to see what are the options available for rembg
$ rembg --help

Now we can use rembg to remove background. Let's say we have a picture called mypicture.jpg, and we want to remove its background, and save the new picture as mypicture-nobg.jpg:
(rembg) $ rembg i mypicture.jpg mypicture-nobg.jpg

Verify that the background has been removed in the new picture.

rembg also comes with http-server, for those who wanted a web interface to remove background from picture. Just run rembg with "s" flag to launch the rembg http server
$ rembg s

A web app will be launched on http://localhost:5000, where you can upload the file to be background removed, and click submit to get the output.

























Simply press "ctrl + c" to cancel rembg's http server, when you are done.

Thursday, August 31, 2023

Installing python's virtual environment in ubuntu linux

Python's virtual environment, is a way that developer can use to separate python's packages for different projects, in a single machine. The packages installed in this lightweight virtual environment, would be available in other virtual environment, thus removing the possibility of packages, org packages' version clashing inside a single machine.


First, we need to determine which python version is currently installed in our machine
$ python -V

 




Then, we need to install python-venv
$ sudo apt update && sudo apt install python3.10-venv -y

Once we have venv, we can start creating virtual environment. Let's say we wanted to have a virtual environment for a project called myproject, run this command
$ python3 -m venv myproject

The above command will create a directory called myproject, with necessary files in it. To activate the virtual environment, and start using it, we can run below commands
$ cd myproject
$ source bin/activate

How do we know that we are already inside the virtual environment? We will see the name of the virtual environment at our bash prompt, like below






We can now install any package that we need inside the virtual environment using pip, and that packages can only be used if we activate "myproject" virtual environment









To exit from the virtualenvironment, simply run deactivate command, and we will get back our standard bash prompt
$ deactivate


Sunday, December 24, 2017

Start a simple http server using python3

This is very simple, because python3 already has a built in module for http server, called http.server. To use this module, run:

$ python3 -m http.server 8000

whereby 8000 is the port you want the http server to run on. To use privileged ports (under 1024) , just append sudo to the above command, like below:

$ sudo python3 -m http.server 80

Once the command run, you can access the current directory via web, like below:



Once done, press Ctrl-c to terminate the server.