Search code examples
imageimagemagickjpegimage-conversion

How to use image magick to generically convert images


I want to convert images with generic extensions, I have this:

cat "$1" | convert 'jpeg:-' -resize '64x64' - | gsutil cp - "gs://images.xxx.com/${sha_sum}-64x64-${file_without_folder}"

the above works. But if I omit the 'jpeg:-' it won't work. Is there a way to generically convert any file type? Perhaps I need the 'jpeg:-' so that image magick knows what the file encoding type is? Is there a way for image magick to auto-detect the encoding perhaps?

I am looking to do this:

cat "$1" | convert -resize '64x64' - | gsutil cp - "gs://images.xxx.com/${sha_sum}-64x64-${file_without_folder}"

so that I can generically upload an image no matter the file type (encoding type).


Solution

  • You can, but you still have to specify the input stream:

    cat "$1" | convert - -resize '64x64' - | gsutil cp - "gs://images.xxx.com/${sha_sum}-64x64-${file_without_folder}"
    

    I assume this is a minimal example and you really are required to read the image from standard in; if not, just specify the image directly:

    convert "$1" -resize '64x64' - | gsutil cp - "gs://images.xxx.com/${sha_sum}-64x64-${file_without_folder}"
    

    Note that not all formats can be detected from the stream data. Use

    identify -list format
    

    to see the types that can be read.