0

I'm calling imagemagik's convert program from within ruby to convert image types. I'm redirecting both stdout and stderr to /dev/null but I'm still getting console text. It only occurs when converting FROM webp so I suspect it's output from the Ubuntu webp package.

buffer = `convert -quiet "#{temp_dir}#{tmp_image_filename}" "#{temp_dir}#{new_image_filename}" > /dev/null 2>&1`

Output:

Decoded /tmp/magick-3658rrhNn7wh4IW2. Dimensions: 580 x 300 . Format: lossy. Now saving...
Saved file /tmp/magick-3658nGuNL-bzCkRA

Is this tty output? I can't figure out how to capture and suppress it. I added the quiet attribute to convert command line but it had no affect (another reason I suspect webp is the culprit). I've tried several other tips in my stackoverflow search with system, IO, wrapping in : $(...), etc to no avail. Any help?

Thanks! Eric

Eric M
  • 109
  • 1
  • 6
  • 1
    Have you tried using [`Open3`](https://ruby-doc.org/stdlib/libdoc/open3/rdoc/Open3.html)? Backticks are only suitable for quick'n'dirty hacks IMO. – mu is too short Nov 06 '20 at 22:03
  • No I haven't, looks like a good option. There's actually an example with convert in the docs LoL! I'll report back... THX! – Eric M Nov 06 '20 at 22:09
  • 1
    Open3 will also save you from launching a shell (which will parse the command line and then exec `convert`) and leave you open to unpleasant quoting and escaping issues that can cause a lot of problems unless you're very careful. Open3 won't involve a shell so a lot of problems just go away. – mu is too short Nov 06 '20 at 22:14
  • Open3 is the way to go. Backticks are a dirty hack and honestly shouldn't even be allowed in Ruby without a special flag (`ruby --yolo` perhaps?). They're exceptionally dangerous if used incorrectly. – tadman Nov 06 '20 at 23:15
  • using Open3.capture3 produces the same results. The convert output (more likely the webp output) bleeds thru to the console... – Eric M Nov 07 '20 at 01:17
  • What happens if you run it manually in the shell with output redirection. Same problem? – Casper Nov 07 '20 at 06:27
  • I found the code where this output comes from. It is a command `dwebp` which is used as a delegate for IM. It's a simple `printf` statement. You can search for `now saving` in this code: https://searchcode.com/file/22469641/libwebp-0.1.3/examples/dwebp.c/ – Casper Nov 07 '20 at 06:40
  • I cannot reproduce your problem. Using backticks with 2>&1 redirection gets rid of the webp output on Ubuntu 18.04. – Casper Nov 07 '20 at 06:48
  • Thanks everyone. For some reason it's working now. Open3.capture3 successfully suppresses the output, tho I don't know why it wasn't before. Now I can't even reproduce the case where capture3 allows anything to slip thru. Thanks for tips! – Eric M Nov 22 '20 at 21:56

1 Answers1

0

Question was answered in the comments. I moved to Open3.capture3 using a format like below and the console text from webp is captured.

stdout, stderr, status = Open3.capture3("convert -flatten \"#{$temp_dir}#{tmp_image_filename}\" \"#{$temp_dir}#{@image_filename}\"")
Eric M
  • 109
  • 1
  • 6