Mogrify in ImageMagick removes file name prefix when processing files - svg

I'm using the following command to convert svg to png:
mogrify -path /myreal/path/to/pngdirectory -format png *.svg
The problem is that the file name 0102-filename.svg becomes filename.png.
But I need to preserve this numbered prefix — not sure why it's not being preserved by default.
Do I miss any configuration parameter in the command to make it work?

Related

How can I invert the colors of images in bulk by Imagemagick?

I would like to invert the colour of numerous figures in a fold in Linux, the code is as follows:
mogrify /home/yu/Desktop/1 -negate /home/yu/Desktop/2
The origin file is in path 1, and the target fold is path 2.
However, it's wrong. It points out that "mogrify: no decode delegate for this image format `' # error/constitute.c/ReadImage/562."
You would need to use the -path parameter for "/home/yu/Desktop/2`, and a file pattern -- not just the parent directory.
mogrify -path /home/yu/Desktop/2 -negate /home/yu/Desktop/1/*.jpg
Be sure to replace *.jpg with whatever file type you are expecting in the source directory.
Remember to define all CLI arguments before the filename pattern when using the mogrify utility.

Extract to same directory, media from different .docx files converted with Pandoc

Goal
I'm converting some .docx to .md with pandoc. These .docx have images that, after conversion, were being placed in a directory (markdown-repository/media/) and it's URL was being referenced in the resulting .md file.
So the goal is to have the resulting .md files with links pointing to the proper images stored in markdown-repository/media/. For this to happen, all images under markdown-repository/media/ need to have an unique name.
The problem
For each conversion, the images were being smashed by the last conversion, because pandocs doesn't track the image names, it creates image1.png, image2.png, image3.png, etc... for each converted file.
My suggestion
Create a folder to store media for each file, and this folder that contains the media, would have the name of the file converted.
Generate random, unique name, for the images.
Replace the links in the .md file with the generated images names.
Example:
fileA.docx
fileB.docx
Step 1
Convert the .docx to .md:
pandoc --extract-media=/result-media/output-media-for-fileA/ -f docx -t markdown fileA.docx -o fileA.md
pandoc --extract-media=/result-media/output-media-for-fileB/ -f docx -t markdown fileB.docx -o fileB.md
At this point we will have under /result-media/output-media-for-fileA/ 3 images
image1.png
image2.png
image3.png
and in the fileA.md these 3 links pointing to those images:
![](/result-media/output-media-for-fileA/image1.png)
![](/result-media/output-media-for-fileA/image2.png)
![](/result-media/output-media-for-fileA/image3.png)
Note: the same for fileB (i will not put here to be more simple, just replace fileA for fileB in the links)
Step 2
Then generate unique file names for the images under /result-media/output-media-for-fileA/ and /result-media/output-media-for-fileB/ and somehow save "the logs" so that we can then replace the image name with the new name inside the fileA.md and fileB.md
Note: This step is where i'm having most difficulty.
Step 3
Then i could just move all images with unique name, to my main folder markdown-repository/media/ .
Resources
This problem was already asked on Pandoc forum but it seems that Pandoc doesn't have any feature to handle this so i believe that with the help of linux shell scripting we can turn around.
https://pandoc.org/MANUAL.html
Get-ChildItem . -Filter *.docx |
Foreach-Object {
pandoc --from docx --to markdown --extract-media= --wrap=none $_ -o $_.Name.Replace('.docx', '.md')
This worked for me:
Get-ChildItem . -Recurse -Filter *.docx |
Foreach-Object { pandoc --from docx --to markdown --extract-media=$($_.DirectoryName) --wrap=none $_.FullName -o $_.FullName.Replace('.docx', '.md')}
The script converts all docx to md and keeps the folder structure. What was missing in the --extract-media command was the directory location to create the .\media directory. The $.DirectoryName needs to be expanded, $($.DirectoryName); otherwise, powershell will read it as text as opposed to a variable which will result in unexpected results.
In regards to the --wrap parameter, from pandoc:
--wrap=auto|none|preserve
Determine how text is wrapped in the output (the source code, not the rendered version). With auto (the default), pandoc will attempt to wrap lines to the column width specified by --columns (default 72). With none, pandoc will not wrap lines at all. With preserve, pandoc will attempt to preserve the wrapping from the source document (that is, where there are nonsemantic newlines in the source, there will be nonsemantic newlines in the output as well). Automatic wrapping does not currently work in HTML output. In ipynb output, this option affects wrapping of the contents of markdown cells.

Iterate through a folder and convert png to jpg

I have a large folder of images of different filetypes (jpg + png). I would like to iterate through the folder and convert any pngs to jpegs. So for example image323.png would be converted to image323.jpg.
I would also like them to be resized so that they are all the same size without losing their scale/ratio.
I know I need to loop through them and probably use a conditional (if file ends in png, change to jpg) and this will involve using PIl but not sure how to put it all together.
The easiest way would be with ImageMagick which you can install on macOS with homebrew:
brew install imagemagick
Make a backup copy first, then you just need to go into the directory with the images and run:
magick mogrify -format jpg *.png
If you want them resized so that they all end up 600 pixels wide:
magick mogrify -format jpg -resize 600x *.png
If you want them resized so that they all end up 300 pixels tall:
magick mogrify -format jpg -resize x300 *.png
If you want them resized so that they all end up no more than 600 pixels wide and no more than 300 pixels tall:
magick mogrify -format jpg -resize 600x300 *.png
If you want them resized so that they all end up EXACTLY 600 pixels wide and EXACTLY 300 pixels tall even if that means distorting the original aspect ratio:
magick mogrify -format jpg -resize 600x300\! *.png
Depending on the number of images you have, how big they are and how fast your CPU and disk subsystem are, you may get on better with GNU Parallel which you can install the same way:
brew install parallel
The command to convert all PNG files to JPEGs in parallel then becomes:
parallel --dry-run convert {} -resize 600x {.}.jpg ::: *png

imagemagick RAW file identify/convert - no such file or directory (tmp file)

I'm trying to use imagemagick-7 (CLI) on ubuntu to identify and convert RAW images to PNG format. I am using the dcraw binary as the delegate for identifying and converting raw images.
I updated the dng:decode delegate in delegates.xml as follows:
<delegate decode="dng:decode" command=""dcraw" "%i"" />
When I run this command: magick identify test.dng, I get the following error:
identify: unable to open image '/tmp/magick-24332a6nW8lcwejNJ.ppm': No such file or directory # error/blob.c/OpenBlob/3489.
The same error is given for magick convert. I noticed that imagemagick is generating a temporary intermediate file in my /tmp directory, which has a different name than the name it's expecting to find there. For example, it generates this file - magick-24332P6aVDePOFeCn.ppm - but is expecting the file it generated to have this name - magick-24332a6nW8lcwejNJ.ppm - which is why it's not finding it.
Note: I tested the same thing on OS X and it works perfectly fine (with the same delegates.xml configuration).
Is this a bug in imagemagick's implementation for unix systems or am I doing something wrong? Any thoughts would be greatly appreciated!
You were close the right command to use in delegate is
<delegate decode="dng:decode" command=""dcraw" -c "%i" > "%u.ppm"" />
Almost! You need to use the %o placeholder to tell the delegate manager were the output file will be written to. However the dcraw utility doesn't have in output destination options -- at least from what I can tell form the man-page. It does have a stdout option (-c), so you should be able to pipe/redirect the stream to an output location.
dcraw -c %i > %o
or in delegate XML.
<delegate decode="dng:decode" command=""dcraw" -c "%i" > "%o"" />
Update
Some tricks I've observed with custom delegations.
Use full path to binaries. Usually because I installed something outside of the systems PATH. Usually /usr/local/bin or /opt directories.
command="/usr/local/bin/dcraw ...
Use mv over pipes. If your not comfortable with debugging unix streams & such. Just do some basic copy/move command. We can rewrite the above command with something like...
dcraw %i; mv %i.ppm %o
Ignore escaping quotes until you know its working. Encapsulating arguments are important, and keep everything safe, but \"%i & "%i are hard to read.
<delegate decode="dng:decode" command="dcraw %i; mv %i.ppm %o" />
... verify things are called correctly ... then probably escape paths.
<delegate decode="dng:decode" command="dcraw "%i"; mv "%i".ppm "%o"" />
As pointed out previously, use identify -list Delegate to verify that the command is loaded correctly, and -verbose to verify that it was called correctly during runtime.
For anyone else who experiences this problem, my solution ended up being to switch from imagemagick to graphicsmagick, which is by default configured to use dcraw (of course, you need to have dcraw installed and on your PATH).
http://www.graphicsmagick.org/

An efficient way to detect corrupted png files?

I've written a program to process a bunch of png files that are generated by a seperate process. The capture mostly works, however there are times when the process dies and is restarting which leaves a corrupted image. I have no way to detect when the process dies or which file it dies one (there are ~3000 png files).
Is there a good way to check for a corrupted png file?
I know this is a question from 2010, but I think this is a better solution: pngcheck.
Since you're on a Linux system you probably already have Python installed.
An easy way would be to try loading and verifying the files with PIL (Python Imaging Library) (you'd need to install that first).
from PIL import Image
v_image = Image.open(file)
v_image.verify()
(taken verbatim from my own answer in this thread)
A different possible solution would be to slightly change how your processor processes the files: Have it always create a file named temp.png (for example), and then rename it to the "correct" name once it's done. That way, you know if there is a file named temp.png around, then the process got interrupted, whereas if there is no such file, then everything is good.
(A variant naming scheme would be to do what Firefox's downloader does -- append .partial to the real filename to get the temporary name.)
Kind of a hack, but works
If you are running on linux or something like you might have the "convert" command
$ convert --help
Version: ImageMagick 5.5.6 04/01/03 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 2003 ImageMagick Studio LLC
Usage: convert [options ...] file [ [options ...] file ...] [options ...] file
If you make an invalid png, and then try to convert, you'll get an error:
$ date> foo.png
$ convert foo.png foo.gif
convert: NotAPNGImageFile (foo.png).
Find all non-PNG files:
find . -type f -print0 | xargs -0 file --mime | grep -vF image/png
Find all corrupted PNG files:
find . -type f -print0 | xargs -0 -P0 sh -c 'magick identify +ping "$#" > /dev/null' sh
file command only checks magic number. Having the PNG magic number doesn't mean it is a well formed PNG file.
magick identify is a tool from ImageMagick. By default, it only checks headers of the file for better performance. Here we use +ping to disable the feature and make identify read the whole file.

Resources