简体   繁体   English

裁剪图像中的透明度

[英]Cropping transparency in an image

I have a PNG image, which has a transparent background. 我有一个具有透明背景的PNG图像。

I'm using PHP GD to display these images, from a server. 我正在使用PHP GD从服务器显示这些图像。 Is it possible in GD to also trim this transparent background, while maintaining the remainder of the image to be transparent and if so, how would I do it? GD中是否也可以修剪此透明背景,同时保持图像的其余部分透明,如果可以,我该怎么做?

eg. 例如。 original image: http://puu.sh/8pNdx/ab2ff1e64f.png 原始图片: http//puu.sh/8pNdx/ab2ff1e64f.png

and the expected result after processing would be: http://puu.sh/8pNg7/7b7ec85459.png 处理后的预期结果将是: http : //puu.sh/8pNg7/7b7ec85459.png

If it is really important to remove transparent pixels than you will have to write a small custom algorithm. 如果删除透明像素真的很重要,则必须编写一个小的自定义算法。

  1. First try to find none transparent boundary pixels. 首先尝试找不到透明的边界像素。 ie Pixels that actually define boundary of your object. 即实际定义对象边界的像素。
  2. Than create a new transparent Bitmap according to position of those identified boundary pixels. 比根据那些确定的边界像素的位置创建一个新的透明位图。
  3. Than draw all your none transparent pixels on that new Bitmap according to new adjusted position. 根据新的调整位置,在新的位图上绘制所有不透明的像素。

Try searching for some efficient algorithm for: 尝试搜索一些有效的算法来:

separating objects from an image 从图像中分离对象

I hope it will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM