IMagick检查亮度图像 [英] IMagick check lightness image

查看:142
本文介绍了IMagick检查亮度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在图像中自动编写一些文本。根据图像的亮度,脚本必须写成白色或黑色。

I need to be able to write some text automatically inside an image. According to the image lightness, the script must write in white or black.

那么如何使用Imagick检查图像的亮度/暗度?

So how do I check the lightness/darkness of an image with Imagick?

推荐答案

你可以这样做:

// Load the image
$imagick = new Imagick("image.jpg");
// convert to HSL - Hue, Saturation and LIGHTNESS
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
// Get statistics for the LIGHTNESS
$Lchannel = $imagick->getImageChannelMean(imagick::CHANNEL_BLUE);
$meanLightness = $Lchannel['mean']/65535;
printf("Mean lightness: %f",$meanLightness);






如果你想做下色文本,请根据Fred's建议,您可以在PHP中执行以下操作:


If you want to do undercoloured text, per Fred's suggestion, you can do that in PHP with:

$image = new Imagick("image.jpg");
$draw  = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFontSize(24);
$draw->setTextUnderColor('#ff000080');
$image->annotateImage($draw,30,50,0,"Undercoloured Text");
$image->writeImage('result.jpg');

这篇关于IMagick检查亮度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆