membrane

pez 2023-10-25T16:49:27.619169Z

The size param when creating an image does not do what I expect it to do. With an image that is 1844 × 1218 and I create an ui/image like the following, I get an image that is 1200 × 127. I expect (and want) it to be 1200x792 (or 793, I don’t think it matters how we round here).

(def original-path "test-data/out/to-chatgpt-a-picture-says-more-than-a-thousand-words/image3.png")
  (->> (ui/image original-path [1200 nil])
       (save! (str original-path "-1200.jpg") :jpg 80))
Am I doing it wrong, or is it membrane?

phronmophobic 2023-10-25T16:58:21.471969Z

not totally sure what save! does, but it seems to be working for me locally

phronmophobic 2023-10-25T16:58:53.883999Z

(let [original-path "/Users/adrian/Downloads/test-img.png"]
  (skia/save-image (str original-path "-1200.jpg")
                   (ui/image original-path [1200 nil])))

phronmophobic 2023-10-25T17:01:36.376139Z

assuming it's the save! from x-meta-image, it seems to work for me.

pez 2023-10-25T17:10:44.436039Z

I copied that save! from there 😀 Maybe something is wrong with my repl. I’ll try restarting it.

phronmophobic 2023-10-25T17:12:59.106839Z

yea, if it's still not working, I would check the ui/bounds of the original image and the aspect resized image to try and figure out where the problem might be. This could happen if it measured the original image size incorrectly.

pez 2023-10-25T17:44:35.834729Z

Funny thing was that if I measured it myself and calculated the height, that worked. Afk now, so can’t experiment.

pez 2023-10-25T18:20:25.242219Z

Restarting the repl helped! WTF was that about. The repl had been running for a few weeks, but anyway. Well, well, I’m happy. And I guess I just destroyed any reproduction attempt anyway. 😃

phronmophobic 2023-10-25T18:20:52.849209Z

weird.

phronmophobic 2023-10-25T18:21:03.095979Z

glad it's working again though

phronmophobic 2023-10-25T18:21:18.106139Z

there is an image cache.

phronmophobic 2023-10-25T18:21:52.608299Z

It's possible that if you loaded the image on disk, loaded the image with membrane, and then changed the image on disk that it might give weird results

phronmophobic 2023-10-25T18:23:30.810909Z

there's probably a better API that makes loading more explicit

pez 2023-10-25T18:59:12.774479Z

The image is downloaded as part of the process. So it is “changed” in that sense. Wait… The images change and reuse names as part of the re-download. So image1.png can get to be named image5.png the next run. The cache could screw things up then, I guess. Is there a way I can invalidate the cache?

phronmophobic 2023-10-25T19:01:59.253549Z

There is, but that kinda breaks a bunch of the benefits you get from immutability.

phronmophobic 2023-10-25T19:02:32.907949Z

There's at least two other options that I think are better:

phronmophobic 2023-10-25T19:04:01.079099Z

1.`ui/image` loads the image via protocol. Instead of passing a path which points to a mutable file, I would recommend reading the image contents into a byte-array and passing that to ui/image

pez 2023-10-25T19:04:33.468609Z

Sounds good!

phronmophobic 2023-10-25T19:05:03.278279Z

2. If you want to get fancier, you can create a new implementation of the protocol that loads an image based on whatever data makes sense.

phronmophobic 2023-10-25T19:05:12.980649Z

actually, there's another 3rd option if you're loading from a url

phronmophobic 2023-10-25T19:05:29.768369Z

just use (ui/image ( url-str))

pez 2023-10-25T19:05:50.131329Z

Oh, well, I have nothing against fancy. But I am very fond of simple + easy. 😃

phronmophobic 2023-10-25T19:06:04.360179Z

there's an implementation of the image loading for http://java.net.URL

pez 2023-10-25T19:07:16.048919Z

The files are downloaded in a different part of the system. Then they are on disk. I think reading it into a byte array and pass that to ui/image is my choice here.

👍 1
phronmophobic 2023-10-25T19:07:57.604699Z

here's a snippet that I use for that:

(defn- slurp-bytes
  "Slurp the bytes from a slurpable thing"
  [x]
  (with-open [out (java.io.ByteArrayOutputStream.)]
    ( ( x) out)
    (.toByteArray out)))

pez 2023-10-25T19:08:24.262289Z

Haha, you are making my day! Thanks!

pez 2023-10-25T19:15:31.746549Z

Works like a charm! I forced the shuffling of the file names now to test, and it was indeed what screwed up the scaling. With you slurper all is good. Thanks! 🙏

1