4

I've got a src block that has an image file as a result and I want that image file to be exported as a certain size.

I know I could apply the property #+ATTR_HTML: :width 100px, but I can't figure out a way to do that automatically when the result is produced.

The specific src block I'm using probably isn't necessary for an answer, but here it is anyway.

#+BEGIN_SRC python :noweb no-export :results file :exports both
<<extract-cells-from-table>>
image = cv2.imread("resources/examples/example-table.png", cv2.IMREAD_GRAYSCALE)
cell_images_rows = extract_cell_images_from_table(image)
cv2.imwrite("resources/examples/example-table-cell-1-1.png", cell_images_rows[1][1])
"resources/examples/example-table-cell-1-1.png"
#+END_SRC

#+RESULTS:
[[file:resources/examples/example-table-cell-1-1.png]]

2 Answers 2

0

I've figured out something that works but isn't perfect.

#+NAME: html-image-size
#+BEGIN_SRC emacs-lisp :var text="" :var width="100%" :var height="100%" :results raw
(concat "#+ATTR_HTML: :width " width " :height " height "\n[[file:" text "]]")
#+END_SRC

Then I can set the :results of a src block to raw and add that emacs-lisp src block as a :post header arg.

#+BEGIN_SRC python :noweb-ref test-detect-table :noweb yes :results raw :post html-image-size(text=*this*, width="300px")
import cv2

<<detect-table>>

image_filename = "resources/examples/example-page.png"
image = cv2.imread(image_filename, cv2.IMREAD_GRAYSCALE)
image = find_tables(image)[0]
cv2.imwrite("resources/examples/example-table.png", image)
"resources/examples/example-table.png"
#+END_SRC

That is almost perfect. The only thing I've noticed that I lose is org-babel-remove-result no longer works. It just removes the #+RESULTS: line while the file link remains.

Anyone know of a solution to that?

Edit: Found a solution.

There's no configurable option. There is a defconst regex that org-babel uses to find the start/end of result blocks. But I wrote a function to remove the #+ATTR.* lines that were causing the built-in regex to fail.

(defun remove-attributes-from-src-block-result (&rest args)
  (let ((location (org-babel-where-is-src-block-result))
        (attr-regexp "[     ]*#\\+ATTR.*$"))
    (when location
      (save-excursion
        (goto-char location)
        (when (looking-at (concat org-babel-result-regexp ".*$"))
          (next-line)
          (while (looking-at attr-regexp)
            (kill-whole-line)))))))

(advice-add 'org-babel-remove-result :before #'remove-attributes-from-src-block-result)
(advice-add 'org-babel-execute-src-block :before #'remove-attributes-from-src-block-result)
4
  • Setting :results to raw causes the inability to remove the result automatically: there is no way for org-babel to figure out what is part of the result and what is not. So the only solution to that problem is to not use raw. I presume you are using the solution in your edit without :results raw?
    – NickD
    Apr 10, 2020 at 20:13
  • It's working fine, even with :results raw.
    – Eric Ihli
    Apr 10, 2020 at 20:50
  • Well, I find that hard to believe, although it might be working fine for your application. What happens if your output is more than one line? What happens if your output includes a line with #+ATTR.... in it?
    – NickD
    Apr 11, 2020 at 0:08
  • Look to org-babel-remove-result and org-babel-result-end. The advice-add I mention removes the ATTR lines. org-babel-remove-result and org-babel-result-end handle removing the actual result (regardless of how many lines), nothing I mention changes the org-babel-remove-result behavior.
    – Eric Ihli
    Dec 5, 2020 at 21:56
0
:wrap "org \n#+ATTR_HTML: :width 200"

seems to work:

#+begin_src mermaid :background-color transparent :wrap "org \n#+ATTR_HTML: :width 200" :file test..nogit..png
sequenceDiagram
 A-->B: Works!
#+end_src

#+RESULTS:
#+begin_org 
#+ATTR_HTML: :width 200
[[file:test..nogit..png]]
#+end_org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.