How do I store the output of ImageMagick into a Bash variable (and then use it)?

I am using ImageMagick and need to conditionally resize images.

To do this, I store the results of the tool identify

in variables.

$infile='test.jpg'
width=$(identify -ping -format %w $infile)
height=$(identify -ping -format %h $infile)

      

But before resizing, I want to do some transformations that resize the image: -trim

and -shave

. So I need to calculate the size of the image between cropping and resizing. And I would like to do the trim operation only once to do a little optimization.

So, I would like:

  • trim and shave
  • store [binary] leads to the variable (for example $data

    )
  • pass the $data

    value of the variable as an input to the tool identify

    and store its result for conditional resizing
  • transfer $data

    to convert

    tool and complete processing

Something like that:

data=$(convert logo: -shave 1x1 gif:-)
width=$(echo $data | identify -ping -format %w gif:-)
echo $data | convert -resize "$width"

      

But echo

doesn't work as needed.

P. S. convert

and identify

are tools from the ImageMagick package

+3


source to share


1 answer


Bash cannot store blobs of data that may contain NULL

trailing characters. But you can convert the data to base64 and use ImageMagick fd:

.



# Store base64-ed image in `data'
data=$(convert logo: -shave 1x1 gif:- | base64)
# Pass ASCII data through decoding, and pipe to stdin file descriptor
width=$(base64 --decode <<< $data | identify -ping -format %w fd:0)
base64 --decode <<< $data | convert -resize "$width" -

      

+5


source







All Articles