In ZSH, redirecting image file into graphic magick results in corrupt jpeg data (but not when using file path parameters) - shell

Currently in my ZSH shell.
Doing this, gives me:
> gm convert -compress JPEG - - < input.jpg > output.jpg
gm convert: Corrupt JPEG data: 873 extraneous bytes before marker 0xd9 (/tmp/gmo1fx92).
However these 2 work without any problems:
> cat input.jpg | gm convert -compress JPEG - - > output.jpg
> gm convert -compress JPEG input.jpg output.jpg
What could be causing this difference?

Related

Pngs are blank when using convert

The following is giving me a blank image of the expected size:
magick convert a.png b.png -compose difference -composite out.png
However, I can produce the correct output as a jpg:
magick convert a.png b.png -compose difference -composite out.jpg
I then tried to convert this to a png and it works fine:
magick convert out.jpg out.png
Why is the first command giving me a blank image and how might I fix the problem?
Libpng version 1.6.37.
Version: ImageMagick 7.0.11-8 Q16 x86_64 2021-04-17 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5)
Delegates (built-in): bzlib freetype gslib heic jng jp2 jpeg lcms lqr ltdl lzma openexr png ps tiff webp xml zlib
UPDATE:
Bmp is also blank:
magick convert a.png b.png -compose difference -composite out.bmp
And this also works for jpg but not png:
composite -compose difference a.png b.png out.png
And this is what I see when running verbose:
a.png PNG 500x700 500x700+0+0 8-bit sRGB 125771B 0.030u 0:00.016
b.png PNG 500x700 500x700+0+0 8-bit sRGB 90207B 0.020u 0:00.012
a.png=>b.png PNG 500x700 500x700+0+0 8-bit sRGB 90207B 0.450u 0:00.084
This is working and is giving my the required output:
composite -verbose -compose minus a.png b.png out.png

Create two sizes of an image using one ImageMagick command

I am using ImageMagick to generate small size JPEG versions of large TIFF images. For each TIFF image I must generate two smaller JPEG versions.
I am currently using two convert commands:
convert.exe 4096-by-3072px-120mb.tif -resize "1024x>" -strip -interlace Plane 1024px-wide-for-web.jpg
convert.exe 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane 1600px-wide-for-web.jpg
Converting TIFF to JPEG one-by-one is taking too much time. This approach is inefficient as each image is loaded across the network and processed twice. It will get worse as I plan to create more sizes for each TIFF (think 10,000 TIFFs x 5 sizes).
So, is it possible to generate two or more output files of different sizes using a single ImageMagick command?
Yes, it is possible, using the -write option:
convert 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane \
-write 1600px-wide-for-web.jpg -resize "1024x>" 1024px-wide-for-web.jpg
which rescales the input image to 1600 pixels wide, writes it out, then rescales the result to 1024 pixels wide and writes that. It's important to write the images in descending order of size, to avoid loss of quality due to scaling to a small size and then back up to a larger one.
If you prefer to rescale both images from the input image, use the +clone
option:
convert 4096-by-3072px-120mb.tif -strip -interlace Plane \
\( +clone -resize "1024x>" -write 1024px-wide-for-web.jpg +delete \) \
-resize "1600x>" 1600px-wide-for-web.jpg
In this case the order of writing images does not matter.
Here is an alternate command that uses memory program register:
magick.exe 4096-by-3072px-120mb.tif -write mpr:main +delete ^
mpr:main -resize "1024x>" -quality 80 -interlace Plane -strip -write 1024px-wide-for-web.jpg +delete ^
mpr:main -resize "1280x>" -quality 80 -interlace Plane -strip -write 1280px-wide-for-web.jpg +delete ^
mpr:main -resize "1600x>" -quality 80 -interlace Plane -strip -write 1600px-wide-for-web.jpg +delete ^
mpr:main -resize "2048x>" -quality 80 -interlace Plane -strip 2048px-wide-for-web.jpg
When tested:
the files generated with this command were identical to those created by separate convert commands
this command was twice as fast compared to separate commands
Note: ^ is the line continuation character on Windows.

Imagemagick depth convert

Having an imgage in.png. Now do the next:
$ convert in.png -strip out.tiff #convert to tiff
$ convert out.tiff -strip out.png #and back
$ cmp in.png out.png
$#no output - the images has no difference - theyre same
$ identify in.png out.png
in.png PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out.png[1] PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
now trying this via eps. So:
$ convert in.png -strip out2.eps #convert to eps
$ convert out2.eps -strip out2.png #back
and the images are different.
$ identify in.png out.png out2.png
in.png PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out.png[1] PNG 300x300 300x300+0+0 16-bit sRGB 1.65KB 0.000u 0:00.000
out2.png[2] PNG 300x300 300x300+0+0 8-bit sRGB 4c 321B 0.000u 0:00.009
As you can see, the conversion eps -> png creates 8-bit png.
Curiosity plus - the EPS is 16 bit!
$ identify out2.eps
out2.eps PS 300x300 300x300+0+0 16-bit sRGB 1.42KB 0.000u 0:00.000
Questions:
why the eps->png conversion changes the bit-depth?
how to convert from eps to png to get 16-bit png? (as from tiff).
EDIT
Tested #Rachel recommendation:
$ convert out2.eps -depth 16 out3.png
$ identify out3.png
out3.png PNG 300x300 300x300+0+0 8-bit sRGB 4c 518B 0.000u 0:00.000
8 bit again.
My ImageMagick version:
$ convert --version
Version: ImageMagick 6.8.9-1 Q16 x86_64 2014-06-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib djvu fftw fontconfig freetype gslib gvc jng jp2 jpeg lcms ltdl lzma pangocairo png ps tiff webp wmf x xml zlib
If anyone want test, here is my test image:
FINAL
So, #Mark Setchell and #Rachel Gallen's recommendation are good. #Marks command works, so the key is PNG48.
$ convert out2.eps -depth 16 PNG48:out7.png
$ identify out7.png
out7.png PNG 300x300 300x300+0+0 16-bit sRGB 1.86KB 0.000u 0:00.000
Finally 16-bit png. What I will ask it in another question, why the next:
$ mogrify -strip out7.png
$ identify out7.png
out7.png PNG 300x300 300x300+0+0 8-bit sRGB 4c 321B 0.000u 0:00.000
8 bit again. And the -strip should remove only metadata and should not change the image itself.
I think you need something along these lines to get 16-bit PNGs
convert a.png -depth 16 PNG48:b.png
i think from looking at the docs you can do
convert -depth 16 image.eps image2.png[16]
the syntax is
convert [ options … ] file [file …] file
This site says 'Use an optional index enclosed in brackets after a file name to specify a desired subimage of a multi-resolution image format like Photo CD (e.g. img0001.pcd[4]) or a range for MPEG images (e.g. video.mpg[50-75]).'
so maybe put the [16] after image2.png

Imagemagick - split image and pipe to convert?

I wish to split and image and then turn it into an animated gif with a single command.
At the moment, I'm doing
mogrify -resize 200%x100% -crop 50%x100% image.jpg
Then I can do
convert -delay 25x100 -loop 0 image-0.jpg image-1.jpg movie.gif
Is there a way that I can pipe the output of mogrify to the input of convert?
I tried running
convert -delay 25x100 -loop 0 <(mogrify -crop 50%x100% m.jpg :-) test.gif
But it failed with the error
mogrify: no decode delegate for this image format `/tmp/magick-bHWzk1Xl' # error/constitute.c/ReadImage/532.
convert: no decode delegate for this image format `/tmp/magick-mVXjCXLd' # error/constitute.c/ReadImage/532.
convert: missing an image filename `test.gif' # error/convert.c/ConvertImageCommand/3011
Close, but I think you're missing an extra '-'
Try :
mogrify -resize 200%x200% -crop 50%x100% -delay 10 input.jpg | convert - -delay 25x100 -loop 0 input-0.jpg input-1.jpg output.gif
This does throw an error, but works. It will also destroy the original, so you might want to use an output filename eg:
mogrify -resize 200%x200% -crop 50%x100% -delay 10 -write output.jpg input.jpg | convert - -delay 25x100 -loop 0 output-0-0.jpg output-0-1.jpg output.gif
This will also complain, but also works as you require.

Error "non-conforming drawing primitive" when trying to draw text or images with graphics magick

I am migrating a javascript program that internally uses image magick, to a Windows batch file using graphics magick.
I cannot succesfully use the convert -draw command.
I have a couple of very simple test cases.The first one is
gm convert -size 1920x1080 xc:white -draw 'text 8,8 "Hello, world"' test.jpg
fails with the error message "non-conforming drawing primitive (text)".
The other
gm convert -size 1920x1080 xc:white -draw 'image over 8,8 0,0 "img.jpg"' test.jpg
fails with "non-conforming drawing primitive (image)".
They both fail when invoked from the command line and when invoked from inside a BAT file.
I have tried with and without quotes ".
I have tried escaping with ^ the quotes ^".
I have tried escaping the single quotes ^'.
I am obviously overlooking something very simple, I just cannot see. Any ideas?
EDIT Some attempts I made....
gm convert -size 1920x1080 xc:white -draw "'text 8,8 Hello'" test.jpg
gm convert -size 1920x1080 xc:white -draw "'text 8,8 \"Hello\"'" test.jpg
gm convert -size 1920x1080 xc:white -draw 'text 8,8 ^"Hello^"' test.jpg
gm convert -size 1920x1080 xc:white -draw "'text 8,8 ""Hello""'" test.jpg
but
gm convert -size 1920x1080 xc:white -draw "text 8,8 ""Hello"" " test.jpg
fails with "unable to read font"
Use double quotes: -draw "image over 8,8,0,0 ""img.jpg"" " ?

Resources