Cropping two transparent png images using Imagick gives different results - linux

I am having different result when cropping two png files.
Imagick Version using convert -version:
Version: ImageMagick 6.6.5-10 2010-11-26 Q16
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
The two files using identify:
works.png PNG 1218x610 1218x610+0+0 8-bit DirectClass 755KB 0.000u 0:00.000
doesntwork.png PNG 70x70 70x70+0+0 8-bit DirectClass 1.64KB 0.000u 0:00.000
I am using this command:
convert <original>.png -crop 50x50+0+0 <target>.png
The problem is that works.png is 100% correct, while doesntwork.png is a black square.
Both original images seem to be the same - and do display correctly on windows 7.
The images:
works.png:
doesntwork.png:

Works for me using convert -versionImageMagick 6.4.0 04/17/08 Q16 (which is the "Current" version under cygwin), running via cygwin on Windows XP SP3.
It may be a bug in your particular version of ImageMagick. The shark works.png is a full color image with alpha (4 8-bit channels). The doesntwork.png is a 1-bit black and white image with 8 bits of alpha, and the shading is done by varying the alpha. That seems to me a rather rare format, so I can imagine a bug slipping into the conversion code.
You might try converting the image to true color first.

In the case of the two images posted, this seems like it might be a case of tools doing what they're told instead of what's intended.
Using -crop 50x50+0+0 on the shark image above gives a completely transparent 50 by 50 image because the 50 by 50 block of pixels in the upper left corner (+0+0) of the shark image is completely transparent. Using -crop 50x200+0+0 captures the tip of the shark's tail as well. -crop 50x50+950+250 gets the eye.
Does all this match your results?

Related

How to convert images from the jpg format to ppm(P3)

I have recently learned how to read ppm3(P3) images in C++. I just read RGB pixels written in a plain format. I want to convert some certain jpg pictures to ppm3 and then experiment with different things, like identifying numbers there, the circled answers in exam papers, etc.
I have tried this website: https://convertio.co/pdf-ppm/, but it transformed a photo in the P6 format. Could anyone help?
You can use ImageMagick in the Terminal/shell:
magick INPUT.JPG -compress none OUTPUT.PPM
If you omit -compress none you'll get binary (i.e. P6) PPM output.
If using old v6 ImageMagick, that becomes:
convert INPUT.JPG -compress none OUTPUT.PPM
All the options, switches, operators and settings for ImageMagick are documented here.
If you want to convert PPM to JPEG, or to PNG, you can just use:
magick INPUT.PPM OUTPUT.JPG
or
magick INPUT.PPM OUTPUT.PNG
You can also programmatically create a random PPM file like this:
#!/bin/bash
W=5; H=4
echo "P3\n${W} ${H}\n255" > image.ppm
for ((i=0;i<$((W*H*3));i++)) ; do
echo $((RANDOM%255))
done >> image.ppm
Then enlarge for easy viewing and make into a PNG:
magick image.ppm -scale 200x result.png
Or, the same thing again, nut maybe slightly more elegantly and without creating an intermediate file:
#!/bin/bash
W=5; H=4
{
printf "P3\n${W} ${H}\n255\n"
for ((i=0;i<$((W*H*3));i++)) ; do
echo $((RANDOM%255))
done
} | magick ppm:- -scale 200x result.png
If you prefer to use the lighter weight, but far less capable NetPBM tools, it would be:
jpegtopnm -plain INPUT.JPG > OUTPUT.PPM
For parsing pixels P6 is probably more useful for binary apps like C
P3 is about 4 times bigger than P6
both P3 and P6 are uncompressed format 1 entry = one pixel component
normally 3 components = 1 pixel (rgb) here is a white cow in a snowfield
each 255 on the Ascii =ÿ in the binary thus faster to count one byte text ÿ than 4 byte 255 the main advantage for P3 is when using an ascii editor to fettle the values as number key inputs (modify lower order numbers as 000 - 031 control code values becomes a problem in text editors)
SAFE binary bytes were used for Ansi Art as here this is a binary.ppm (just using safe non control codes.
The binary version (P6 uncompressed) is most easily generated For PDF users by xpdf or poppler pdftoppm (one single executable no real need for more than that).
http://www.xpdfreader.com/download.html
For Jpeg you can use jpegtopnm as described in other answer by Mark
https://sourceforge.net/projects/netpbm/files/
for docs and other info see https://netpbm.sourceforge.net/doc/
for binaries on windows the Cygwin / GNUwin32 ports may be useful but older 2005 https://gnuwin32.sourceforge.net/packages/netpbm.htm One exe & 4 dlls
for better description see https://en.wikipedia.org/wiki/Netpbm#File_formats
for windows related viewing see https://github.com/vbaderks/netpbm-wic-codec and also possibly conversion https://github.com/peirick/ZackViewer

Resizing does not respect resolution specification

I've got a 16x512 image comprised of 16x16 images. I want to seperate them, individually scale each one down to 16x8, then put them all back into their full 16x512. I've got a basic idea, but I'm having trouble executing it.
Using the commands from unix stackexchange, I split by file by using convert -crop 16x16 my_image.png crop-%d.png, which yields 32 images (512 / 16 == 32). My next step was where trouble has started. From askubuntu, I found the command mogrify -resize 16x8 crop-*.png, however this does not yield 16x8 images, but rather 8x8, which I do not want. Furthermore, this post on stackoverflow gives me the command for merging these images, which is convert crop-*.png -append my_image_cropped.png, however it does not yield a 16x512 like I want, but rather 8x256 (the 8 is due to the previous bug, but I still want a height of 512, not 256).
What do I need to accomplish my goals? The image in question can be found on imgur.
Edit: Here are some images which will describe the basic idea
The full image:
Both 16x16 and 16x8 side by side
The finalized image, basically the 16x8 will sit in the 16x16 area (right at the bottom part, that is essential), but won't fully fill it.
I am not sure I understand what you want to do. But if you resize 16x8, Imagemagick will keep aspect ratio. If you want to force it to be exactly 16x8 and can accept distortion, then use the ! flag. But you then say you want to put the 32 pieces back to form 16x512, but the resize will make it 16x256, since you have 32 image of height 8. So you have to resize again. Here is how to do that, if that is really what you want.
Create a gradient image for testing:
convert -size 16x512 gradient: grad.png
Do the processing:
convert grad.png -crop 16x16 -resize 16x8! -append -resize 16x512! newgrad.png
Note that proper Imagemagick syntax reads the input first.
ADDITION:
Given your new information in your comment, try this:
convert grad.png -crop 16x16 -resize 16x8 -gravity northwest -background none -extent 16x16 -append newgrad.png
Change the background color as desired and the gravity setting as desired for positioning.

Color reduction with ImageMagick

There's an image I convert to grey in a first step using this:
convert out.jpg -colorspace GRAY -normalize png:out.png
In another step I would like to reduce colors to 12:
convert out.png +dither -colors 12 -filter box -normalize png:out.png
This works perfectly with a very old version of GraphicsMagick which I have installed on one machine. On another machine is the latest version of ImageMagick. Here the resulting image just has 8 colors.
Is there a way I can force ImageMagick to make exactly 12 colors? Not more, not less?
Use the -posterize switch
convert colors.png -colorspace gray +dither -posterize 12 mono12.png
You can create our own 12 color colorable image and use -remap to recolor your image. See http://www.imagemagick.org/Usage/quantize/#remap and use -dither none

Extracting Text from a PDF file with embedded font

I have a PDF file containing some tabular data.
http://dl.dropbox.com/u/44235928/sample_rotate-0.pdf
I have to extract the tabular data from it. I have tried following with no success :
Select the text and paste it to notepad/excel-sheet. (I am getting junk characters)
Used save as text from Acrobat Reader. It is also giving junk characters and not the actual text.
Tried ApachePDFBox command line utility to extract text from PDF. It is also giving junk characters instead of real texts.
Finally I am trying a OCR solution. I am converting the pdf file into .tif images using ImageMagick and getting those images processed by tesseract OCR.
The OCR solution is not very accurate though( about 80% words matched ).
I tried changing density and geometry of the image created from PDF to get better results from tesseract OCR.
convert -rotate 90 -geometry 10000 -depth 8 -density 800 sample.pdf img_800_10000.tif;
tesseract img_800_10000.tif img_800_10000.tif nobatch letters;
I am not sure for what kind of image( density, geometry, monochromatic, sharpen boundary etc) would be best suited for the OCR.
Please suggest what could be the best possible parameters(density,geometry,depth etc) for generating images from a PDF file, so that the tesseract accuracy will increase.
I am open to other( non-ocr ) solutions as well.
In this case I recommend to NOT use ImageMagick for the PDF -> TIFF conversion. Instead, use Ghostscript. Two reasons:
Using Ghostscript directly will give you more control over individual parameters of the conversion.
ImageMagick cannot do that particular conversion itself -- it will call Ghostscript as its 'delegate' anyway, but will not allow you to give all the same fine-grained control that your own Ghostscript command will give you.
Most of the text in the table of your sample PDF is extremely small (I guess, only 4 or 5 pt high). This makes it rather difficult to run a successful OCR unless you increase the resolution considerably.
Ghostscript uses -r72 by default for image format output (such as TIFF). Tesseract works best with r=300 or r=400 -- but only for a font size from 10-12 pt or higher. Therefor, to compensate for the small text size you should make Ghostscript using a resolution of at least 1200 DPI when it renders the PDF to the image.
Also, you'll have to rotate the image so the text displays in the normal reading direction (not bottom -> top).
This is the command which I would try first:
gs \
-o sample.tif \
-sDEVICE=tiffg4 \
-r1200 \
-dAutoRotatePages=/PageByPage \
sample_rotate-0.pdf
You may need to play with variations of the -r1200 parameter (higher or lower) for best results.
Since a comment asked "How to define the geometry of an image when using Ghostscript as we do in convert?", here is an answer:
It does not make sense to define geometry (that is image dimensions) and resolution for a raster image created by Ghostscript at the same time.
Once you convert a vector based page of a given dimension (such as PDF) into a raster image (such as the TIFF G4 format) giving a desired resolution (as done in the other answer), you already indirectly and implicitly also did set the dimension:
The original PDF dimension of your sample file sample_rotate-0.pdf is 1008x612 points.
At a resolution of 72 DPI (the default Ghostscript uses if not given directly, or -r72 in the Ghostscript command if given directly) the image dimensions will be 1008x612 pixels.
At a resolution of 720 DPI (-r720 in the Ghostscript command) the image dimensions will be 10080x6120 pixels.
At a resolution of 1440 DPI (-r1440 in the Ghostscript command of my other answer) the image dimensions will be 20160x12240 pixels.
At a resolution of 1200 DPI (-r1200 in the Ghostscript command) the image dimensions will be 16800x10200 pixels.
At resolution of 1000 DPI (-r1000 in the Ghostscript command) the image dimensions will be 14000x8500 pixels.
At a resolution of 120 DPI (-r120 in the Ghostscript command) the image dimensions will be 1680x1020 pixels.
At resolution of 100 DPI (-r100 in the Ghostscript command) the image dimensions will be 1400x850 pixels.
If you absolutely insist to specify the dimension/geometry for the output image on the Ghostscript commandline (rather than the resolution), you can do so by adding -gNNNNxMMMM -dPDFFitPage to the commandline.
There you can find decoded content of your file: https://docs.google.com/open?id=0B1YEM-11PerqSHpnb1RQcnJ4cFk
A absolutely sure the OCR is the best way to read pdf file, but you can try REGEX-ing the native content. It going to be be the hard and long way.

Wrong colours when converting TIFF image to PNG in ImageMagick

I'm working on a PHP script that automatically converts TIFF images to PNG files.
For that purpose, I use ImageMagick:
$ convert a.tif a.png
It works to some degree, however, the colours are very acute and deviant from the way they are pictured on my PC. To illustrate the problem, please have a look at the enclosed files, the include:
The Windows Live Foto Gallery output (that's pretty much how I want it to be)
The ImageMagick output (the mess I end up with)
The original TIFF file
Has anyone an idea whether, and if so how, I can alter the ImageMagick colour interpretation?
Thanks a lot!
Alright,
thanks to ergosys, the problem was easily solved: I needed to apply ICC colour profiles.
The XMP declared ISO 12647-2:2004, which was to be found at http://eci.org.
$ convert -profile ISOcoated_v2_eci.icc -profile eciRGB_v2.icc a.tif c.png
When converting from a CMYK color space to an RGB color space, as you do when going from tiff to png, you have to convert the color spaces along with the image. Try:
convert -colorspace rgb a.tif a.png
I ran this locally and get a better result from this than from the command line in your question, but my color vision sucks, so I can't guarantee that this is what you were after. =] Hope it gets you on the right track, anyway.

Resources