imagemagick resize - multiple files with wrong filenames after resizing .jpg images - resize

imagemagick resize - multiple files with wrong filenames after resizing .jpg images
Ubuntu 14.04 and ImageMagick 6.7.7-10
I need to resize ~900 jpg images (kept in several folders) to several dimensions, with the current aspect ratio. I wished to start with 300px wide versions.
I selected the first folder 1997 and I typed convert '*.jpg[300x]' *.jpg and the task was successful.
I have 98 jpg files resized to 300px wide (and no large files as they have been overwritten.)
I switched to the second folder 1998 and I typed in the same convert '*.jpg[300x]' *.jpg
( I also tried with convert *.jpg -resize 300 *.jpg, with the same result. )
Unexpected result:
I still have the (20) old jpg files, and 39 resized files (2 pieces of each, except the last one, which has a single resized version)
The filenames of the resized files start with the name of the last original file and end with a counter.
➜ 1998 ls
i--santa_ferenc-olaj-129_angyali_udvozlet.jpg
i--santa_ferenc-olaj-130_szentek_kozossege.jpg
i--santa_ferenc-olaj-130+_szentek_kozossege.jpg
i--santa_ferenc-olaj-131_te_vagy_a_kiraly_jezus.jpg
i--santa_ferenc-olaj-132_orom.jpg
i--santa_ferenc-olaj-133_cim_nelkul.jpg
i--santa_ferenc-olaj-134_atfestve_a_sziv_megterese.jpg
i--santa_ferenc-olaj-135_ave.jpg
i--santa_ferenc-olaj-136_isten_kezeben.jpg
i--santa_ferenc-olaj-137_peter_emlekere.jpg
i--santa_ferenc-olaj-138a_aldozat.jpg
i--santa_ferenc-olaj-138b_aldozat.jpg
i--santa_ferenc-olaj-139_jelenesek.jpg
i--santa_ferenc-olaj-140_atfestve_latomas.jpg
i--santa_ferenc-olaj-141_a_zaszlos.jpg
i--santa_ferenc-olaj-142_gondviseles.jpg
i--santa_ferenc-olaj-143_az_ido_tukreben.jpg
i--santa_ferenc-olaj-144_jelenesek_ii.jpg
i--santa_ferenc-olaj-145_jelenesek_iii.jpg
The last original file's name i--santa_ferenc-olaj-147_lanc.jpg is missing from here, eventhough I can see the image in the folder view. Then the resized images come:
i--santa_ferenc-olaj-147_lanc-0.jpg
i--santa_ferenc-olaj-147_lanc-10.jpg
i--santa_ferenc-olaj-147_lanc-11.jpg
i--santa_ferenc-olaj-147_lanc-12.jpg
i--santa_ferenc-olaj-147_lanc-13.jpg
i--santa_ferenc-olaj-147_lanc-14.jpg
i--santa_ferenc-olaj-147_lanc-15.jpg
i--santa_ferenc-olaj-147_lanc-16.jpg
i--santa_ferenc-olaj-147_lanc-17.jpg
i--santa_ferenc-olaj-147_lanc-18.jpg
i--santa_ferenc-olaj-147_lanc-19.jpg
i--santa_ferenc-olaj-147_lanc-1.jpg
i--santa_ferenc-olaj-147_lanc-20.jpg
i--santa_ferenc-olaj-147_lanc-21.jpg
i--santa_ferenc-olaj-147_lanc-22.jpg
i--santa_ferenc-olaj-147_lanc-23.jpg
i--santa_ferenc-olaj-147_lanc-24.jpg
i--santa_ferenc-olaj-147_lanc-25.jpg
i--santa_ferenc-olaj-147_lanc-26.jpg
i--santa_ferenc-olaj-147_lanc-27.jpg
i--santa_ferenc-olaj-147_lanc-28.jpg
i--santa_ferenc-olaj-147_lanc-29.jpg
i--santa_ferenc-olaj-147_lanc-2.jpg
i--santa_ferenc-olaj-147_lanc-30.jpg
i--santa_ferenc-olaj-147_lanc-31.jpg
i--santa_ferenc-olaj-147_lanc-32.jpg
i--santa_ferenc-olaj-147_lanc-33.jpg
i--santa_ferenc-olaj-147_lanc-34.jpg
i--santa_ferenc-olaj-147_lanc-35.jpg
i--santa_ferenc-olaj-147_lanc-36.jpg
i--santa_ferenc-olaj-147_lanc-37.jpg
i--santa_ferenc-olaj-147_lanc-38.jpg
i--santa_ferenc-olaj-147_lanc-3.jpg
i--santa_ferenc-olaj-147_lanc-4.jpg
i--santa_ferenc-olaj-147_lanc-5.jpg
i--santa_ferenc-olaj-147_lanc-6.jpg
i--santa_ferenc-olaj-147_lanc-7.jpg
i--santa_ferenc-olaj-147_lanc-8.jpg
i--santa_ferenc-olaj-147_lanc-9.jpg
i--santa_ferenc-olaj-147_lanc.jpg
What is the problem? :'(

Try this:
mogrify -resize 300 *.jpg

Related

Why can't I convert certain TIF files that I received in a split archive?

I received a large number of document files, where each document has its own split archive for each page (i.e. file1.001,file1.002,file2.001,file3.001). These are meant to be TIF files that can easily be combined and converted into PDF documents.
However, some of these files will not convert through imagemagick. Some can simply be converted using a different program, which works fine. There are some files where this doesn't work. I tried converting them to .jpg, then to tif, but they won't convert to .jpg. Things got weird when I converted them to .png, as some of these files would have multiple output files associated with them.
This is hard to explain, but I'll try and give an example; file1.001 and file1.002 both have the same image present on them when converted to tif and opened. However, when either of the tif documents is converted to a .png, two .png files are created. One has the original page, but the other one has a second page of the document that I could not view previously.
What could be causing this weird behavior, and how can I convert these to pdf more reliably?
I also used BlueBeam Staple to convert the files, if that helps at all.
Edit:
I've verified I'm on the latest imagemagick release, and I've been using it through PHP to process files. I'm running Windows 10.
Also, here's some example files to play around with. The first TIF actually shows the second page, instead of the page I normally see when I open the file.
Edit 2: Sorry, I thought uploading the image would preserve the file type. Here's a link to some test samples
When I convert your tiff to png, I get two files using IM 7.1.0-10 Q16-HDRI or IM 6.9.12-25 Q16 both on Mac OSX Sierra.
magick -quiet 294944.tif x.png
Produces:
and
Is this not what you get or expect?
P.S.
What are the other two files: 327924.001 327924.002
If those are some kind of split tiff, then it does not look like libtiff, which Imagemagick uses to read TIFFs can handle them. I get errors when attempting to use identify on them.
You definitely have some issue with whatever attempted to write those tiffs.
instrument 294944 page 1 of 2 = G4 199 dpi sheet 2 of 2 294944.tif (25.17 x 17.53 inches)
instrument 294944 page 2 of 2 = G4 199 dpi sheet 1 of 2 294944.tif (24.12 x 17.63 inches)
instrument 327501 page 1 of 1 = UN 72 dpi sheet 1 of 1 327924.001 (124.78 x 93.86 inches)
instrument 327924 page 1 of 2 = G4 400 dpi sheet 1 of 2 327924.002 (23.80 x 17.53 inches)
instrument 327924 page 2 of 2 = G4 400 dpi sheet 2 of 2 327924.002 (23.84 x 17.41 inches)
Two are identified as CCITT Group 4 Fax Encoding which is common for TIFFs of this type.
Tiff is a multi image format so a multipage FAX can be viewed as one file or 4 different printing CMYK colour plates could be sent as one image file for either overlay as one check print or printed one at a time for quality inking.
The file name Tif (or tiff) is usually applied to files with one or more pages (even 400+ for a long novel)
The extension part001.tif part002.tif is usually applied to groups of multiple pages OR for single sequential pages part1.001.tif part1.002.tif
Unfortunately for you you have a mix following a convention that seems to indicate number of pages 002 = 2 pages, but in inconsistent order, so need to check which were used for each file, as there is uncertainty.
Also the internal number does NOT always reflect the filename? perhaps transfer of interest ?
IN ADDITION you have a mix of compression methods and resolution thus cannot be sure of correct scale to be applied.
The best way to resolve this issue is decide how you wish them to be regrouped/sequenced and use the correct scale for each page or group of pages then recombine as desired into PDF.
It would help for a large number to tabulate the pages by number scale size compression etc and then process in identical groups before reorder and merge.

Batch extract Hex colour from images to file

I have around 10k images that I need to get the Hex colour from for each one. I can obviously do this manually with PS or other tools but I'm looking for a solution that would ideally:
Run against a folder full of JPG images.
Extract the Hex from dead center of the image.
Output the result to a text file, ideally a CSV, containing the file name and the resulting Hex code on each row.
Can anyone suggest something that will save my sanity please? Cheers!
I would suggest ImageMagick which is installed on most Linux distros and is available for OSX (via homebrew) and Windows.
So, just at the command-line, in a directory full of JPG images, you could run this:
convert *.jpg -gravity center -crop 1x1+0+0 -format "%f,%[fx:int(mean.r*255)],%[fx:int(mean.g*255)],%[fx:int(mean.b*255)]\n" info:
Sample Output
a.png,127,0,128
b.jpg,127,0,129
b.png,255,0,0
Notes:
If you have more files in a directory than your shell can glob, you may be better of letting ImageMagick do the globbing internally, rather than using the shell, with:
convert '*.jpg' ...
If your files are large, you may better off doing them one at a time in a loop rather than loading them all into memory:
for f in *.jpg; do convert "$f" ....... ; done

How to convert ps to same-looking pdf file?

I have a ps file consisting of several pages, with the following entry for the bounding box:
%%PageBoundingBox: 36 36 7164 2124
When viewing this file with okular it looks ok. After the conversion to a pdf as follows
ps2pdf graph.ps
a pdf is created, but each page shows only a magnification for each of the ps file. For the conversion I also tried options like
-sPAPERSIZE=letter
-dEPSCrop
with no visible effect. My question: How can I convert my ps file into a pdf file in such a way, that when viewed with okular the result looks identical?
It seems the desired width and hight must be specified for the conversion, in this case the command would be
ps2pdf -dDEVICEWIDTHPOINTS=7128 -dDEVICEHEIGHTPOINTS=2088 isearch.ps
as 7128 is the width and 2088 is the height of the bounding box for the given example.
If you are using Windows (thus gswin32c.exe), you can use the following in the cmd prompt:
c:\Program Files (x86)\gs\gs9.18\bin\gswin32c.exe" -sDEVICE=pdfwrite -o TheFinalOutput.pdf YourFileToConvert.ps

JPG to PDF Conversion, How to Fit Full Page

I have page scans of various sizes in JPG format which I convert to a single PDF using ImageMagick. However I noticed every PDF page for each type of scan produces a different size PDF page, even if I use -page A4 option on ImageMagick. I would every JPG, in whatever size to "fill" each PDF page, and every PDF page to be the same. I also have access to tools like pdftk, pdfjam.
Any ideas?
As a hack you can use pdflatex and the wallpaper package. It does the trick and has the advantage over most other methods of not altering the image content (resolution, compression, pixel content) and adds only about 1.2kB of overhead.
To keep the aspect ratio, use:
filename=test.jpg;
echo "\documentclass[a4paper]{article}\
\usepackage{wallpaper}\usepackage{grffile}\
\begin{document}\
\thispagestyle{empty}\
\ThisCenterWallPaper{1}{$filename}~\
\end{document}"\
| pdflatex --jobname "$filename";
rm "$filename".aux "$filename".log
To fill the page completely, use:
filename=test.jpg;
echo "\documentclass[a4paper]{article}\
\usepackage{wallpaper}\usepackage{grffile}\
\begin{document}\
\thispagestyle{empty}\
\ThisTileWallPaper{\paperwidth}{\paperheight}{$filename}~\
\end{document}"\
| pdflatex --jobname "$filename";
rm "$filename".aux "$filename".log
Finally you can concatenate your pages using pdftk
pdftk page1.pdf ... page2.pdf cat out final_document.pdf
When I used -density 50% on ImageMagick convert it managed to zoom lower res images to bigger PDF pages.
This should do the trick, assuming your PDF output should have A4 sized pages (portrait):
convert -scale 595x842\! *.jpg output.pdf

Rendering the whole media box of a pdf page into a png file using ghostscript

I'm trying to render Pdfs pages into png files using Ghostscript v9.02. For that purpose I'm using the following command line:
gswin32c.exe -sDEVICE=png16m -o outputFile%d.png mypdf.pdf
This is working fine when the pdf crop box is the same as the media box, but if the crop box is smaller than the media box, only the media box is displayed and the border of the pdf page is lost.
I know usually pdf viewers only display the crop box but I need to be able to see the whole media page in my png file.
Ghostscript documentation says that per default the media box of a document is rendered, but this does not work in my case.
As anyone an idea how I could achieve rendering the whole media box using ghostscript?Could it be that for png file device, only the crop box is rendered? Am I maybe forgetting a specific command?
For example, this pdf contains some registration marks outside of the crop box, which are not present in the output png file. Some more information about this pdf:
media box:
width: 667
height: 908 pts
crop box:
width: 640
height: 851
OK, now that revers has re-stated his problem into that he is looking for "generic code", let me try again.
The problem with a "generic code" is that there are many "legal" formal representations of "CropBox" statements which could appear in a PDF. All of the following are possible and correct and set the same values for the page's CropBox:
/CropBox[10 20 500 700]
/CropBox[ 10 20 500 700 ]
/CropBox[10 20 500 700 ]
/CropBox [10 20 500 700]
/CropBox [ 10 20 500 700 ]
/CropBox [ 10.00 20.0000 500.0 700 ]
/CropBox [
10
20
500
700
]
The same is true for ArtBox, TrimBox, BleedBox, CropBox and MediaBox. Therefor you need to "normalize" the *Box representation inside the PDF source code if you want to edit it.
First Step: "Normalize" the PDF source code
Here is how you do that:
Download qpdf for your OS platform.
Run this command on your input PDF:
qpdf --qdf input.pdf output.pdf
The output.pdf now will have a kind of normalized structure (similar to the last example given above), and it will be easier to edit, even with a stream editor like sed.
Second Step: Remove all superfluous *Box statements
Next, you need to know that the only essential *Box is MediaBox. This one MUST be present, the others are optional (in a certain prioritized way). If the others are missing, they default to the same values as MediaBox. Therefor, in order to achieve your goal, we can simply delete all code that is related to them. We'll do it with the help of sed.
That tool is normally installed on all Linux systems -- on Windows download and install it from gnuwin32.sf.net. (Don't forget to install the named "dependencies" should you decide to use the .zip file instead of the Setup .exe).
Now run this command:
sed.exe -i.bak -e "/CropBox/,/]/s#.# #g" output.pdf
Here is what this command is supposed to do:
-i.bak tells sed to edit the original file inline, but to also create a backup file with a.bak suffix (in case something goes wrong).
/CropBox/ states the first address line to be processed by sed.
/]/ states the last address line to be processed by sed.
s tells sed to do substitutions for all lines from first to last addressed line.
#.# #g tells sed which kind of substitution to do: replace each arbitrary character ('.') in the address space by blanks (''), globally ('g').
We substitute all characters by blanks (instead of by 'nothing', i.e. deleting them) because otherwise we'd get complaints about "PDF file corruption", since the object reference counting and the stream lengths would have changed.
Third step: run your Ghostscript command
You know that already well enough:
gswin32c.exe -sDEVICE=png16m -o outputImage_%03d.png output.pdf
All the three steps from above can easily be scripted, which I'll leave to you for your own pleasure.
First, let's get rid of a misunderstanding. You wrote:
"This is working fine when the pdf crop box is the same as the media box, but if the crop box is smaller than the media box, only the media box is displayed and the border of the pdf page is lost."
That's not correct. If the CropBox is smaller than the MediaBox, then only the CropBox should be displayed (not the MediaBox). And that is exactly how it was designed to work. This is the whole idea behind the CropBox concept...
At the moment I cannot think of a solution that works automatically for each PDF and all possibly values that can be there (unless you want to use payware).
To manually process the PDF you linked to:
Open the PDF in a good text editor (one that doesn't mess with existing EOL conventions, and doesn't complain about binary parts in the file).
Search for all spots in the file that contain the /CropBox keyword.
Since you have only one page in the PDF, it should find only one spot.
This could read like /CropBox [12.3456 78.9012 345.67 890.123456].
Now edit this part, carefully avoiding to add to (or lose from) the number of already existing characters:
Set the value to your wanted one: /CropBox [0.00000 0.00000 667.00 908.000000]. (You can use spaces instead of my .0000.. parts, but if I do, the SO editor will eat them and you'll not see what I originally typed...)
Save the file under a new name.
A PDF viewer should now show the full MediaBox (as of your specification).
When you convert the new file with Ghostscript to PNG, the bigger page will be visible.