Skip to content

Commit

Permalink
Fixes #92
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Feb 28, 2024
1 parent 7689bc0 commit 9170e69
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/tech/v3/libs/buffered_image.clj
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,27 @@
1 :byte-gray
3 :byte-bgr
4 :byte-abgr))))


(defn tensor->image
"Convert a tensor directly to a buffered image. The values will be interpreted as unsigned
bytes in the range of 0-255.
Options:
* `:img-type` - Force a particular type of image type - see keys of [[image-types]]."
(^BufferedImage [t options]
(let [t (dtt/ensure-tensor t)
dims (dtype-proto/shape t)
[y x c] (case (count dims)
2 [(first dims) (second dims) 1]
3 dims
(throw (RuntimeException. "Unrecognized tensor shape for image conversion - " dims)))
img-type (or (get options :img-type)
(case (long c)
1 :byte-gray
3 :byte-bgr
4 :byte-abgr))
dst (new-image y x img-type)]
(dtype-cmc/copy! t (as-ubyte-tensor dst))
dst))
(^BufferedImage [t] (tensor->image t nil)))

0 comments on commit 9170e69

Please sign in to comment.