了解 ImageMagick 的转换并转换为 Ruby RMagick

发布于 2024-10-01 11:46:14 字数 1197 浏览 10 评论 0原文

我无法将以下 PHP/ImageMagick 代码转换为 Ruby RMagick(以使未来的用户更易于管理并了解它真正在做什么):

$output = array();
$returnValue = 0;
$pngFiles = $myDir->find("/.png$/i");
foreach($pngFiles as $pngFile) {
   $cmd = 'convert '.$pngFile->path.' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info:'
   exec($cmd, $output, $returnValue);
   if($output[0] != 1) {
      logMessage("PNG file contains some alpha transparency and will not be modified");
   }
}

到目前为止我想我或多或少理解了convert-command在做什么,但是将其转换为RMagick让我重新思考这一点。

例如:为什么 $myDir 中的 PNG 的 $output[0] != 1 sometimes true ,但是RMagick 的 Image.alpha? 是 < $myDir 中的 PNG 总是 true?我错过了什么吗?

我认为让我回到正轨的最好方法是,如果有人可以向我解释转换命令到底在做什么(包括表达式 %[fx:ua])。

更新:与此同时,我已经编写了需要此信息的脚本。如果对您有任何帮助,您可以在 Github 上查看

I'm failing at translating the following PHP/ImageMagick code into Ruby RMagick (to make it more manageable for future users and to understand what it's really doing):

$output = array();
$returnValue = 0;
$pngFiles = $myDir->find("/.png$/i");
foreach($pngFiles as $pngFile) {
   $cmd = 'convert '.$pngFile->path.' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info:'
   exec($cmd, $output, $returnValue);
   if($output[0] != 1) {
      logMessage("PNG file contains some alpha transparency and will not be modified");
   }
}

By now I thought I more or less understood what the convert-command is doing, but translating it to RMagick makes me rethink that.

For example: Why is $output[0] != 1 sometimes true on PNGs in $myDir, but RMagick's Image.alpha? is always true on PNGs in $myDir? Am I missing something?

I think the best way to get me back on track would be, if anyone could explain to me what the convert-command is exactly doing (including the expression %[fx:u.a]).

Update: In the meantime I've written the script I needed this information for. You can check it out at Github if it's to any help to you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦年海沫深 2024-10-08 11:46:14

该代码检查特定图像是否包含透明度。

-format '%[fx:ua]' info:

这指示 image magick 检查第一个图像 u,即 a 的 Alpha 通道并输出它的信息,如果左上角像素是透明的,它将返回 0,如果不是,我认为它会返回非零。这就是为什么将图像大小调整为 1x1,以便只需要参考单个像素。 -channel o 是不透明通道。

因此,英文代码将读取、循环浏览所有 PNG 文件,仅查看 Alpha 通道(不透明度),调整大小为单个像素并查看其是否透明。因此出现了回显消息。

不幸的是,我不知道 Ruby 或 RMagick,但快速浏览一下 API 似乎建议使用 image.channel(AlphaChannel) 来获取 alpha 通道(AlphaChannel 是一个ChannelType 值,不确定是否必须指定 ChannelType.AlphaChannel),然后按照 .resize(1,1) 获取大小向下,然后使用 .pixel_color(0,0).get_pixels(0,0,1,1) 来获取 Pixel对象返回(get_pixels() 返回一个数组),我相信它有一个 opacity 属性。但是,channel() 命令将 RGB 值更改为所选通道的值,并且我不确定它是否保留了不透明度通道,因此您可能只需要查看红色,或者省略.channel() 完全调用 - 尽管我不知道这是否会立即破坏结果。

也许如果 Ruby 支持像样的函数式编程方法的话。

image.channel(AlphaChannel).resize(1,1).pixel_color(0,0).red

或者如果 pixel_color() 由于某种原因没有返回不透明度

image.channel(AlphaChannel).resize(1,1).get_pixels(0,0,1,1)[0].red

如果没有 channel() 调用,它将是:

image.resize(1,1).pixel_color(0,0).opacity

或者

image.resize(1,1).get_pixels(0,0,1,1)[0].opacity

再次,我的 Ruby 不存在,所以你可能需要对它们进行大量重新排列,但原语就在那里。

参考资料

  1. RMagick 文档
  2. ImageMagick 'fx' 转义
  3. ImageMagick -通道选项

The code checks to see if a particular image contains transparency.

-format '%[fx:u.a]' info:

This instructs image magick to check the first image u, the alpha channel of that a and output info on it, it will return 0 if the top-left pixel is transparent and non-zero if not I think. That is why the image is being resized to 1x1, so that only a single pixel needs to be consulted. The -channel o is the opacity channel.

So the code in English would read, cycle through all PNG files, look at the alpha channel (opacity) only, resize to a single pixel and see if it's transparent. Hence the echo message.

Unfortunately I do not know Ruby, or RMagick, but a quick look at the API seems to suggest using image.channel(AlphaChannel) to get the alpha channel (AlphaChannel is a ChannelType value, not sure if you have to specify ChannelType.AlphaChannel), then follow with .resize(1,1) to get the size down, and finish with either .pixel_color(0,0) or .get_pixels(0,0,1,1) to get the Pixel object back (get_pixels() returns an array), which I believe has an opacity attribute. However, the channel() command changes the RGB values to the value of the channel selected, and I'm not sure it preserves the opacity channel so you might just need to look at red forinstance, or omit the .channel() call entirely - though I do not know if that would disrupt the result offhand.

Perhaps if Ruby supports decent functional programming approaches.

image.channel(AlphaChannel).resize(1,1).pixel_color(0,0).red

or this if pixel_color() does not return the opacity for some reason

image.channel(AlphaChannel).resize(1,1).get_pixels(0,0,1,1)[0].red

Without the channel() calls it would be:

image.resize(1,1).pixel_color(0,0).opacity

or

image.resize(1,1).get_pixels(0,0,1,1)[0].opacity

Again, my Ruby is non-existent, so you may have to rearrange those a lot, but the primitives are there.

References

  1. RMagick Documentation
  2. ImageMagick 'fx' escapes
  3. ImageMagick -channel options
塔塔猫 2024-10-08 11:46:14

为仍在寻找解决方案的任何人发布此内容,有一种方法可以使用此处描述的 rmagick gem 在 ruby​​ 中执行此操作 https://stackoverflow.com/a/41282162/1975112

Posting this for anyone who is still looking for a solution to this, there is a way of doing this in ruby using the rmagick gem described here https://stackoverflow.com/a/41282162/1975112

清君侧 2024-10-08 11:46:14

您显示的 php 代码(实际上只是发送要在 shell 中进行转换的工作)不会检查图像是否具有 Alpha 通道,它只是获取给定的任何文件并将其打开。如果它已经有一个,则不会有文件更改,但不会要求 Convert 根据状态做出任何决定,只需继续添加通道即可。

The php code you show (which really just sends the work to convert in a shell) does not check to see if the images have alpha channels, it just takes whatever file is given and turns it on. If it already had one there would be no file change, but convert is not being asked to make any decision based on the status, just go ahead and add the channel.

‖放下 2024-10-08 11:46:14

你为什么不只复制命令并用系统调用它?

不需要宝石...没有问题,代码应该看起来几乎相同。

编辑: RMagic 仅包装 imagemagic,所以如果您已经有 imagmagic 命令字符串,为什么还要为 rmagic 烦恼呢?

why did you not copy just the command and call it with system ?

no gems required ... no questions, code should look almost the same.

EDIT: RMagic only wraps imagemagic so why to bother with rmagic if you already have imagmagic command string.

兔小萌 2024-10-08 11:46:14

我从未编写过一行 Ruby 代码,但这里是 PHP 脚本的部分重制:

require 'find'

pngFiles = Dir.glob("*.png")

Find.find('./') do |f|
  if system 'convert ' + f + ' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info' do
    print "PNG file contains some alpha transparency and will not be modified"

我认为您唯一需要检查的只是 alpha 通道,而不是所有那些复杂的东西。查看此页面以获取更多信息: http://www.imagemagick.org/script/escape.php 。它有一个 %A 运算符,可输出有关 alpha 通道的信息。

I've never coded a single line of Ruby, but here is a partial remake of the PHP script:

require 'find'

pngFiles = Dir.glob("*.png")

Find.find('./') do |f|
  if system 'convert ' + f + ' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info' do
    print "PNG file contains some alpha transparency and will not be modified"

I think that the only thing you need to check for is just an alpha channel, not all of that complicated stuff. Look at this page for more info: http://www.imagemagick.org/script/escape.php. It has a %A operator, and that outputs something about the alpha channel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文