ImageMagick command line character limit - command-line

I'm using command line definitions for ImageMagick on Windows to add several hundred coloured rectangles to a blank image (histogram). The length of the commands exceeds the 8192 character limit. I have hundreds of these commands:
-fill rgb(188,161,161) -draw "rectangle 1,106 4,19"
The ImageMagick documentation says that several commands can be placed in a file, which is then read. When testing, these have to be the same command type, i.e. I'd ahve to create a file for the draw commands and one for the fill commands. However, the colouring won't work then, as the correct fill command isn't linked to the relevant draw command.
magick -size 1920x108 xc:white -fill #fill.txt -draw #draw.txt hist.png
Is anybody aware of a workaround or any other options? I thought having the commands in a windows batch file might work, but this also seems to be limited by the 8192 characters.

You might find that Magick Vector Graphics (MVG) better suits your needs:
convert -size 1920x1080 xc:gray90 -draw #draw.txt result.png
where the file draw.txt contains:
fill cyan rectangle 10,10 1800,400
fill magenta rectangle 10,450 1750,800
fill yellow rectangle 10,850 1850,1000
There are some excellent examples by Anthony Thyssen here and a specification of the available drawing primitives here.

Here is an excerpt from MS link:
On computers running Microsoft Windows XP or later, the maximum length
of the string that you can use at the command prompt is 8191
characters. On computers running Microsoft Windows 2000 or Windows NT
4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.
So basically, it is a limitation of command line, not imagemagick. Is it possible that you split your task into two and then combine two images that was produced by these two steps onto one resulting image ?

Related

Resize image and create 3x3 tiles with ImageMagick

I managed to make tiles with ImageMagick like this:
-size 900x900 tile:D:\tile.jpg D:\tiles.jpg
But i want to resize the image before the tiling (300*300px) - and then maybe afterwards apply a sharpen, but the most important is the resizing.
How do i do that in one command line?
Work from a simple command like this...
convert D:\tile.jpg -resize 300x300 -write mpr:tiler +delete ^
-size 900x900 tile:mpr:tiler -sharpen 0x2 D:\tiles.jpg
That starts by reading in your "tile.jpg" image and resizes it to 300x300. Then it writes that resized image to an ImageMagick built-in memory register named "mpr:tiler" and deletes it from the current list. (You can name it "mpr:almost_anything".)
Next the command sets a canvas size of 900x900 and creates a tiled canvas filled with that "mpr:tiler" image.
You can add sharpening and other operations after reading in the input image and before writing it to the memory register, or after creating the tiled image and before writing it to an output file.
The command above is in Windows syntax. To run it in *nix change that continued-line caret "^" to a backslash "\". If you're using ImageMagick v7 replace "convert" with "magick".

Alter a person's race from the command line.

A disability project I'm building (here for your interest). Uses a set of copyright-free icons. Here are some examples of them in use.
It's bothering us that all of the icons are of white people. We'd like to make them more diverse and representative.
The icons are pngs. Here's an example. I feel like there must be a command line way of replacing one colour with another. If I had that, then I could do a lot of things semi-automatically, but I have NO idea if such a thing is possible.
Is it?
You can replace a specific color with another using ImageMagick or GraphicsMaqick. In your screenshot, the "flesh" is color #FFEFC6. Use -fill newcolor -opaque oldcolor to replace this color with another (and use "-fuzz 10%" to replace all pixels whose color is "close" to oldcolor):
magick pdzSe.png -fill black -fuzz 10% -opaque "#FFEFC6" black.png
magick pdzSe.png -fill orange -fuzz 10% -opaque "#FFEFC6" orange.png
magick pdzSe.png -fill brown -fuzz 10% -opaque "#FFEFC6" brown.png
If you want to use GraphicsMagick, replace magick with gm convert, and if you want to use ImageMagick version 6, replace magick with convert. If you're running on Windows, you'll need to replace the % with %%.
This should be enough to get you started, but it's a crude solution that doesn't account for blended pixels such as those appearing in your sample "thumbs-up" icon. Below are the results of running the same commands against "thumbs-up", enlarged 4x so you can see the "halo" effect around the edges of the "flesh". Getting rid of the "halos" will require more work, specific to your icon set. Ideally you'd have the original vector art-work to update, but I suppose that's not the case.
In fact, the original artwork is available at straight-street.com (search for "good") and working with the original SVG files is much simpler; there is no "halo" issue. Here I just used a text editor to change the color FFEFC6 to FF8888:

JPEG to PNG conversion with 300 DPI

Unable to convert a JPEG image into a 300 DPI PNG image using ImageMagick.
After conversion the PNG image is 72 DPI only. I'm using ImageMagick 6.9.0-0 Q16 x86 and Ghostscript v9.15.
Below is the line I use in my Perl script:
system("\"$imagemagick\" -set units PixelsPerInch -density 300 \"$jpg\" \"$png\"");
Adjusting the units & density will not alter the underlining image data, but updates meta info for rendering libraries. Important for vector to raster, but not very useful for raster to raster. To adjust the DPI of an image, use the -resample operation.
convert source.jpg -resample 300 out.png
You verify the DPI resolution with the following...
identify -format "%[resolution.x] %[resolution.y]\n" out.png
I'm wondering where the 72dpi is coming from. Assuming you are using X and some kind of Unix, ImageMagick defaults to using the screen resolution (72 dpi). I'm not sure what it does under OSX/XQuartz but it's likely similar. Is your screen resolution set to 72dpi (!?).
I'm with #emcconville #ikegami - just do this straight from ImageMagick on the commandline - passing the right options to be sure.
There are image manipulation modules that you can use from perl without having to resort to system commands as well such as Imager::Transformations, Image::Magick, and GD. Here's how to convert with GD.
perl -MGD -E 'my $imgjpg = GD::Image->newFromJpeg("img.jpg");
open my $imgpng, ">", "img.png" or die; print $imgpng $imgjpg->png();'
With most image manipulation packages the original resolution show be maintained during conversion - though some (including GD) will default to lower color depths (8 bit) unless passed a Truecolor flag.
e.g. GD::Image->newFromJpeg("img.jpg", 1);

Libreoffice Draw Export Resolution makes no sense

I am attempting to make a very simple label using Libreoffice Draw v 4.0.2.2. The label has not much more to it than regularly spaced lines of centered text
This image will be printed, and I have a fixed size/ppi requirement to ensure appropriate print quality.
I set the page size to my specs, and layout the text as I desire. The print shop takes several image formats including .tiff and .png. When I export the image, a dialog pops up that asks for the image size/ resolution. The given ppi is very low (~40) and I require a minimum of 180ppi. When I enter this, the image size adjusts itself and results in an image that is far too small.
The only solution that appears to be viable is to explode the page size and the drawing text size so it gets shrunk upon export. This is a very imprecise and illogical feature (bug?) of the program that I really wish is a result of my ignorance.
I found a thread in the mailing list which describes this issue exactly. The only answer that is given is essentially "yes, this is ridiculous and doesn't help anybody".
Can anyone give some advice to this? Or at least shed some light on who might need this "feature"?
There is something off about the Export tool of LibreOffice in general. It has been years since it is broken. Taking a screenshot is an alternative, but obviously you cannot control the resolution.
So, a better work around is exporting to SVG, and then convert the SVG to PNG with Inkscape. Once downloaded, convert the file with the following command:
inkscape -z -e out.png -w 1024 in.svg
If you are in Windows (x64), you will need to indicate the full path:
"C:/Program Files/Inkscape/inkscape.exe" -z -e out.png -w 1024 in.svg
If you install the 32 bit version, this should work:
"C:\Program Files (x86)/Inkscape/inkscape.exe" -z -e out.png -w 1024 in.svg
This can be done from inside Libre Office, there is no need to use any external tool. The Export dialog is very confusing, yes; you have to realize that both size and resolution can be set independently.
Select File -> Export -> choose the desired format. The export dialog should appear.
TAKE NOTE of Width and Height. Set the desired resolution; notice how Width and Height change (?). Don't worry, restore Width and Height to your saved values. And that's it. You get a high resolution image with the desired size and DPI.
Libre Draw (the one I'm using anyway) is a vector drawing app - have you asked the print shop if they can use vector formats like eps, pdf? Most should be able to in my experience. Then resolution becomes irrelevant.
-Terry

Tesseract and tiff format - spp not in set {1,3}

While trying to run this command:
tesseract bond111.tif bond111 batch.nochop makebox
I get the next error
Error in pixReadFromTiffStream: spp not in set {1,3}
Error in pixReadStreamTiff: pix not read
Error in pixReadTiff: pix not read
Assuming that spp not in set is the main error here, what does it mean?
At first it had trouble because the bpp was higher than 24 so I reduced it using Gimp but that did not resolve the issue.
It probably means your TIFF image has an alpha channel and therefore the underlying Leptonica library used by Tesseract doesn't support it. If you're using Imagemagick then be aware that operations such as -draw can cause alpha channels to be added. If you're using convert in your workflow and want to remove the channel again immediately, flatten the image before writing by adding -background white -flatten +matte before the output filename, e.g.:
convert input.tiff -fill white -draw 'rectangle 10,10 20,20' -background white -flatten +matte output.tiff
Tesseract (well, Leptonica) accepts PNGs these days and is less picky about them, so it might be easier to migrate your workflow to PNG anyway.
Sources: magick-users mailing list posting; tesseract-ocr mailing list posting
Thanks for your post ZakW, you pointed me to the right direction.
Anyhow i also needed to set '-depth 8'. Quality was not good enough for OCR, whatever I tried.
What worked for me is this solution:
ghostscript -o document.tiff -sDEVICE=tiffgray -r720x720 -g6120x7920 -sCompression=lzw document.pdf
tesseract document.tiff document -l deu
vim document.txt
This way I got perfect text with Umlauts in german.
Adjusting the conversion to the following line did help me.
convert -density 300 input.pdf -depth 8 -background white -alpha Off output.tiff
Note that the other answers did not work for me since they use the deprecated +matte flag instead of -alpha Off.
You can try using the command 'tiffinfo' provided by libtiff_tools to verify the TIFF format of your src image. A number of TIFF formats exist, with different values for Bits-per-pixel (bpp) and Samples-per-pixel (spp).
Error in pixReadFromTiffStream: spp not in set {1,3,4}
An 'spp' value of 2 is invalid for TIFF.
I solved the problem by saving directly to TIFF format from Gimp, instead of converting from .png to .tif using ImageMagick's 'convert'.
See also: TIFF format