How to force ImageMagick to produce single output image? - bash

I'm trying to convert tiff file to jpg in bash script, although I get 3 jpeg files (good, average and poor quality, I assume, same image) instead of one.
Here's an example of command:
convert -resize 200x200 -quality 90 "$CURRENT_DIR/$i" "$OP/$WITHOUT_EXT.thumb.jpg"
I've also tried without resize and quality params, but result is still the same.
Is it possible to get a single output file? Thank you!

try to add [] or [0] at end of source file:
convert -resize 200x200 -quality 90 "$CURRENT_DIR/${i}[0]" "$OP/$WITHOUT_EXT.thumb.jpg"

Related

Convert all .jpg images in directory to .pdf | Debugging ImageMagick

I have found multiple posts regarding this issue and everyone is recommending ImageMagick but for me, it doesn't seem to work as intended and I don't see too many docs on their site regarding .jpg->.pdf conversion.
Is there some alternative, preferably CLI tool?
Or can I somehow debug why ImageMagick doesn't work for me? I don't get any errors I just get corrupted files as a result.
My usecase
My os is Windows and
I have 64 .jpg files called 0.jpg, 2.jpg, ... 63.jpg and I would like to merge all those images into one .pdf file.
I have tried these commands:
magick *.jpg out.pdf
convert *.jpg out.pdf
but in both cases, I am unable to open the out.pdf file because it is corrupted. I have noticed that I can only convert 0.jpg file to pdf correctly but when I try to convert any other of my 64 jpg files then as a result I am getting a corrupted .pdf file
For example:
This gives me the correct .pdf:
magick 0.jpg 0.pdf
but this gives me corrupted .pdf:
magick 2.jpg 2.pdf
I assume that this a reason why I can't merge all of the files into one not corrupted .pdf file and my assumption is that there is something wrong with the rest of my .jpg files but I have no idea how to debug this issue. Every other .jpg file looks exactly the same as the one .jpg I can convert and all of them open without issues.
magick identify -verbose foobar.jpg results:
I can convert 0.jpg file to .pdf correctly but 2.jpg results in corrupted .pdf.
There are some apparent differences but I am not sure what those properties mean in the context of .jpg -> .pdf conversion
One thought is that someone has converted the grayscale image to color with 3 equal channels so that it IM says it has colorspace RGB. However, the JPEG colorspace tag is 2, which says it has no specific colorspace.
Properties:
date:create: 2021-04-01T17:29:06+00:00
date:modify: 2021-04-01T05:18:58+00:00
exif:ExifOffset: 46
exif:ExifVersion: 48, 50, 50, 48
exif:PixelXDimension: 960
exif:PixelYDimension: 1508
exif:Software: Google
jpeg:colorspace: 2
jpeg:sampling-factor: 2x2,1x1,1x1
From the JPG docs
ColorSpace
0 = Bi-level
1 = YCbCr, ITU-R BT 709, video
2 = No color space specified
3 = YCbCr, ITU-R BT 601-1, RGB
4 = YCbCr, ITU-R BT 601-1, video
8 = Gray-scale
9 = PhotoYCC
10 = RGB
11 = CMY
12 = CMYK
13 = YCCK
14 = CIELab
It is possible that this conflict or lack of colorspace may confuse certain viewers after the file is imbedded in a PDF vector shell.
Thank you #Mark Setchell for pointing me in the right direction by sharing this command:
magick identify -verbose XXX.jpg
My images are grayish so I don't know why majority of the files has sRGB colorspace but after converting colorspace to Gray I can convert them to .pdf properly (I don't see any difference when I open up my .jpgs after conversion to grayscale).
Using this command I can change colorspace to Gray:
magick 2.jpg -colorspace gray gray2.jpg
Then I can convert "grayscale" .jpgs to .pdf by simply:
magick gray2.jpg gray2.pdf
P.S.
If anyone has some better solution I will gladly accept it but if nothing shows up I am going to accept this
#fmw42
Here is a .zip file that contains
20.jpg - source image
20.pdf - corrupted pdf after running >magick 20.jpg 20.pdf
gray20.jpg - source image converted to gray by >magick 20.jpg -colorspace gray gray20.jpg
gray20.pdf - source image converted to .pdf after changing colorspace to gray first by >magick gray20.jpg gray20.pdf
Keep in mind that this image is from some weird manga but I have tried to pick the most normal page (it was harder than it seemed to be but this image itself is not nsfw)

Image Magick is generating black images while converting pdf to jpg

Image Magick version in use is 6.8.8.1.
Via command line on windows I am just trying to convert a *.pdf file into .jpg file using the following command
convert -density 100 -colorspace RGB "input.pdf" "output.jpg"
But the resulting output.jpg file is with a black image (there is no content). Any one out there could please guide on this ?
When you open this PDF-file by using a text-editor and it's header contains something like this:
%PDF-1.5
%¦éÏÄ
4 0 obj
<</Length 5 0 R/Filter /FlateDecode>>stream
....
....
then you have to decode this flat-encoded stream first before you can convert it to an image.
To solve this problem:
You can use the GUI-Tool pdftk free, set your environment-path to the bin-folder within the pdftk-folder and execute:
pdftk ENCODED_FILENAME.pdf output DECODED_FILENAME.pdf uncompress
in the shell to deflate/unzip this file.
Create a new file that is not encoded or zipped.
Hope this helps.
Set alpha flag to off in convert command.

How to crop 486 pixels from the bottom of each JPG image in the folder with ImageMagick?

How to crop 486 pixels from the bottom of each JPG image in the folder with ImageMagick?
The following command
magick -crop -0-486 *.jpg
says
magick.EXE: no images found for operation `-crop' at CLI arg 1 # error/operation.c/CLIOption/524
magick.EXE: no image to apply a property "%w" # warning/property.c/GetMagickPropertyLetter/2561.
magick.EXE: unknown image property "%w" # warning/property.c/InterpretImageProperties/3499.
magick.EXE: no image to apply a property "%h" # warning/property.c/GetMagickPropertyLetter/2449.
magick.EXE: unknown image property "%h" # warning/property.c/InterpretImageProperties/3499.
magick.EXE: no image to apply a property "%m" # warning/property.c/GetMagickPropertyLetter/2480.
magick.EXE: unknown image property "%m" # warning/property.c/InterpretImageProperties/3499.
Please, give specific example, internet in controversal (various names like mogrify, convert, various commands etc). Also don't point to ImageMagick "Talmud". Need just a simple example.
OS is Windows, Magick is installed with Chocolatey.
Please make a backup of your images before using the following commands.
The command for a single image is convert or if you have ImageMagick 7+, it is magick.
The command for multiple images is mogrify, or if you have ImageMagick 7+, it is magick mogrify.
The command you want is as follows and it will chop 486 pixels off the bottom of each image in the current directory:
magick mogrify -gravity south -chop x486 *.jpg
The main ImageMagick command command used to be called convert but there is a Microsoft tool with the same name that has caused confusion for years, so all the ImageMagick commands were prefixed with magick, followed by the old name. So,
animate ...
becomes:
magick animate ...
And
mogrify ...
becomes:
magick mogrify ...
In the case of convert, which is the most common usage, you can now use
magick convert ...
or simply
magick ...
where convert is implied.

Converting tiff to mode 1 bit images

Is it possible to convert a tiff image to mode 1-bit image using command line tools. I saw it can be done with gimp but I need to run a script so I prefer a solution using packages like imagemagick etc
If the image contents is already black and white, and you just need to convert, use:
convert input.tif -depth 1 output.tif
If you also require to threshold the image, use something like:
convert input.tif -separate -black-threshold 128 -depth 1 output.tif

Batch convert/resize different image formats to JPEG of certain resolution with ImageMagick

How do I use mogrify to batch-convert a ton of files (.TIF, .EPS, .JPG but most annoyingly of all-) .PSD files and only keep their first layer?
I found a solution on how to convert all images, but I kept getting filenames in my output-directory which were different from my originals (they had -0, -1, -2 etc. attached to the original filename).
I have 2 folders, ./original/ (which contains my PSDs, EPS, TIF, GIF, JPGs and other images) + the folder ./converted/ (which is the target folder for my conversion)
The command I use to convert my images are:
mogrify -verbose -path ./converted/ \
-alpha off -strip -mattecolor white -background white \
-resize 512x512 -format jpg -quality 75% -interlace Plane ./original/*.*
But no matter what I try, I keep getting this garbage which mangles my filenames. There is one mode however which just merges every interpretation of the files into one, but that's also quite useless as I am getting black backgrounds on my transparent files.
After 2 hours of searching I finally found the answer, and since I love stackoverflow, I'm sharing it here:
In order to select the best image of PSD's or first frame of .GIF's, all you need to add is an index of 0 (litterally, without the double quotes: "[0]") to the input file.
So the command to convert all images (you need to install ImageMagick with mogrify first), with a white background for anything transparant, within a bounding box of 512x512 pixels, outputted at jpg with 50% compression quality, the layers Disposed and the first index used, you need to execute the following command:
mogrify -verbose -path ./converted/ \
-alpha off -strip -mattecolor white -background white -layers Dispose \
-resize 512x512 -format jpg -quality 75% -interlace Plane ./original/*.*[0]
Hopefully this will serve as an easy to use template for anyone who needs to batch convert a lot of images.
I found several image converters but none were as easy to use as mogrify, and of course, it being a linux executable, it gives near infinite possibilities about what you can do with it... everything should be scriptable.

Resources