Recent

Author Topic: SOLVED: Removing transparent/empty pixels from* PNG  (Read 4676 times)

AsleyCruz

  • Full Member
  • ***
  • Posts: 108
    • Graphic and web designer
SOLVED: Removing transparent/empty pixels from* PNG
« on: April 13, 2020, 08:12:28 am »
Hi team

If I have a PNG image (256x256) like the attached file...
How can I delete the empty pixels?

I just want the image cropped.

Thanks in advance.
« Last Edit: April 18, 2020, 05:26:17 am by AsleyCruz »
Graphic & web designer

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Removing transparent/empty pixels from PNG
« Reply #1 on: April 13, 2020, 08:28:10 am »
sorry I'm not functioning properly at the time. Are you asking how to avoid keeping empty pixels in memory or how to apply a mask when pasting an image form the clipboard?

You do understand that pixels do not exist in 2 dimensions on disk right? on disk everything is a long one dimensional array, only a small header at the start of the file tells you that every N pixel you need to go to the next line.
if that header gets lost or corrupted you will get weird results. Now although possible to convert an image in to a list of pixel blocks you will need extra info for each bloc like top left position width and possible height of the block which makes it a bit overkill for groups with small number of pixels and as the group count gets longer the memory waste gets longer.


AsleyCruz

  • Full Member
  • ***
  • Posts: 108
    • Graphic and web designer
Re: Removing transparent/empty pixels from PNG
« Reply #2 on: April 13, 2020, 08:52:43 am »
sorry I'm not functioning properly at the time. Are you asking how to avoid keeping empty pixels in memory or how to apply a mask when pasting an image form the clipboard?

You do understand that pixels do not exist in 2 dimensions on disk right? on disk everything is a long one dimensional array, only a small header at the start of the file tells you that every N pixel you need to go to the next line.
if that header gets lost or corrupted you will get weird results. Now although possible to convert an image in to a list of pixel blocks you will need extra info for each bloc like top left position width and possible height of the block which makes it a bit overkill for groups with small number of pixels and as the group count gets longer the memory waste gets longer.

Ok no problem eljo if that cant be solved.
And... Is it possible to remove fuchsia color from the image to generate a transparent PNG?

Thanks in advance and have a good early morning.
Graphic & web designer

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: Removing transparent/empty pixels from PNG
« Reply #3 on: April 13, 2020, 09:55:53 am »
You can indeed use a color to be transparent, though you probably won't have antialiasing. I think the limit between your shape and the background contains pixels that are half-fuschia, so that's complicated to know what to do with them.

I suggest to open the image with transparent background in LazPaint, select Fuschia as back color, and do Image > Remove transparency > Clear alpha channel and set background.

Now you have a picture that has only pure Fuschia as background. From there you can easily replace this color by transparent. In LazPaint, using the Magic wand with 0 tolerance, clicking the background and pressing Delete.

You can do the same with code of course.
Conscience is the debugger of the mind

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Removing transparent/empty pixels from PNG
« Reply #4 on: April 15, 2020, 07:49:24 am »
Hello!

Personally, I have a slightly different problem. I use a screenshot tool which allows to capture only the active window. But, I don't know why, it creates an image larger than the window. Is there a way to remove quickly the transparent parts?

Currently I open the image in LazPaint, make a rectangle selection, crop to the selection (Ctrl+Maj+X), and after that I still have to play with the canvas size to remove the one or two lines that remain (because I never succeed to do a perfect selection with mouse).

Is there a way to do it quickly, whether in LazPaint or with a BGRABitmap program?

Thank you for your attention.

Roland
« Last Edit: April 15, 2020, 09:38:35 am by Roland57 »
My projects are on Gitlab and on Codeberg.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Removing transparent/empty pixels from PNG
« Reply #5 on: April 15, 2020, 09:26:21 am »
Hello!

Personally, I have a slightly different problem. I use a screenshot tool which allows to capture only the active window. But, I don't know why, it creates an image larger than the window. Is there a way to remove quickly the transparent parts?

Currently I open the image in LazPaint, make a retangle selection, crop to the selection (Ctrl+Maj+X), and after that I still have to play with the canvas size to remove the one or two lines that remain (because I never succeed to do a perfect selection with mouse).

Is there a way to do it quickly, whether in LazPaint or with a BGRABitmap program?

Thank you for your attention.

Roland
this has nothing to do with the oiginal subject not even close. You need to start a new thread and add all the required info like which operating system which tool you use to get the screen shot etc. There are always alternatives and in windows you can simply use alt+ptrscrn to get a screenshot of the active window in the clipboard, after that paste it in any image editing application you want and you are good to go.

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Removing transparent/empty pixels from PNG
« Reply #6 on: April 15, 2020, 09:51:54 am »
this has nothing to do with the original subject not even close.

Without wanting to contradict you, it seems to me on the contrary that it is the same problem. If you download my image and open it in LazPaint you will see what I mean.

The only difference is that what I ask is doable.  :)
My projects are on Gitlab and on Codeberg.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Removing transparent/empty pixels from PNG
« Reply #7 on: April 15, 2020, 09:54:56 am »
this has nothing to do with the original subject not even close.

Without wanting to contradict you, it seems to me on the contrary that it is the same problem. If you download my image and open it in LazPaint you will see what I mean.

The only difference is that what I ask is doable.  :)
no it does not. You ask for advice on how to use a tool not how to code it. In any case it is considered impolite to hijack an other thread.

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: Removing transparent/empty pixels from PNG
« Reply #8 on: April 15, 2020, 02:25:09 pm »
Hmm the initial topic was about removing transparent pixels, which may be understood as what Roland asks.

eljo maybe we misunderstood the original request. Also AsleyCruz is not complaining about anything.

About your question Roland, I guess the pixels around are half-transparent and make the shadow.

You can remove them using Filter > Apply function. In the alpha expression put something like:
Code: Pascal  [Select][+][-]
  1. (alpha-0.5)*2

Then you will be able to crop it more tightly.
Conscience is the debugger of the mind

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Removing transparent/empty pixels from PNG
« Reply #9 on: April 15, 2020, 09:33:46 pm »
About your question Roland, I guess the pixels around are half-transparent and make the shadow.

You can remove them using Filter > Apply function. In the alpha expression put something like:
Code: Pascal  [Select][+][-]
  1. (alpha-0.5)*2

Then you will be able to crop it more tightly.

Thank you for your answer. I hadn't noticed the shadow. I understand now why the picture is larger than the captured window.

Inspired by your solution, I made a little program that erases the shadow and crops the image.
My projects are on Gitlab and on Codeberg.

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: Removing transparent/empty pixels from PNG
« Reply #10 on: April 16, 2020, 06:50:04 am »
Nice. Indeed, you can test that alpha value compared to a threshold.

Some remark about finding the bounds, that would not work in all cases. It works if the shape is a rectangle or a rounded rectangle, but what if there is nothing on the middle column or row?

Note that there is a function TBGRABitmap.GetImageBounds that will do that for you.
Conscience is the debugger of the mind

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Removing transparent/empty pixels from PNG
« Reply #11 on: April 16, 2020, 07:54:47 am »
Some remark about finding the bounds, that would not work in all cases. It works if the shape is a rectangle or a rounded rectangle, but what if there is nothing on the middle column or row?

Yes, of course, you are right.

Note that there is a function TBGRABitmap.GetImageBounds that will do that for you.

Great! Works perfectly.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CropPictureButtonClick(Sender: TObject);
  2. var
  3.   LRect: TRect;
  4. begin
  5.   LRect := FBitmap.GetImageBounds(cAlpha);
  6.   BGRAReplace(FBitmap, FBitmap.GetPart(LRect));
  7.   Invalidate;
  8. end;
My projects are on Gitlab and on Codeberg.

 

TinyPortal © 2005-2018