Remove grey background and turn it transparent

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jestarray
Posts: 3
Joined: 2019-01-13T20:37:36-07:00
Authentication code: 1152

Remove grey background and turn it transparent

Post by jestarray »

ImageMagick does not replace '#c0c0c0' with a transparent background

I have tried magick convert img.png -fuzz 50% -transparent '#c0c0c0' 0.png
and a convert option without "magick" running version windows 10 x64 v7.0.8-14Q16 .
A problem for later: using a bash script to convert 200 of these and it seems imagemagick also doesn't overwrite if output name is the same

here is the image: https://i.imgur.com/JnNBtpX.png

No change...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove grey background and turn it transparent

Post by snibgo »

It works fine for me, with IM 7.0.7-28 on Windows 8.1:

Code: Select all

magick jnnbtpx.png -fuzz 50% -transparent #c0c0c0 x2.png
If you are using Windows, use double quotes not single quotes. If you use Windows BAT, double the percent sign: %%

I suggest you use "magick", not "magick convert".
snibgo's IM pages: im.snibgo.com
jestarray
Posts: 3
Joined: 2019-01-13T20:37:36-07:00
Authentication code: 1152

Re: Remove grey background and turn it transparent

Post by jestarray »

not working for me at all. I tried all the variations: of magick convert, magick, convert, running gitbash, running cmd...

strangely getting...

Code: Select all

 getting magick.exe: unable to open image 'image.png': No such file or directory @ error/blob.c/OpenBlob/3490
then the file is right there, quadrupledcheck if I was still sane.. Tried double quoting the hex color, it outputs but does not remove the transparency.

on that note, -fuzz 50% ,not sure what that does, this is pixel art so i dont really want anything modified but removing the background.
jestarray
Posts: 3
Joined: 2019-01-13T20:37:36-07:00
Authentication code: 1152

Re: Remove grey background and turn it transparent

Post by jestarray »

running it on linux and it works fine...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove grey background and turn it transparent

Post by snibgo »

jestarray wrote:getting
Where is that message coming from? Are you typing the command at the Windows command prompt in a Windows command window, or what? Are you in the correct directory?
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Remove grey background and turn it transparent

Post by GeeMack »

Using IM 7.0.8-23 Q16 x64 HDRI on Windows 10 and the sample PNG image linked in the OP, and using these commands...

Code: Select all

magick convert JnNBtpX.png -fuzz 50% -transparent "#c0c0c0" 0.png

Code: Select all

magick JnNBtpX.png -fuzz 50% -transparent #c0c0c0 x2.png
... the results appear completely black when viewed with IrfanView, and unchanged from the input when viewed with Photoshop. In neither case does it result in changing the gray to transparent. I've tried to add "-background none" with no change in result.

I can make it change the gray to transparent if I add "-bordercolor none -border 0", but that surely isn't a workaround.

If I create the image from scratch using ImageMagick, the commands to change the gray to transparent work as expected. I haven't looked into the specs of the provided sample image, but it might be some kind of unconventional PNG file.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove grey background and turn it transparent

Post by snibgo »

The input is grayscale, with an offset, but those shouldn't create problems. Perhaps there is a recent bug in IM, possibly as part of "grayscale is a single channel" changes. Try "-colorspace sRGB" before "-transparent".
snibgo's IM pages: im.snibgo.com
Post Reply