PDA

View Full Version : Tutorial about generating transparent PNG's for use as tokens in a VTT or similar.



Redrobes
02-22-2009, 08:41 AM
This is part tutorial or at least the process by which I achieve them, part call for people to add how they do them and part gratuitous attempt to get rep points to recover my place with GP and keep ahead of Ascention. Ok not really, but since I know your all thinking that I thought id mention it first ;)

A chap who is using my app has discovered that to make tokens you need to make transparent images and is worried that he will spend half a lifetime drawing alpha transparency masks for all the images he wants to use. He is making a sci-fi world so I thought id use some non copyright NASA images to show what can be done and why its not going to take as long as one might initially think if you know a few tricks.

I use PSP 7 which is old enough that it does not cope with transparent PNGs natively apart from the type which has a single color index assigned to clear - like GIF say. I believe its actually better to deal with the alpha mask or transparency layer as a separate image but then I am forced to. PSP7 can deal with images with transparency but it lacks the capability to save them in PNG format. I believe that later versions do not lack this file format problem and Gimp / PS users definitely don't have an issue. Also, image magick can deal with them easily too though I know I am of a small minority of people which use it.

So back to basics and to recap some things which many will already know but its good to be complete here. Most image file formats have color information and no transparency information at all. These formats have red green and blue info or RGB. The common formats in this category are JPEG and BMP and both of these two can have full color images - like 16 million colors or so. Next up is GIF which holds 256 colors max - known as a color index format. You can optionally assign one of them to the transparent color. Then when viewed with the right viewer - like a web page browser (firefox / IE etc) it appears with 255 palette of discreet colours out of the 16 million possible and the one assigned to clear showing the background of the web page through the image. But that GIF is either totally clear or totally opaque. PNG has many colour options but the one we focus on is the RGBA where it has a channel called the Alpha channel and that is a greyscale layer assigned to transparency. So it can show any of the 16 million colors and it can also show them as fully opaque, fully transparent and also anything in between. In a VTT or stamp based tool you can use this effect to have image tokens which have a shape and fades - like fading out shadow portions of the image.

Redrobes
02-22-2009, 08:42 AM
Right, so lets say you have a pic off the web thats a non copyright image, CC or otherwise able to rip. Its RGB like a JPEG image and you want to make it into a PNG token with a cut out shape. Heres an example. HiRes pics from here http://grin.hq.nasa.gov/subject.html but I am using smaller versioned ones for examples.

You could do this in a number of ways but this is the way I use. Starting with the X36 plane pic, look for a color that is definitely not in the image - like bright red. Get the single line tool and draw around it. What I tend to do is put the start point down near to where I want to start but a little away from the edge I am trying to follow. Then drag out the line and put the end point down exactly where I need it. Then without moving the mouse, start a new line and drag back to the starting point again but this time set the end point down exactly where it needs to be. The reason I do it this way is that you dont have to guess the angle exactly in one go. Like image below but end point 3 past the corner. Note, turn off anti aliasing for the line. Let it have jaggy edges. Otherwise it will blend red into the image and we will want to pick up the red in a mo.

Redrobes
02-22-2009, 08:43 AM
So you have lined out the whole plane, use the selection magic wand tool and set the match color tolerance really high and pick a point in the background region of the plane. It should select all the background and up to but not including the red line. If its not hitting that then you need to adjust the tolerance. Normally something really high like 100 or so should do it tho if the red is sufficiently different to the image. Once that has done its thing then you can use the selection menu and use the expand selection by a few pixels - the number of pixels depends on the line size but basically set it to half the line width. If you have the 'marching ants' selection border showing it should now run through the middle of the red lines. Select the fill tool and set the fill match to none. Ie. Fill everything. Put that down in the background area and it should fill everything in the selection area giving a solid red background.

Redrobes
02-22-2009, 08:43 AM
Touch up any missing little bits and then use the brightness tool to increase brightness by a large margin - like 50% or something like that.

Then select the fill tool again, set the match tolerance to 0 and set the color to black. Fill the light red area to black.

Now make the image greyscale using the color menu. Then use the brightness control again and add 127 brightness and 100% contrast. This should make everything not black into white. A touch of soften to smooth the edges down and your done.

Redrobes
02-22-2009, 08:44 AM
Here is an example of the X36 token.

Redrobes
02-22-2009, 08:44 AM
So that was a simple shape that had straight lines to cut out. How about something harder. This is not so bad *if* you can get a picture with a background color that is constant so that we can differentiate the image from its background. Images with a constant black or white background are best, then ones with all solid color. Here is the old and now burnt up Mir Space Station. Its pretty complicated to cut out but we can do most of that previous cut out process on it.

Redrobes
02-22-2009, 08:45 AM
Take a copy of the color image and make a greyscale version of it. Soften it a little with a slight blur - maybe a few times. This reduces any noise in the image but you don't want to do it so that it looks very blurry. Now take that brightness option again and add as much brightness as you can with 100% contrast. In this case it was a lot - about 124. The more you can add without the background showing through the better. It depends on the pic in question though this one is with a near black background that allows us to go quite high. Once done then blur it a bit more and do again. This will expand the white to fill in small holes. By this point you can probably get out a brush and touch up the odd black dot in white and the odd white dot in black and remove stuff you don't want like the white in bottom right corner.

Once its all solid black and white use the selection tool again on the white. Then contract the selection by a few pixels. We do this because we expanded the white earlier and we have been real aggressive in getting everything into the white areas. The amount to contract is a few pixels depending on the size of image and original color difference. Then invert the selection and flood the background with a match of 'none' again like before. Blur the edges just a little and save out the final alpha mask.

Redrobes
02-22-2009, 08:46 AM
Here is an example of the Mir station token on a background.

Redrobes
02-22-2009, 08:46 AM
And doing a few more like this here is collage of tokens on a background image with the astronaut floating away in the background. The big astronaut, planet and station are tokens on the top.

Hope you enjoyed that and hope that some of you guys can add to that as to how you do the same thing.

Steel General
02-22-2009, 10:14 AM
Nice job RR! *bonk*

Midgardsormr
02-22-2009, 12:52 PM
You've left out a step. Once you have your alpha mask, how do you connect it to your original image to form the PNG?

Obviously, as a Photoshop user, my interest is merely academic, but I've always been curious about how you get the PNG if your software can't save it that way to start with.

And since you mentioned Image Magick obliquely in your post, a primer on the use of that utility would not go unappreciated (at least by me), if you have the time someday. I'd really like to get to know it better for the batch processing capabilities.

Redrobes
02-22-2009, 01:07 PM
Yes good point. OK lets hit two birds with one stone.

To convert a color image file_C.png and an alpha mask file_A.png into an RGBA transparent file.png you can call :-

composite %1_A.png -compose copy-opacity %1_C.png -depth 8 %1.png

in a batch file using image magick and pass in the name 'file' as the batch file parameter ( %1 ). Then you can call that batch file from another with a list of files that need converting if you wanted to - which is similar to what I do for Kimmo's character tokens.

In my app you load them in as a pair - see image - and save out the corresponding file.

Redrobes
02-22-2009, 07:26 PM
I was going to post some more about ImageMagick but got distracted for a few hours....

Image magick is a command line script based set of programs which can manipulate images. There is no user interface so that puts a lot of people off but on the other hand it can be embedded into stuff like this forum for example which Arcana has said uses it to generate some of the attachment icons. Its very widely used if not widely known to be being used if you know what I mean.

For doing interactive stuff its completely useless but for doing stuff which has to be done time and time again its a strong tool. Gimp and many other apps are now embedding a scripting language into them to be able to do similar stuff so for those people they would stick with the UI launched scripts. But you can get IM on a lot of platforms with a small download and if you can get a command line shell prompt then you have a pretty big image wrench at your disposal.

There are about 4 commands but most people use just the one. Convert. It was called that because in the old days it used to convert one image file format into another and that was it. Then all the other stuff got added until its now fully featured. But make no mistake about it, even though it looks clunky and it is pretty slow, it does each job requested very very well indeed. Where there's an option to do a short and rough cut, it does not take it.

So to resize an image called in.png to out.jpg you can call

convert in.png -resize 800x600 out.jpg

and it will do it but if the aspect of the image was not 4:3 then it maintains the aspect of the original. If you definitely want it that size then call

convert in.png -resize 800x600! out.jpg

If the image was larger than 800x600 then it down samples it using a lanczos filter and if going up it uses the Mitchell filter. To put that simply, it uses very good algorithms in both directions. It uses pretty good defaults for just about everything in fact.

To go into detail about a lot of what it can do would take many many pages and there's oodles of spec and tutorial stuff for it out there. My seamless tiling script is windows batch file calling IM commands to do it so you can have a look at that to see it doing more complicated stuff.

My advice tho is go to the usage pages and see the zillion examples to do all the fancy stuff it can do if you know how. I have to look up loads and loads of stuff its too much to remember.

http://www.imagemagick.org/Usage/

Midgardsormr
02-23-2009, 11:49 AM
So one quick usage question. I scanned the documentation you linked to, and based on that, this should work.

If I use the command
convert in.jpg -resize 10000x200 out.jpg

I should get an image that is 200 pixels high, and the width will remain elastic. Is that correct? Is that the best way to accomplish it, or is there a wildcard of some sort to get IM to ignore one dimension?

Redrobes
02-23-2009, 02:57 PM
Yeah I believe that is the case. I think Y is more important than X. If you did a

-resize 10000x

then it would do 10000 wide and scale the height to match aspect. So I think yours is equiv to

-resize x200

I believe too that if you use the mogrify command, which is exactly the same as convert but it works on the image supplied not save to a different one, then you can scale all the images in a directory with a wildcard so that they are all 200 high like this

mogrify -resize x200 *.jpg

at least your supposed to be able to. I tried that on some test images and its crashing because of the wildcard in it. Hmm not sure but its supposed to work. You should be able to convert all images from jpg to png in a similar manner too...

mogrify -format jpg *.png

Anyway, you get the idea, its pretty easy to do batch processing of lots of images at once this way. I know other tools can do this too but I don't think its so easy or so controllable as IM.

Midgardsormr
02-23-2009, 08:35 PM
Somebody hit Redrobes with a rep hammer for me!

Thanks a lot for the help. Having a decent batch processing tool will make a few things far easier for me; some of them might even relate to mapping. I installed IM months ago, but I've never gotten around to using it for anything other than your seamless tile script.

Korash
02-23-2009, 09:23 PM
Somebody hit Redrobes with a rep hammer for me!

Okay, Done :)

icosahedron
02-24-2009, 07:10 AM
A chap who is using my app has discovered that to make tokens you need to make transparent images and is worried that he will spend half a lifetime drawing alpha transparency masks for all the images he wants to use. He is making a sci-fi world so I thought id use some non copyright NASA images to show what can be done and why its not going to take as long as one might initially think if you know a few tricks.


Just a note from the chap who 'instigated' this tutorial. ;)

Thanks for the interesting and informative tutorial, Redrobes, I didn't know any of that and I might be able to apply it one day. (I'll figure out this repping business shortly).

Unfortunately, this brings me back to the very reason I chose Viewingdale in the first place - these drawing programs are so difficult to learn in a short space of time. They all seem to be made by professionals for professionals.

You all use these things with such instinctive skill, but here's how it looks through the eyes of a beginner:

Firstly, you need to learn the controls of the software; then you need to know what an alpha channel is, what it is used for and how to access it and work with it with this particular software; then you need to know what anti-aliasing is, how it affects your image and how this particular software controls it; then you need to know what match colour tolerance is, how it affects your image and how this particular software controls it; then you need to know that expanding the image by a few pixels is a good idea and you need to know how to make the machine do it; then you need to recognise that increasing the brightness and contrast will help you to paint the thing white and you need to know where the controls are to do that; then you need to know that something called soften exists, you need to know what it does and how it will improve your image, then you need to figure how to make the machine do that - and that's just for the 'simple' image in your first example, and it assumes you can even find an 'edge' to draw around amongst the drop shadow.

Add to that the fact that I don't even know what it is that I need to know, and that the instructions for each operation are buried in a manual the size of an encyclopaedia that I have to read cover to cover to pick out which bits might be relevant...

All that just to import an image I can stamp down on a map!

If you only have a couple of hours a week to map in (say it's a back-burner activity for a GM and you want to spend most of that time making maps for next week's game) it can be easier just to find a simple paint program with an eraser that can be set to black, white, or 'transparent' and sit clicking pixels for a few days - at least you can pick that up and put it down in a tea break.

Midgardsormr
02-24-2009, 01:36 PM
Bear in mind that Redrobes is a geek of the highest order: the type who builds his own tools from scratch.

I've got a little bit of the technician in me, which is why I asked the questions I did, but for most purposes, I'm more of an artist. Most of the time, I want to find a tool that does what I tell it to do with a minimum of fuss.

I'll try to find some time to write up a supplementary tutorial to do what you seem to need at the moment: make building stamps quickly and easily. For now, I need to do some detailing on my Perspective homework, which is due in two hours:

Redrobes
02-24-2009, 03:09 PM
Yes all very valid points there. I think I am the worst at explaining stuff in simple terms.

Heres a simple example. I have a cyberman I took with my digi cam and have it on some black paper. I have made three transparency images. First is just the cyberman outline, second is same but has dark grey oval in middle and 3rd is where I have blurred the white version a lot and then added the white back in again. This last one gives a halo effect around him. Putting all three into the app next to each other shows what your going to see. Where the dark grey circle is shows some of the black paper but allows a lot of the floor pattern through too. Basically you can do semi-transparent icons this way.

I'll see if I can add the VMI file's as a zip at the end. You would need to create icons for those images to get a cyberman in your scene. I posted that cyberman as a PNG file in my challenge entry Dec08 for those Gimp/PS/PNG based users.

RobA
02-24-2009, 03:57 PM
There is a good tutorial at gimptalk on this in gimp:
http://www.gimptalk.com/forum/cutting-out-renders-using-the-paths-tool-t769.html

(I apologize for the title... for some reason people there call "removing the background of an image" "rendering" as opposed to the conventional CGI use of the term rendering, which confused the heck out of me.... I think it comes from the PS selection tool having the word render in the dialog...)

-Rob A>

jfrazierjr
02-24-2009, 04:01 PM
hmmm... I wonder if Redrobes likes Dr Who?

icosahedron
02-24-2009, 04:31 PM
Yes all very valid points there. I think I am the worst at explaining stuff in simple terms.


Not at all - I've probably learned more about transparencies from that tutorial than I've picked up myself in the last five years.

I'll just lurk and learn for a while. :)

Redrobes
02-24-2009, 05:04 PM
hmmm... I wonder if Redrobes likes Dr Who?

I do but its that I was trying to use sci-fi stuff and its all I had. I have a few planets and the odd ship but its nothing that I can use as a demo.

I played Traveller once or twice at least twenty years ago. I cant remember a thing about it. I did a little Shadowrun too but again not enough for a campaign. And then there's Paranoia which is amazingly silly. Judge Dredd the board game is AWESOME but we haven't played that for a while now but we used to do that once a year or so... anyone play that...

Anyway, got an hour or two free now so ill make some snow tokens.

Redrobes
02-24-2009, 07:52 PM
RobA: Thats a good way to do it. For tools that support PNG with alpha then cutting it out is probably the best way. I like the idea of a curve based selection area, my PSP does not have that. I will have to jump ship to something else one day.

I have been making snow textures for Ico. Couple of pics. Will upload a token patch shortly. I have always found doing snow immensely hard - maybe I dont see enough of the real stuff to know what its supposed to be like.

A few of kimmos characters in there - notice that the mage walks on the sunny side of the street heh heh.

jfrazierjr
02-24-2009, 09:20 PM
I played Traveller once or twice at least twenty years ago. I cant remember a thing about it. I did a little Shadowrun too but again not enough for a campaign. And then there's Paranoia which is amazingly silly.

Heh...

Trust The Computer. The Computer is Your Friend.

What a funny game...

joćo paulo
02-24-2009, 09:29 PM
nice Tuty* Redrobes

Midgardsormr
02-25-2009, 04:38 PM
(I apologize for the title... for some reason people there call "removing the background of an image" "rendering" as opposed to the conventional CGI use of the term rendering, which confused the heck out of me.... I think it comes from the PS selection tool having the word render in the dialog...)

I don't recall having seen the word "render" in Photoshop anywhere except as it pertains to the Render filters. From the Glossary elsewhere on GimpTalk:

In 3D-graphics, a "render" is the result of rendering a 3D scene. In signature-making, a "render" is a foreground cut-out of a stock image (see "Stock"). By removing background that would otherwise disturb the image, the render can be applied on a new background with any choice of effects. The art of cutting out the render ("rendering") is a skill to master all of its own.

It annoys me a bit that the word's been diluted in that fashion. Render has a very specific meaning in both CGI specifically and art generally. There's really no room for another meaning, especially as that meaning is somewhat opposite to the actual definition. "Rendering" in this sense isn't really an act of creation. At least, no more so than cutting newspaper images for a collage is. "Clipping" or "cutting" would be a far more appropriate term.

Ahem… end rant. Whether or not the terminology is good, the process certainly is, and it resembles what I was going to say. The pen is a very valuable and versatile tool, though it does take a bit of practice to get the hang of using it.

edit: Maybe they're using the term as a descendant of rendering meat--separating out what is undesirable. hmm.

Redrobes
02-25-2009, 04:54 PM
Yeah that is a bit odd. Rendering in art is filling in an area isn't it ? In the old days of ray tracing thats how it looked on screen too. You get rendering in concrete buildings too which is the same kind of thing so this new term is a bit out of place. I guess you can rend something up out of the ground which is a kind of far flung attempt to guess what they were thinking at the time.

Midgardsormr
02-25-2009, 11:35 PM
I like that. Call it rending instead--makes it sound like an act of violence.