dongling3243 2015-06-27 16:43
浏览 278
已采纳

使用ImagickDraw绘制单像素线时出现奇怪的阴影/模糊

So I'm trying to draw a line with Imagick today. Sounds simple enough right?

Here's the code I have:

$image = new Imagick();
$image->newImage(100, 100, 'white');
$image->setImageFormat('png');

$line_color = new ImagickPixel('#555555');
$line_weight = 1;

$line = new ImagickDraw();

$line->setResolution(100, 100);
$line->setStrokeWidth($line_weight);
$line->setStrokeColor($line_color);

$line->line(20, 20, 20, 60);

$image->drawImage($line);
$image->writeImage('test.png');

Nothing surprising, I create a 100x100 white image, I set a line color at 555555, a line weight at 1, I create my ImagickDraw object and tell him to draw a line.

The problem is that the output looks like this: http://i.stack.imgur.com/hM8QS.png

Which looks fine at first, until you zoom in and see this: http://i.stack.imgur.com/O6Yie.png

I tried several settings for the color, the weight and the resolution, but I can't get rid of this weird shadow/blur effect. It does this with pretty much anything I draw (lines, rectangle etc...) except with points. So if I want I can create my single-pixel line with a series of points and a simple for loop, but it will really get tedious to do. Especially since I'm not gonna draw just this single line.

So, anyone has any idea of what is happening? How can I get rid of this behavior?

  • 写回答

1条回答 默认 最新

  • doucheng1944 2015-06-27 19:49
    关注

    The effect you are seeing is due to stroke anti-aliasing being enabled. You can disable it with setStrokeAntialias.

    $line->setResolution(100, 100);
    $line->setStrokeWidth($line_weight);
    $line->setStrokeColor($line_color);
    $line->setStrokeAntialias(false);
    

    Reenable stroke anti-aliasing whilst drawing curved lines to avoid aliasing artifacts.

    Bizarely, in my tests, this causes the line to disappear completely when the stroke color is set to #555555 (or an equivalent representation) as it is in your code sample. However, setting the color to #545454 or #565656 causes the line to be drawn correctly, without the shadow/blur effect you described.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用