Rmagick Remove White Background from Image and Make It Transparent

RMagick remove white background from image and make it transparent

convert image.png -matte -fill none -fuzz 1% -opaque white result.png

Replaces anything white with transparency. The fuzz option includes anything that is almost-white.

How to replace white background color with transparent of an image in ImageMagick?

First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:

convert image1.png -fuzz 20% -transparent white result.png

The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.

remove white background with ImageMagick but not the white inside picture?

The problem with your current approach is that it doesn't respect boundaries, it is just applied globally, making all white pixels transparent regardless of their connectivity to the background.

Instead, you will get on better using a "floodfill" that only floods into areas that are within the fuzz distance of the top-left corner pixel.

So, I chose an unused colour of magenta so you can see what is happening:

convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' result.png

Sample Image

You would then follow that with the command to make magenta transparent like this:

convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' -transparent magenta result.png

Sample Image

Rmagick - image with transparent background from text

When you create an image, the default background is white. You can tell rmagick you want a different background:

canvas = Image.new(400, 60) do |c|
c.background_color= "Transparent"
end

ImageMagick White to Transparent Background While Maintaining White Object

SOLUTION:

Replace white background with some other colour first, then change that colour to transparent.

<?php

# get img url
$imgUrl = $_GET['img'];

# create new ImageMagick object from image url
$im = new Imagick($imgUrl);

# replace white background with fuchsia
$im->floodFillPaintImage("rgb(255, 0, 255)", 2500, "rgb(255,255,255)", 0 , 0, false);

#make fuchsia transparent
$im->paintTransparentImage("rgb(255,0,255)", 0, 10);

# resize image --- passing 0 as width invokes proportional scaling
$im->resizeImage(0, 200, Imagick::FILTER_LANCZOS, 1);

# set resulting image format as png
$im->setImageFormat('png');

# set header type as PNG image
header('Content-Type: image/png');

# output the new image
echo $im->getImageBlob();

Sample Image

Sample Image

Sample Image

Sample Image

Replace transparency in PNG images with white background

This works for me:

convert -flatten img1.png img1-white.png

Documentation references:

  • -flatten command-line option
  • -layers command-line option (-flatten is equivalent to -layers flatten)


Related Topics



Leave a reply



Submit