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
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
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();
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
Ruby Loaderror: Cannot Load Such File
Ror + Unable to Install Tiny_Tds
Convert an Array to Hash, Where Keys Are the Indices
How to Order Files by Last Modified Time in Ruby
Verify Client Certificate Using Sslserver in Ruby
How to Test If a Div Has a Certain CSS Style in Rspec/Capybara
Nokogiri: Select Content Between Element a and B
Safely Assign Value to Nested Hash Using Hash#Dig or Lonely Operator(&.)
How to Parse JSON Request Body in Sinatra Just Once and Expose It to All Routes
Can't Install Thrift Gem on Os X El Capitan
Execute Ruby Code in Sublime Text 2
Rails - Multiple Top Level Domains and a Single Session/Cookie
Ruby Bundler Multiple Sources in Gemfile
How to Merge Two Hashes with No New Keys
How to Compare a String Against Multiple Other Strings