Piping an image retrieved using curl to sips to change format without saving intermediate file

489 views Asked by At

I have url links to image files I want to retrieve from the internet.

I can download the files using curl without issue using:

curl "https://...web address..." > myfileName;

The image files are of various types, some .bmp some .jpg etc. I have been using sip in Terminal on Mac osx to convert each to .png files using:

sips -s format png downloadFileName  --out newFileName.png

This works well on files I've saved as downloadedFileName regardless of the starting file type.

As I have many files to process I wanted to pipe the output of the curl download directly into sips, without saving an intermediate file.

I tried the following (which combines my two working steps without the intermediate file name):

curl "https://...web address..." | sips -s format png  --out fileName.png

And get a no file error: Error 4: no file was specified.

I've searched the sip man pages but cannot find a reference for piped input and have been unable to find a useful answer searching SO or google.

Is there a way to process an image downloaded using curl directly in sips without first saving the file?

I do not necessarily need the solution to use a pipe, or even be on one line. I have a script that will cycle through a few thousand urls and simply want to avoid saving lots of files that will be deleted a line later.

I should add, I do not necessarily need to use sips either. However, any solution must be able to handle image files of unknown type (which sips does admirably) as no file extension is present on the files.

2

There are 2 answers

3
Arkadiusz Drabczyk On

I don't have sips installed but its manpage indicates that it cannot read from stdin. However, if you use Bash or ZSH (MacOS default now) you can use process substitution, in this example I use convert which is a part of ImageMagick and can convert different image types too:

$ convert <(curl -s https://i.kym-cdn.com/entries/icons/mobile/000/018/012/this_is_fine.jpg) this_is_fine.png
$ file this_is_fine.png
this_is_fine.png: PNG image data, 800 x 450, 8-bit/color RGB, non-interlaced

After doing that this_is_fine.png will be the only file in the directory with no temporary files

0
Knight Industries On

Apparently sips only reads regular files which makes it impossible to use /dev/stdin or named pipes.

However, it is possible using the mature and feature-rich convert command:

$ curl -sL https://picsum.photos/200.jpg | convert - newFilename.png
$ file newFilename.png
newFilename.png: PNG image data, 200 x 200, 8-bit/color RGB, non-interlaced

(First install ImageMagick via brewinstall imagemagick or sudoportinstall ImageMagick.)

ImageMagick permits image data to be read and written from the standard streams STDIN (standard in) and STDOUT (standard out), respectively, using a pseudo-filename of -.

source, section STDIN, STDOUT, and file descriptors