我需要在本地生成一些(数千个)虚拟图像来测试基于CLI的内容转换器。文件名已设置。
名单作为示例:
fw4df.jpg
antotherName.jpg
等等。
我会使用ImageMagick
扩展名编写一个脚本来创建带有一些虚拟内容的图像。
有什么建议可以解决这个问题吗?
答案 0 :(得分:2)
我会为1,000张图片执行此操作:
head -1000 /path/to/your/word/list |
while read word; do \
echo -n $word | convert label:@- "$word.jpg" \
done
你得到了
答案 1 :(得分:1)
ImageMagick附带一些内置图像。最常见的是wizard:
,rose:
和logo:
convert wizard: wizard.jpg
convert rose: rose.jpg
convert logo: logo.jpg
如果你只想要一个聪明的PHP函数,我会继续使用PseudoImages来简化所有事情。
function createPseudoImage($label_text, $width, $height, $label_format='No. %d') {
$background = new Imagick;
$background->newPseudoImage($width, $height, 'PATTERN:HORIZONTALSAW');
$label = new Imagick();
$label->setBackgroundColor('transparent');
$label_width = $background->getImageWidth() * 0.8;
$label_height = $background->getImageHeight() * 0.8;
$label_text = sprintf('CAPTION:'.$label_format, $label_text);
$label->newPseudoImage($label_width, $label_height, $label_text);
$offset_x = $background->getImageWidth()/2 - $label->getImageWidth()/2;
$offset_y = $background->getImageHeight()/2 - $label->getImageHeight()/2;
$background->compositeImage($label, Imagick::COMPOSITE_ATOP, $offset_x, $offset_y);
return $background;
}
$fpo = createPseudoImage(49, 500, 200);
$fpo->writeImage('/tmp/out.png');
但是YMMV
答案 2 :(得分:0)
我根据PHP doc示例编写了一个脚本。也许它会帮助某人:
<?php
generateDummyImages(array("test.jpg", "test2.jpg"));
/**
*
* @param array $files
* @param string $dataOutPath
*/
function generateDummyImages($files, $dataOutPath = "") {
$width = 500;
$height = 500;
$imageCompressionQuality = 90;
foreach($files as $i => $filename) {
/* Create a new imagick object */
$im = new Imagick();
/* Create new image. This will be used as fill pattern */
$im->newPseudoImage($width, $height, "gradient:red-black");
/* Create imagickdraw object */
$draw = new ImagickDraw();
/* Start a new pattern called "gradient" */
$draw->pushPattern('typo', 0, 0, $width, $height);
/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, $width, $height, $im);
/* Close the pattern */
$draw->popPattern();
/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#typo');
/* Set font size to 52 */
$draw->setFontSize(100);
/* Annotate some text */
$draw->annotation($width / 2 - 100, $height / 2, "No. " . $i);
/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage($width, $height, "white");
/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);
/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);
/* Set the format to PNG */
$canvas->setImageFormat('jpg');
$canvas->setImageCompression(Imagick::COMPRESSION_JPEG);
$canvas->setCompressionQuality($imageCompressionQuality);
/* Output the image */
$canvas->writeimage($dataOutPath . $filename);
echo "Image generated: " . $dataOutPath . $filename . PHP_EOL;
}
}
?>