I just made up the weird topic name to give an idea of what i'm trying to do. The ImageMagick software is installed on my server and works fine. I have successfully used ImageMagick in scripts and it's really an amazing image tool, i've had many uses. However, There's one thing I was wondering about though. I have dug for hours all over the internet, and come close, but fail. I can't find a feature that does what i'm looking for.
I need to be able to remove all of one color, which is definitely possible with imagemagick (telling the whole "background color" to be transparent), however, what's wrong is that with what i'm doing, I need to apply alpha channels to an image, AND have the solid color background be transparent. I have somewhat succeeded, but then it doesn't work once I apply the alpha, or if I just apply the alpha the solid color stays around the avatar..
How would do this in an image application such as photoshop is to use magic want to select all of the background pixels, and then hit delete to literally destroy/remove the pixels, and then apply alpha channels.
The situation is, I am developing a script to allow people to use customized sprite sheet avatars in this interactive chat application. It supports alpha, but without alpha it just removes all pixels of the solid background color. If alpha exists it listens to the alpha and doesn't bother removing all of the one color. There's a cool thing you can do involving alpha to make "ghost avatars". So this is why I need this feature, so people don't need to have an advanced image application such as Paint Shop Pro or Photoshop just to do the effect.
Does anyone know of a way that all of one color can be deleted instead of made transparent? If I could delete all the pixels of a specific color (similar to if I define a color to be transparent), then adding alpha transparency is all that's left to do to make the script succeed.
I'd appreciate any advice you could give me.
* I thought I would mention in case it helps find a solution, when doing this in photoshop I have to duplicate the image then hide the other copy so that it isn't a part of the standard "background", with the other one disabled, I then select the solid color background of the image and then I can continue to delete it and add the alpha. i'm not sure if this sort of thing matters in imagemagick, but I thought I would give the additional info in case it helps find a way to do this.
Magick Eraser / Pixel Deletion question
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Magick Eraser / Pixel Deletion question
merge you two alpha channels first, then put that back into your image.
if you can provide links to your initial image (and alpha channels if available) and the final result you want with some explanation, I can probably help you with this. I just am not sure what I am working with.
But you cannot delete colors, you can only make them transparent or partially transparent.
if you can provide links to your initial image (and alpha channels if available) and the final result you want with some explanation, I can probably help you with this. I just am not sure what I am working with.
But you cannot delete colors, you can only make them transparent or partially transparent.
Re: Magick Eraser / Pixel Deletion question
Alright, I got some of the example images. In this example i'll be using my friend's avatar. The way the sprite sheets work is each frame must be 32px in width and 64px in height. It's not limited to two frames either, there can be as many as several dozen frames going to the right, the software plays the sprites as an animation for walking, sitting, etc. I just thought I would say that so it's understood that avatars must remain the same proportion as the original and may be bigger than that in case that's a problem for whatever solution comes up and the solution involves having a second alpha image with a specific predefined size.
So here we go, this is the original avatar, unmodified:
http://sonic-club.com/users/se7/magick/original.png
The pink background is normally some bring color that definitely isn't a part of the avatar otherwise it becomes transparent under normal circumstances.
This is the result of deleting all the pixels of that solid color, the background itself then becomes pure alpha, instead of alphafied color:
http://sonic-club.com/users/se7/magick/ ... ted_bg.png
For my next example i'm using 35% alpha on the image that has the deleted background:
http://sonic-club.com/users/se7/magick/ ... _alpha.png
That image right there happens to be the sort of result i'm looking for, when I make the script though I planned to allow the user to define in the form what percentage of transparency they want.
Here's what happens if I don't delete the background and just add an alpha channel to the image:
http://sonic-club.com/users/se7/magick/ ... ted_bg.png
Imagemagick can successfully pull this off. If I add normal transparency to the background, it looks perfect and then I think all I have to do is add alpha, but obviously it can't have both types of transparency at once, so it cancels it out and takes the alpha, becoming this.
I can give some script examples if you want, it's nothing too promising though. I have tried to merge a blank alpha image to the avatar. At first I thought I succeeded, but I think that was the time when it just resulted in the avatar losing it's actual alpha and just LOOKING alpha on the white background on the browser, but when you test out the image more you find that it just has a white or transparent background and the image itself was covered in a way that made it look faded in an alpha way.
It's a very simple thing I want to do, but I just haven't figured out how to pull it off or if it's possible. It's looking like the only way to pull this off is to be able to erase or delete the specified pixels so that nothing is there at all except purely the alpha channel.
So to simplify what i'm saying.. the sprite sheet has a solid color background and i'm deleting that solid color everywhere (even if the pixels aren't touching eachother so it can't be a simple flood fill of any sort), after that I apply a specified percentage to the alpha channel and it gives a ghost effect, revealing in the chat software what's behind the avatar itself.
If you want me to provide anything else then let me know. Thanks again for assisting me.
Edit: I was thinking, you said "and alpha channels if available". You don't mean what I gave you just now do you? Do you mean pure alpha not linked to the target image? I have a theory but I also don't think imagemagick can pull this off.... It's pretty brilliant for something I just came up with though.. Is it possible..that I can have a big alpha channel sheet with no pixels on it, and that imagemagick can select the shape of all the unwanted pixels, and use that shape as a template to cut it out of the alpha and replace the pixels with pure alpha that way? Like a cookie cutter? That sounds more like something imagemagick could do. You tell me if you think either of the things I want done are possible though..
So here we go, this is the original avatar, unmodified:
http://sonic-club.com/users/se7/magick/original.png
The pink background is normally some bring color that definitely isn't a part of the avatar otherwise it becomes transparent under normal circumstances.
This is the result of deleting all the pixels of that solid color, the background itself then becomes pure alpha, instead of alphafied color:
http://sonic-club.com/users/se7/magick/ ... ted_bg.png
For my next example i'm using 35% alpha on the image that has the deleted background:
http://sonic-club.com/users/se7/magick/ ... _alpha.png
That image right there happens to be the sort of result i'm looking for, when I make the script though I planned to allow the user to define in the form what percentage of transparency they want.
Here's what happens if I don't delete the background and just add an alpha channel to the image:
http://sonic-club.com/users/se7/magick/ ... ted_bg.png
Imagemagick can successfully pull this off. If I add normal transparency to the background, it looks perfect and then I think all I have to do is add alpha, but obviously it can't have both types of transparency at once, so it cancels it out and takes the alpha, becoming this.
I can give some script examples if you want, it's nothing too promising though. I have tried to merge a blank alpha image to the avatar. At first I thought I succeeded, but I think that was the time when it just resulted in the avatar losing it's actual alpha and just LOOKING alpha on the white background on the browser, but when you test out the image more you find that it just has a white or transparent background and the image itself was covered in a way that made it look faded in an alpha way.
It's a very simple thing I want to do, but I just haven't figured out how to pull it off or if it's possible. It's looking like the only way to pull this off is to be able to erase or delete the specified pixels so that nothing is there at all except purely the alpha channel.
So to simplify what i'm saying.. the sprite sheet has a solid color background and i'm deleting that solid color everywhere (even if the pixels aren't touching eachother so it can't be a simple flood fill of any sort), after that I apply a specified percentage to the alpha channel and it gives a ghost effect, revealing in the chat software what's behind the avatar itself.
If you want me to provide anything else then let me know. Thanks again for assisting me.
Edit: I was thinking, you said "and alpha channels if available". You don't mean what I gave you just now do you? Do you mean pure alpha not linked to the target image? I have a theory but I also don't think imagemagick can pull this off.... It's pretty brilliant for something I just came up with though.. Is it possible..that I can have a big alpha channel sheet with no pixels on it, and that imagemagick can select the shape of all the unwanted pixels, and use that shape as a template to cut it out of the alpha and replace the pixels with pure alpha that way? Like a cookie cutter? That sounds more like something imagemagick could do. You tell me if you think either of the things I want done are possible though..
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Magick Eraser / Pixel Deletion question
The idea is to create the two alpha channels and compose multiply them, then put that into your original as the one alpha channel
try this
(note I renamed your files for ease of use by adding avatar to the front)
convert avatar_original.png \
\( avatar_photoshop_deleted_bg.png -alpha extract \) \
\( avatar_deleted_bg_with_35percent_alpha.png -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite avatar_result.png
first line is original no alpha
second line extracts the masking alpha
third line extacts the 35% alpha
fourth line multiplies the two alpha channels
fifth line deletes the temps and puts the multiplied alpha into the alpha channel of the original
see
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/basics/#parenthesis
try this
(note I renamed your files for ease of use by adding avatar to the front)
convert avatar_original.png \
\( avatar_photoshop_deleted_bg.png -alpha extract \) \
\( avatar_deleted_bg_with_35percent_alpha.png -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite avatar_result.png
first line is original no alpha
second line extracts the masking alpha
third line extacts the 35% alpha
fourth line multiplies the two alpha channels
fifth line deletes the temps and puts the multiplied alpha into the alpha channel of the original
see
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/basics/#parenthesis
Re: Magick Eraser / Pixel Deletion question
You're awesome. First I want to say that your code worked flawless with what I wanted to do.
I will admit it got tricky after that though. With this script I was planning, I can't predict what user the avatar will upload so it has to be adaptable to any png avatar the user uploads, any size with any background color and any shape of the avatar, you definitely knew what you were talking about though so it worked out in the end. While testing with another template, leaving the same ones it got alpha from, it didn't seem to like that.
Since GD and ImageMagick can't delete pixels per se, your example gave me a workaround for what I wanted to do.
So here's how it turned out.. I'm sure imagemagick could do this, but I had GD take care of making the two images to be processed with the user-uploaded avatar.
I'll go ahead and explain how I pulled it off
I made the script so that GD creates one image out of the provided image, it gets the color of the first pixel, then makes that color the type of transparency that is possible when you define what color you want transparent on a PNG.
GD then creates another image by getting the dimensions of the image uploaded (if the image is smaller than the one uploaded dimension-wise, then the other frames won't show, it's common for avatars to vary in amount of frames, most are two but some people create three and four.. Anyway.. the reason for having GD create this other image is that it creates a canvas of the exact size (a white solid color blank canvas) and then I define the alpha using GD. It then creates the file.
After that ImageMagick processes both images using almost exact code to what you provided, and then the output is the exact result I was looking for.
Thanks very much for help. I'll definitely research ImageMagick better so that I understand the possibilities thoroughly.
Once again I do realize it was probably silly of me to rely on two lib's to create the result I want, i'm sure imagemagick can do what I told GD to do, but I just had the code convenient from previous attempts so I took the fast way.
Thanks again, you've been a huge help. Sorry about taking so long to get back to you. I was planning to get the script done within just a day or two, but some problems came up with the host and such and certain things like imagemagick weren't available for quite a few days.
I will admit it got tricky after that though. With this script I was planning, I can't predict what user the avatar will upload so it has to be adaptable to any png avatar the user uploads, any size with any background color and any shape of the avatar, you definitely knew what you were talking about though so it worked out in the end. While testing with another template, leaving the same ones it got alpha from, it didn't seem to like that.
Since GD and ImageMagick can't delete pixels per se, your example gave me a workaround for what I wanted to do.
So here's how it turned out.. I'm sure imagemagick could do this, but I had GD take care of making the two images to be processed with the user-uploaded avatar.
I'll go ahead and explain how I pulled it off
I made the script so that GD creates one image out of the provided image, it gets the color of the first pixel, then makes that color the type of transparency that is possible when you define what color you want transparent on a PNG.
GD then creates another image by getting the dimensions of the image uploaded (if the image is smaller than the one uploaded dimension-wise, then the other frames won't show, it's common for avatars to vary in amount of frames, most are two but some people create three and four.. Anyway.. the reason for having GD create this other image is that it creates a canvas of the exact size (a white solid color blank canvas) and then I define the alpha using GD. It then creates the file.
After that ImageMagick processes both images using almost exact code to what you provided, and then the output is the exact result I was looking for.
Thanks very much for help. I'll definitely research ImageMagick better so that I understand the possibilities thoroughly.
Once again I do realize it was probably silly of me to rely on two lib's to create the result I want, i'm sure imagemagick can do what I told GD to do, but I just had the code convenient from previous attempts so I took the fast way.
Thanks again, you've been a huge help. Sorry about taking so long to get back to you. I was planning to get the script done within just a day or two, but some problems came up with the host and such and certain things like imagemagick weren't available for quite a few days.