mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-21 02:10:11 +08:00
commit
c611936db1
@ -1,108 +0,0 @@
|
||||
[#]: subject: "Remove the background from an image with this Linux command"
|
||||
[#]: via: "https://opensource.com/article/23/4/image-editing-linux-python"
|
||||
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Remove the background from an image with this Linux command
|
||||
======
|
||||
|
||||
You have a great picture of yourself and want to use it for your social media profile, but the background is distracting. Another picture has a perfect background for your profile picture. How do you combine the two? Some smartphone apps that do this kind of photo manipulation but are too expensive or are riddled with ad-ware. And they aren't open source. [Rembg][1] is up to the challenge!
|
||||
|
||||
Rembg is written in Python, so install Python 3 on your computer. Most Linux distributions include Python 3 by default. You can check your version with this simple command:
|
||||
|
||||
```
|
||||
$ python3 --version
|
||||
```
|
||||
|
||||
Rembg requires at least Python 3.7 and no greater than Python 3.11. In my case, I have Python 3.10.6 installed.
|
||||
|
||||
### Install Rembg on Linux
|
||||
|
||||
I created a directory called `PythonCoding` on my Linux laptop and then made a Python virtual environment:
|
||||
|
||||
```
|
||||
$ python3 -m venv /home/don/PythonCoding
|
||||
```
|
||||
|
||||
Next, I installed `rembg` using `pip`:
|
||||
|
||||
```
|
||||
$ python3 -m pip install rembg
|
||||
```
|
||||
|
||||
### Combine images
|
||||
|
||||
Time to work some magic. First, I chose the image containing a picture taken at All Things Open in 2019.
|
||||
|
||||
![Don Watkins at All Things Open conference][2]
|
||||
|
||||
I ran the following `rembg` command to rename it with a shorter filename for convenience:
|
||||
|
||||
```
|
||||
$ rembg i dgw_ato.jpeg dgw_noback.jpg
|
||||
```
|
||||
|
||||
The first time you run `rembg`, it downloads an open source [pattern recognition model][3]. This can be over 100 MB and `rembg` saves it in your user directory as `~/.u2net/u2net.onnx`. The model is the U-2-Net and uses the Apache 2.0 license. For more information about the pattern recognition models (including how to train your own), read the Rembg documentation.
|
||||
|
||||
It created my new photo without the background in about ten seconds. I have a Ryzen 7 with 16 GB of RAM. Your experience may vary depending on your hardware.
|
||||
|
||||
![A processed image of Don Watkins, with the background removed by Rembg.][4]
|
||||
|
||||
I have used [GIMP][5] to remove backgrounds in the past, but `rembg` does it quicker and more completely than I have experienced with GIMP.
|
||||
|
||||
That's all there is to removing a background. What about adding a new one?
|
||||
|
||||
### Add a new background
|
||||
|
||||
Next, I want to add a new background to the picture. There are different ways to do that. You can, for instance, combine images with [ImageMagick][6], but getting the frame size right can be complex. The easiest way is to use GIMP or [Krita][7].
|
||||
|
||||
I used GIMP. First, open the newly created image (`ato_image.jpg` in my case). Now go to the **File** menu and select **Open as layers**. Choose a different image for the background. This image opens as an overlay above the existing photo.
|
||||
|
||||
I wanted to move the new background below my portrait. On the right of the GIMP window are two thumbnails, one for each image layer. The background layer is on top. I dragged the background layer beneath my portrait image, and here's the result:
|
||||
|
||||
![Don Watkins with a new background.][8]
|
||||
|
||||
That's a much nicer setting for my profile picture!
|
||||
|
||||
### Try Rembg
|
||||
|
||||
Rembg has three subcommands you can review in the `--help` menu:
|
||||
|
||||
```
|
||||
$ rembg --help
|
||||
```
|
||||
|
||||
They are:
|
||||
|
||||
- `rembg i` for files
|
||||
- `rembg p` for folders
|
||||
- `rembg s` for HTTP server
|
||||
|
||||
Rembg is released with an [MIT][9] license. Try it the next time you need a background removed from an image.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/4/image-editing-linux-python
|
||||
|
||||
作者:[Don Watkins][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/don-watkins
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://github.com/danielgatis/rembg
|
||||
[2]: https://opensource.com/sites/default/files/2023-03/don-watkins-at-All-Things-Open.webp
|
||||
[3]: https://github.com/xuebinqin/U-2-Net
|
||||
[4]: https://opensource.com/sites/default/files/2023-03/don-watkins-no-background.webp
|
||||
[5]: https://opensource.com/content/cheat-sheet-gimp
|
||||
[6]: https://opensource.com/article/17/8/imagemagick
|
||||
[7]: https://opensource.com/article/21/12/open-source-photo-editing-krita
|
||||
[8]: https://opensource.com/sites/default/files/2023-03/don-watkins-at-the-beach.webp
|
||||
[9]: https://github.com/danielgatis/rembg/blob/main/LICENSE.txt
|
@ -0,0 +1,109 @@
|
||||
[#]: subject: "Remove the background from an image with this Linux command"
|
||||
[#]: via: "https://opensource.com/article/23/4/image-editing-linux-python"
|
||||
[#]: author: "Don Watkins https://opensource.com/users/don-watkins"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
使用此 Linux 命令从图像中删除背景
|
||||
======
|
||||
|
||||
你有一张很棒的自己的照片,并想将其用于你的社交媒体资料,但背景让人分心。另一张图片为你的个人资料图片提供了完美的背景。你如何将两者结合起来? 一些智能手机应用可以进行这种照片处理,但价格太贵或充斥着广告软件。而且它们不是开源的。[Rembg][1] 将迎接挑战!
|
||||
|
||||
Rembg 是用 Python 编写的,因此请在你的计算机上安装 Python 3。大多数 Linux 发行版默认包含 Python 3。你可以使用这个简单的命令检查你的版本:
|
||||
|
||||
```
|
||||
$ python3 --version
|
||||
```
|
||||
|
||||
Rembg 至少需要 Python 3.7 且不高于 Python 3.11。就我而言,我安装了 Python 3.10.6。
|
||||
|
||||
### 在 Linux 上安装 Rembg
|
||||
|
||||
我在我的 Linux 笔记本电脑上创建了一个名为 PythonCoding 的目录,然后创建了一个 Python 虚拟环境:
|
||||
|
||||
```
|
||||
$ python3 -m venv /home/don/PythonCoding
|
||||
```
|
||||
|
||||
接下来,我使用 `pip` 安装 `rembg`:
|
||||
|
||||
|
||||
```
|
||||
$ python3 -m pip install rembg
|
||||
```
|
||||
|
||||
### 合并图像
|
||||
|
||||
是时候施展魔法了。首先,我选择了 2019 年在 All Things Open 拍摄的照片。
|
||||
|
||||
![Don Watkins at All Things Open conference][2]
|
||||
|
||||
为了方便起见,我运行了以下 `rembg` 命令以使用较短的文件名重命名它:
|
||||
|
||||
```
|
||||
$ rembg i dgw_ato.jpeg dgw_noback.jpg
|
||||
```
|
||||
|
||||
第一次运行 `rembg` 时,它会下载一个开源[模式识别模型][3]。这可能超过 100 MB,并且 `rembg` 将它保存到 `~/.u2net/u2net.onnx` 的用户目录中。该模型是 U-2-Net 并使用 Apache 2.0 许可证。有关模式识别模型的更多信息(包括如何训练你自己的模型),请阅读 Rembg 文档。
|
||||
|
||||
它在大约十秒钟内创建了我没有背景的新照片。我有一个带有 16 GB RAM 的 Ryzen 7。你的体验可能因硬件而异。
|
||||
|
||||
![A processed image of Don Watkins, with the background removed by Rembg.][4]
|
||||
|
||||
过去我曾使用 [GIMP][5] 删除背景,但 `rembg` 比我使用 GIMP 时更快更彻底。
|
||||
|
||||
这就是删除背景的全部内容。如果添加一个新的呢?
|
||||
|
||||
### 添加新背景
|
||||
|
||||
接下来,我想给图片添加一个新的背景。有不同的方法可以做到这一点。例如,你可以使用 [ImageMagick][6] 组合图像,但获得正确的帧大小可能很复杂。最简单的方法是使用 GIMP 或 [Krita][7]。
|
||||
|
||||
我使用 GIMP。首先,打开新创建的图像(在我的例子中是 `ato_image.jpg`)。现在转到**文件**菜单并选择**打开为图层**。为背景选择不同的图像。此图像作为现有照片的叠加层打开。
|
||||
|
||||
我想将新背景移到我的肖像下方。在 GIMP 窗口的右侧有两个缩略图,每个图像层一个。背景层在上面。我将背景层拖到肖像图像下方,结果如下:
|
||||
|
||||
![Don Watkins with a new background.][8]
|
||||
|
||||
这对我的个人资料照片来说是一个更好的设置!
|
||||
|
||||
### 尝试 Rembg
|
||||
|
||||
Rembg 有三个子命令,你可以在 `--help` 菜单中查看:
|
||||
|
||||
```
|
||||
$ rembg --help
|
||||
```
|
||||
|
||||
他们是:
|
||||
|
||||
- `rembg i` 用于文件
|
||||
- `rembg p` 用于文件夹
|
||||
- `rembg s` 用于 HTTP 服务器
|
||||
|
||||
Rembg 使用 [MIT][9] 许可证发布。下次你需要从图像中删除背景时试试看。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/4/image-editing-linux-python
|
||||
|
||||
作者:[Don Watkins][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/don-watkins
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://github.com/danielgatis/rembg
|
||||
[2]: https://opensource.com/sites/default/files/2023-03/don-watkins-at-All-Things-Open.webp
|
||||
[3]: https://github.com/xuebinqin/U-2-Net
|
||||
[4]: https://opensource.com/sites/default/files/2023-03/don-watkins-no-background.webp
|
||||
[5]: https://opensource.com/content/cheat-sheet-gimp
|
||||
[6]: https://opensource.com/article/17/8/imagemagick
|
||||
[7]: https://opensource.com/article/21/12/open-source-photo-editing-krita
|
||||
[8]: https://opensource.com/sites/default/files/2023-03/don-watkins-at-the-beach.webp
|
||||
[9]: https://github.com/danielgatis/rembg/blob/main/LICENSE.txt
|
Loading…
Reference in New Issue
Block a user