douma5954 2016-10-25 20:56
浏览 259
已采纳

图像像素坐标

I'm using PHP and ImageMagick to read colors from an image's edge.

$x = 1;
$y = 1;
$pixel = $image->getImagePixelColor($x, $y);
$colors = $pixel->getColor(); // rgba

But I don't quite get how the coordinates work. So I created this 4px png.

enter image description here

1,1 gives rgba(0,30,255,0) instead of the transparent pixel. Also, what if I select a large range of pixels?

  • 写回答

2条回答 默认 最新

  • drxd54816 2016-10-26 08:24
    关注

    The easiest way to understand this is to remember that when a pixel is transparent, its colour becomes irrelevant, yet it is retained.

    So, if we make an opaque 512x512 image of random noise, like this:

    convert -size 512x512 xc:gray +noise random a.png
    

    enter image description here

    and check its size, you can see it is 1.5MB:

    -rw-r--r--@ 1 mark  staff   1.5M 26 Oct 09:14 a.png
    

    If I now make an otherwise identical, transparent version:

    convert -size 512x512 xc:gray +noise random -alpha transparent b.png
    

    You can see the file has got bigger (at 1.7MB), because it is now storing transparency AND the original random data. I won't show the image because there is nothing to see:

    -rw-r--r--@    1 mark  staff   1.7M 26 Oct 09:16 b.png
    

    I can now make the transparency layer fully opaque and the random pixels become visible again - so they were still there all along - even when the image was transparent:

    convert b.png -alpha opaque c.png
    

    enter image description here

    If, however, I make the pixels transparent again, AND also set them all to the colour of the image background (since their colour is irrelevant), like this:

     convert -size 512x512 xc:gray +noise random -alpha transparent -alpha background d.png 
    
    -rw-r--r--@ 1 mark  staff   1.8K 26 Oct 09:22 d.png
    

    And now suddenly the 1.5MB file is nearly 1,000 times smaller at 1.8K because it compresses much better as all the (invisible/transparent) colours are one single, solid background colour.

    In answer to your question about coordinates. We can make a little image like this:

    convert -size 1x1             \
           xc:red xc:cyan +append \
        \( xc:yellow xc:magenta +append \) -append a.png
    

    enter image description here

    And dump it like this:

    convert a.png txt:
    
    # ImageMagick pixel enumeration: 2,2,65535,srgb
    0,0: (65535,0,0)  #FF0000  red
    1,0: (0,65535,65535)  #00FFFF  cyan
    0,1: (65535,65535,0)  #FFFF00  yellow
    1,1: (65535,0,65535)  #FF00FF  magenta
    

    And see that pixel coordinates in ImageMagick start at top-left corner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码