Python Rembg (solved)

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Python Rembg (solved)

Post by 2themoon »

Hello people

I am trying to run a simple task in python. rembg (remove background)
the task doesnt run .. I get syntax errors. I am not sure about the syntax for the paths that I am using.
I also dont know how this pil or pillow thing works.

the original looks like this:

Code: Select all

from rembg import remove
from PIL import Image

input_path = 'input.jpg' # input image path
output_path = 'output.png' # output image path

input = Image.open(input_path) # load image
output = remove(input) # remove background
output.save(output_path) # save image
these are the lines i enter in python"

Code: Select all

 from rembg import remove
from PIL import Image


input_path = '/home/rocket/Pictures/Morts.png' # input image path
output_path = '/home/rocket/Pictures/' # output image path

input = Image.open(/home/rocket/Pictures/Morts.png) # load image
output = remove(/home/rocket/Pictures/Morts.png) # remove background
output.save(/home/rocket/Pictures/mortsbg.png) # save image
this is the feedback:

Code: Select all

rocket@rocket-Latitude-E6520:~$ python
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rembg import remove
>>> from PIL import Image
>>> 
>>> 
>>> input_path = '/home/rocket/Pictures/Morts.png' # input image path
>>> output_path = '/home/rocket/Pictures/' # output image path
>>> 
>>> input = Image.open(/home/rocket/Pictures/Morts.png) # load image
  File "<stdin>", line 1
    input = Image.open(/home/rocket/Pictures/Morts.png) # load image
                       ^
SyntaxError: invalid syntax
>>> output = remove(/home/rocket/Pictures/Morts.png) # remove background
  File "<stdin>", line 1
    output = remove(/home/rocket/Pictures/Morts.png) # remove background
                    ^
SyntaxError: invalid syntax
>>> output.save(/home/rocket/Pictures/mortsbg.png) # save image
  File "<stdin>", line 1
    output.save(/home/rocket/Pictures/mortsbg.png) # save image
                ^
SyntaxError: invalid syntax
>>> 
the original photo is in this folder:
/home/rocket/Pictures/Morts.png

so as you can see its running in python 3.8 I dont know how pil or pillow works. I have installed pillow.

Are my script lines incorrect .. pls show correct entries.

system report:

Code: Select all

System:    Kernel: 5.4.0-171-generic x86_64 bits: 64 compiler: gcc v: 9.4.0 
           Desktop: Cinnamon 4.6.7 wm: muffin dm: LightDM Distro: Linux Mint 20 Ulyana 
           base: Ubuntu 20.04 focal 
Machine:   Type: Laptop System: Dell product: Latitude E6520 v: 01 serial: <filter> Chassis: 
           type: 9 serial: <filter> 
           Mobo: Dell model: 0J4TFW v: A01 serial: <filter> BIOS: Dell v: A06 date: 07/11/2011 
Battery:   ID-1: BAT0 charge: 43.2 Wh condition: 43.2/48.8 Wh (89%) volts: 12.6/11.1 
           model: SMP DELL P8TC727 serial: <filter> status: Full 
CPU:       Topology: Dual Core model: Intel Core i5-2540M bits: 64 type: MT MCP arch: Sandy Bridge 
           rev: 7 L2 cache: 3072 KiB 
           flags: avx lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx bogomips: 20755 
           Speed: 3093 MHz min/max: 800/3300 MHz Core speeds (MHz): 1: 3093 2: 3217 3: 3093 
           4: 3196 
Graphics:  Device-1: Intel 2nd Generation Core Processor Family Integrated Graphics vendor: Dell 
           driver: i915 v: kernel bus ID: 00:02.0 chip ID: 8086:0126 
           Device-2: NVIDIA GF119M [NVS 4200M] vendor: Dell driver: nvidia v: 390.157 
           bus ID: 01:00.0 chip ID: 10de:1056 
           Display: x11 server: X.Org 1.20.13 driver: modesetting,nvidia 
           unloaded: fbdev,nouveau,vesa resolution: 1600x900~60Hz, 1920x1080~60Hz 
           OpenGL: renderer: NVS 4200M/PCIe/SSE2 v: 4.6.0 NVIDIA 390.157 direct render: Yes 
Audio:     Device-1: Intel 6 Series/C200 Series Family High Definition Audio vendor: Dell 
           driver: snd_hda_intel v: kernel bus ID: 00:1b.0 chip ID: 8086:1c20 
           Device-2: NVIDIA GF119 HDMI Audio vendor: Dell driver: snd_hda_intel v: kernel 
           bus ID: 01:00.1 chip ID: 10de:0e08 
           Sound Server: ALSA v: k5.4.0-171-generic 
Network:   Device-1: Intel 82579LM Gigabit Network vendor: Dell driver: e1000e v: 3.2.6-k 
           port: 5080 bus ID: 00:19.0 chip ID: 8086:1502 
           IF: eno1 state: down mac: <filter> 
           Device-2: Intel Centrino Ultimate-N 6300 driver: iwlwifi v: kernel port: 4000 
           bus ID: 03:00.0 chip ID: 8086:422b 
           IF: wlp3s0 state: up mac: <filter> 
Drives:    Local Storage: total: 465.76 GiB used: 46.01 GiB (9.9%) 
           ID-1: /dev/sda vendor: Toshiba model: MK5061GSYN size: 465.76 GiB speed: 3.0 Gb/s 
           serial: <filter> 
RAID:      Hardware-1: Intel 82801 Mobile SATA Controller [RAID mode] driver: ahci v: 3.0 
           bus ID: 00:1f.2 chip ID: 8086.282a 
Partition: ID-1: / size: 76.35 GiB used: 46.01 GiB (60.3%) fs: ext4 dev: /dev/sda5 
USB:       Hub: 1-0:1 info: Full speed (or root) Hub ports: 3 rev: 2.0 chip ID: 1d6b:0002 
           Hub: 1-1:2 info: Intel Integrated Rate Matching Hub ports: 6 rev: 2.0 
           chip ID: 8087:0024 
           Device-1: 1-1.4:3 info: Dell DW375 Bluetooth Module type: Bluetooth driver: btusb 
           rev: 2.0 chip ID: 413c:8187 
           Hub: 2-0:1 info: Full speed (or root) Hub ports: 3 rev: 2.0 chip ID: 1d6b:0002 
           Hub: 2-1:2 info: Intel Integrated Rate Matching Hub ports: 8 rev: 2.0 
           chip ID: 8087:0024 
           Device-2: 2-1.4:3 info: Logitech M105 Optical Mouse type: Mouse 
           driver: hid-generic,usbhid rev: 2.0 chip ID: 046d:c077 
           Device-3: 2-1.8:4 
           info: Broadcom BCM5880 Secure Applications Processor with fingerprint swipe sensor 
           type: Smart Card driver: N/A rev: 1.1 chip ID: 0a5c:5801 
Sensors:   System Temperatures: cpu: 67.0 C mobo: 31.0 C sodimm: 41.0 C gpu: nvidia temp: 60 C 
           Fan Speeds (RPM): cpu: 2676 
Repos:     No active apt repos in: /etc/apt/sources.list 
           Active apt repos in: /etc/apt/sources.list.d/brave-browser-release.list 
           1: deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg] https: //brave-browser-apt-release.s3.brave.com/ stable main
           Active apt repos in: /etc/apt/sources.list.d/official-package-repositories.list 
           1: deb http: //packages.linuxmint.com ulyana main upstream import backport #id:linuxmint_main
           2: deb http: //archive.ubuntu.com/ubuntu focal main restricted universe multiverse
           3: deb http: //archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
           4: deb http: //archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
           5: deb http: //security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
           6: deb http: //archive.canonical.com/ubuntu/ focal partner
           Active apt repos in: /etc/apt/sources.list.d/opera-stable.list 
           1: deb https: //deb.opera.com/opera-stable/ stable non-free #Opera Browser (final releases)
Info:      Processes: 245 Uptime: 3h 20m Memory: 7.64 GiB used: 1.91 GiB (25.0%) Init: systemd 
           v: 245 runlevel: 5 Compilers: gcc: 9.4.0 alt: 9 Client: Unknown python3.8 client 
           inxi: 3.0.38                                                                  ----------------------------------
            --------------                                                                
should I remove and reinstall python.

kind regards all .. have a great day
Last edited by 2themoon on Mon Mar 11, 2024 5:53 am, edited 2 times in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Python Rembg

Post by xenopeek »

This is a syntax error, as indicated in the feedback:

Code: Select all

Image.open(/home/rocket/Pictures/Morts.png)
You must quote literal strings. So:

Code: Select all

Image.open('/home/rocket/Pictures/Morts.png')
Do that for all path arguments, quote them. Or use the variables. These lines are now useless because you replaced the variables in the Image method calls with paths:

Code: Select all

input_path = '/home/rocket/Pictures/Morts.png' # input image path
output_path = '/home/rocket/Pictures/' # output image path
Image
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: Python Rembg

Post by dave0808 »

Or put another way: the original script defines 2 variables, one for the input filename and one for the output. The remainder of the script makes use of those variables.

In your modified script, you've changed the variables AND the remaining code that makes use of those variables. And as xenopeek points out, in those last 3 lines you've not used any quotes, presumably because the original script didn't.

If I were you, I'd revert back to the original script, and then just change the 2 variables: input_path and output_path.

Take note that originally you only put in a path name for output_path but it looks like the script requires a full filename.
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Re: Python Rembg

Post by 2themoon »

I get it thanks ... one of the things i wasnt sure of is if the path includes the actual picture or if it is path to the picture directory.
Will give it a go. this cant be that difficult .. lol
I dont know coding or at least i know very basics.

thks .. I wil give it a go and close this if it all works. otherwise there might be future post here.
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Re: Python Rembg

Post by 2themoon »

>>> from rembg import remove
>>> from PIL import Image
>>>
>>>
>>> input_path = '/home/rocket/Pictures/Morts.png' # input image path
>>> output_path = '/home/rocket/Pictures/Mortsbg.png' # output image path
>>>
>>> input = Image.open('/home/rocket/Pictures/Morts.png') # load image
>>> output = remove('/home/rocket/Pictures/Mortsbg.png') # remove background
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/rocket/.local/lib/python3.8/site-packages/rembg/bg.py", line 247, in remove
raise ValueError("Input type {} is not supported.".format(type(data)))
ValueError: Input type <class 'str'> is not supported.
>>> output.save('/home/rocket/Pictures/mortsbg.png') # save image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'output' is not defined
>>>

well i got the rembg.py to show itself ... as you can see now there is a format type data entry .... not supported.
maybe if the python string actually works then the output might also work.
i did an install for rembg. how do I see if it is actually there and in the right place.

the picture before is Morts.png then rembg and output the picture to the same directory but now called Mortsbg.png
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Re: Python Rembg

Post by 2themoon »

rocket@rocket-Latitude-E6520:~$ rembg --help
Usage: rembg [OPTIONS] COMMAND [ARGS]...

Options:
--version Show the version and exit.
--help Show this message and exit.

Commands:
b for a byte stream as input
d download all models
i for a file as input
p for a folder as input
s for a http server
rocket@rocket-Latitude-E6520:~$
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Python Rembg

Post by xenopeek »

Code: Select all

from rembg import remove
from PIL import Image

input_path = '/home/rocket/Pictures/Morts.png'
output_path = '/home/rocket/Pictures/Mortsbg.png'

input = Image.open(input_path)
output = remove(input)
output.save(output_path)
That's a copy of the example from https://github.com/danielgatis/rembg with just the paths changed -- that's all you should change.

the rembg.remove method doesn't take a path as argument, it takes a PIL image. So you have to pass it the PIL image you opened.
Image
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Re: Python Rembg

Post by 2themoon »

It will be easy they said, just seven lines.

found a way to reduce it to one line

kiss my axe I say. now it says onnxruntime not found.

rocket@rocket-Latitude-E6520:~$ rembg p '/home/rocket/pictures/b4' '/home/rocket/pictures/after'
Traceback (most recent call last):
File "/home/rocket/.local/bin/rembg", line 5, in <module>
from rembg.cli import main
File "/home/rocket/.local/lib/python3.8/site-packages/rembg/__init__.py", line 5, in <module>
from .bg import remove
File "/home/rocket/.local/lib/python3.8/site-packages/rembg/bg.py", line 21, in <module>
from .session_factory import new_session
File "/home/rocket/.local/lib/python3.8/site-packages/rembg/session_factory.py", line 4, in <module>
import onnxruntime as ort
ModuleNotFoundError: No module named 'onnxruntime'
2themoon
Level 1
Level 1
Posts: 26
Joined: Fri May 12, 2023 6:53 am

Re: Python Rembg

Post by 2themoon »

Yay now it works.

after using timeshift to restore system I then installed python .. i had to install pip then pillow and then rembg you have to install.
nobody tells you that you need onnxruntime. this one is a problem child. i got the following terminal input line off the web

tools/ci_build/github/linux/copy_strip_binary.sh -r build/Linux -a onnxruntime -l libonnxruntime.so.1.9.1 -c RelWithDebInfo -s . -t 2a96b73a1afa9aaafb51
followed by ... pip install onnxruntime==1.8.1 if i try 1.9.1 i get errors

i have no idea if this is the latest onnx but it works ... being a microsoft thing i kkknow nothing about onnx

script input is straightforward now.

rembg i /home/rocket/pictures/b4/Morts.png /home/rocket/pictures/after/mortsbg.jpg
for some reason the conversion from png to jpg gives a size error but i can probably fix that in Gimp.

i can close this now
thanks
Post Reply

Return to “Programming & Development”