mogrify icon indicating copy to clipboard operation
mogrify copied to clipboard

Write to standard out

Open tielur opened this issue 6 years ago • 12 comments

Does this library have the ability to create an image and then write to standard out without having to interact with the file system?

tielur avatar Mar 30 '18 05:03 tielur

Not currently, but a pull request would be very welcome. I think most ImageMagick commands support writing to standard out.

talklittle avatar Mar 30 '18 06:03 talklittle

I started looking at this as I kinda have a need for it as well. It looks fairly straight forward, but I'm struggling with what the API should look like (I think I've actually spent more time thinking about the API than it would to actually do the work based on what I'm looking at for the imagemagick docs 😓 )

For my usage I've got an inline base64 encoded image that I'd like to resize and get an inline output back.

The best idea I've come up with is to effectively pass the base64 encoded image to open, and populate the path. For the output, I'd do the same, if save is passed with an inline: true option. This would work, but it feels wrong because its not really a path anymore. Before I open up an editor and submit a PR I'd appreciate if anyone else has any better ideas that doesn't substantially change the api but also doesnt feel yucky.

theangryangel avatar Jun 01 '18 13:06 theangryangel

The work I was doing that would involve this has been put on the back burner so I never looked much further into it. I'd love to see this implemented though and I'd be happy to help.

tielur avatar Jun 01 '18 18:06 tielur

I spent an hour with this the other evening and its functionally working. Unfortunately;

  • I broke a load of tests
  • I'm not very pleased with the hacks and workarounds

Part of the pain is that a bunch of functions all assume the input and output are file system based and take actions based on extensions, the path, etc. which for my use case is a pain. For just output these mostly dont really matter and it's a special case in the save function, basically (output path of format:-, capture and a small bit of manipulation - literally thats it).

I'd like to come up with something a more elegant/cleaner, that allows both input and output without touching the file system, hopefully without breaking the API (although I'm not entirely convinced thats going to be possible and API design is my weakest skill).

I'm going away on my first proper holiday in several years next week (🎉 ), and I'm not sure how much time I'll be able to put into this until the end of June. If someone wants to pick up before then I won't get upset 😄

theangryangel avatar Jun 05 '18 09:06 theangryangel

Hello @theangryangel -- I am interested about this issue, do you have a branch with the "hack"? I might give it a try.

kpanic avatar Nov 20 '18 19:11 kpanic

@theangryangel I opened #62

kpanic avatar Nov 20 '18 20:11 kpanic

Merged #62 which adds buffer support to create/2.

Before release I'd like to:

  • ~~Cleanup the API so create/2 implementations return consistent types~~ (Done in #63)
  • ~~Document the new options~~ (Done in #63)
  • Look into whether buffer support is possible for save/2

talklittle avatar Nov 25 '18 19:11 talklittle

@talklittle thanks for you hard work! @kpanic thank you!

route avatar Nov 26 '18 11:11 route

I'm looking into mogrify with the -write - option to write to stdout, for the save/2 function. It kind of works except for a bug in the mogrify command-line application (tested with version 6.9.7-4) where it writes the buffer twice.

$ mogrify -write - -resize 100x100 bender.jpg | wc -c
8558

Compared with regular operation:

$ mogrify -write tmp.jpg -resize 100x100 bender.jpg
$ cat tmp.jpg | wc -c
4279

Work so far is at branch https://github.com/talklittle/mogrify/tree/save-buffer but I consider this upstream bug a blocker.

talklittle avatar Nov 30 '18 10:11 talklittle

Same behaviour on 6.9.10-14

$ mogrify -write - -resize 100x100 bender.jpg | wc -c
8552
$ mogrify -write tmp.jpg -resize 100x100 bender.jpg
$ wc -c tmp.jpg 
4276 tmp.jpg
$ mogrify --version
Version: ImageMagick 6.9.10-14 Q16 x86_64 20181023 https://imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib

kpanic avatar Dec 01 '18 22:12 kpanic

@talklittle I've opened an issue on imagemagick.

They are fixing it, but apparently you can pass +write instead of -write as a workaround for the double output.

lucacorti avatar May 16 '21 17:05 lucacorti

@lucacorti Thanks for opening the ImageMagick issue.

I tested the +write workaround (https://github.com/talklittle/mogrify/tree/save-buffer-pluswrite), and it works for the buffers, but with a side effect that breaks other functionality and Mogrify tests.

The documentation says (https://imagemagick.org/script/command-line-options.php#write):

To restore the image to its original state after writing it, use the +write filename option.

It apparently tells ImageMagick to rewrite the input image file to disk. This is a problem because it apparently can change the bytes and width/height of the original image. Tested on ImageMagick 6.9.7.

So unfortunately we can't use +write as an immediate workaround.

When the upstream fix becomes stable, we can merge the code to use buffers. We'll probably want to raise an error if trying to use the feature on older ImageMagick versions.

talklittle avatar May 23 '21 21:05 talklittle