I have a ps image that I want to convert to a gif image with horizontal by vertical dimensions of 900 and 800 respectively. I have tried to use the command:
convert panel.gs -resize x800 y900 panel.gif
or also:
convert panel.gs -resize 900x800 panel.gif
Can you help me to tweak the convert commands so I can get the desired results?
.gs is not a valid suffix. Did you mean .ps?
Imagemagick will need ghostscript as a delegate. You did not say what was wrong nor what platform or what version of Imagemagick.
If the image does not have the same aspect ratio as the final dimensions you want, you will either 1) need to distort it to fit using !, 2) resize it and then extend to the size you want filling with background color, or 3) resize it with ^ and crop it to the size you want.
convert panel.ps -resize "900x800!" panel.gif
convert panel.ps -resize 900x800 -gravity center -background white -extent 900x800 panel.gif
convert panel.ps -resize "900x800^" -gravity center -extent 900x800 panel.gif
Well, firstly you haven't actually said what's wrong with the two commands that you have tried already.....
Your PostScript program probably does not contain an 'image' as such, PostScript is not a bitmap format its a programming language.
You can use Ghostscript to render the PostScript to an image, and then use ImageMagick to resize that image, possibly you can combine these two steps, or just perform a single conversion, it depends on what exactly you want to happen, which isn't clear.
If (for example) your PostScript program requests a media size of 9 inches by 8 then you can create a bitmap image by simply setting the resolution to 100 dpi using -r100.
If you want the image scaled differently in each direction, then you need to set a non-square resolution. For example if the PostScript program requests media of 9 inches by 4 then you need to set the resolution to 100x200 in order to get an image exactly 900 x 800 pixels. You would use -r100x200 for this.
The alternative, from a PostScript point of view, is to set the media size to a given
value in pixels (using -g900x800) and set -dDFIXEDMEDIA which prevents the PostScript program from changing it. You can then use -dFitPage which will have Ghostscript scale the content to fit the page. However it will scale the content equally in both directions, which may leave white space around the edge.
Now since Ghostscritp doesn't write GIF directly you'll need to load whatever bitmap format you select into IM in order to write it out as a GIF, so perhaps the simplest solution is just to use Ghostscript to render the PostScript to a defined resolution (eg 100 dpi) and then load that image into IM and rescale it there.
Since IM (and therefore convert) use Ghostscript to process PostScript programs, that's what's happening now so it isn't obvious to me what your problem is.
Related
I'm trying to batch convert PDF's to PNG's. Previously, this was always done manually through GIMP by importing a PDF, then converting it to PNG.
With the script that I wrote, this should all be done automatically. But for some reason, the image quality I get from using
convert \
-density 300 \
-adaptive-resize 2048 \
-define png:compression-level=9 \
"File1"
"File2"
Doesn't have the same "quality" compared to doing it via GIMP. See the image below for the difference in image quality.
In GIMP, I don't change much to the image. When I import the PDF, I change the resolution to 2048 pixels. When I convert and export it to PNG, I use all the default values GIMP offers, nothing fancy.
Changing the density to a higher or lower value doesn't do anything to the image. Also changing adaptive-resizing to normal resizing doesn't do much.
In the example image, both pictures are 2048 pixels wide. As you can see the lower image has a lot thicker/blurrier lines.
Example image comparison:
So, I have found a way around my problem.
Increasing the PPI kind of helped but still not as much as I would have liked it to.
Eventually I added this:
-channel A -fx "p*(p>0.2?22:0)"
Just some simple piece of code I found somewhere around here. It checks for the Alpha levels in the picture and if it's below a certain threshold it will just remove or "make the pixel" transparent. If it's over the threshold it will just boost the pixel to maximum visibility. Combined with the high PPI I dont get any "half pixels" anymore.
I'm really struggling trying to understand how to at least preserve if not reliably change density/resolution (not sure what to call it) when converting between file types with sampled images using ImageMagick's convert command line tool
I'm starting with a PDF file that has been scanned at either 200 or 300 dpi. I'd like to be able to convert it to TIFF and PNG for use with various OCR and QR code scanning utilities that only accept one of those formats.
The documentation at http://www.imagemagick.org/script/convert.php is "minimal" to say the least. Parameters that look like they could apply include the following:
Image Settings:
-density geometry horizontal and vertical density of the image
-sampling-factor geometry
horizontal and vertical sampling factor
Image Operators:
-resample geometry change the resolution of an image
-resize geometry resize the image
-sample geometry scale image with pixel sampling
-scale geometry scale the image
I've tried -density 200 and -density 200x200, both with no avail. Conversions to both tiff and png seem to take place at 72dpi independent of the source resolution.
I'd like to know what to use for this particular situation (i.e. preserving the resolution), but I'd also like to understand where I'm supposed to be able to get more documentation, including definition of terms, format of arguments, etc.
Recently, I read an interesting optimization technique to optimize transparent PNG images.
The idea was to split a transparent PNG image into 2 parts : PNG 8 bit with color information and PNG 24 with transparency, and to merge it on the client side. It will drastically reduce the size of the image. In the article example made with Photoshop, but I'm pretty sure, we could make it automatically with imagemagick.
So, the question is : how to do split a PNG image with imagemagick in such way ?
The article talks about "dirty transparency" which means the colour values of transparent pixels, although not visible, are retained in the image - they are just made invisible by the alpha layer.
These values, because they continue to contain the colour information, prevent the PNG optimiser from encoding them efficiently. You can achieve what the article suggests in this respect within ImageMagick by using:
convert image.png ... -alpha background result.png
That will make all transparent pixels have the same colour (your background colour) and then the PNG encoder will be be able to optimise them more readily since the values repeat over and over again.
See last part of this answer.
OK, so I have a folder of like 16 images all between the dimensions of 205x150 to 103x148. I want to size them down to the pixel height and width of 25px and stack them horizontally on a transparent background... is that possible?
I should probably be using ImageMagick for this...
You can do all that with ImageMagick.
You're question is not very specific, so here's a quick cheat sheet of command examples that may help you:
# resize image to width 25, keeping aspect ratio
convert -geometry 25x src/image1.png out/image1.png
# resize image to height 25, keeping aspect ratio
convert -geometry x25 src/image1.png out/image1.png
# concatenate images horizontally
convert +append src/image1.png src/image2.png out/image12horiz.png
# concatenate images vertically
convert -append src/image1.png src/image2.png out/image12vert.png
In addition, the montage command is probably perfect to create the final image you are looking for (on a transparent bg with some padding, etc), but I don't remember the syntax.
Another useful command is identify, to find the dimensions of images (along with other details).
After you install ImageMagick, you can see the list of commands in man ImageMagick, and get further details on each command in the man pages. There are an awful lot of functions, but it should not be too difficult to figure out the rest on Google. (I do that all the time.)
Just to add something to #janos answer.
I haven't used previous versions of ImageMagick but on version v6 or later according to the docs http://www.imagemagick.org/Usage/resize/#geometry
Geometry is a very special option. The operator behaves slightly differently in every IM command, and often in special and magical ways. The reasons for this is mostly due to legacy use and should be avoided if at all possible.
So other than the -geometry parameter you can still use -resize and omit the value you want in order to keep the aspect ratio. You can also use the -quality parameter to avoid image quality downgrade when resizing them. Value of quality is between 1 (lowest image quality and highest compression) and 100 (best quality but least effective compression). You can read more here: https://imagemagick.org/script/command-line-options.php#quality
For example:
# resize image to height 25, keeping aspect ratio with quality 90
convert -resize x25 original_image.jpeg -quality 90 resized_image.jpeg
I am trying to remove background color so as to improve the accuracy of OCR against images. A sample would look like below:
I'd keep all letters in the post-processed image while just removing the light purple color textured background. Is it possible to use some open source software such as Imagemagick to convert it to a binary image (black/white) to achieve this goal? What if the background has more than one color? Would the solution be the same?
Further, what if I also want to remove the purple letters (theater name) and the line so as to only keep the black color letters? Simple cropping might not work because the purple letters could appear at other places as well.
I am looking for a solution in programming, rather than via tools like Photoshop.
You can do this using GIMP (or any other image editing tool).
Open your image
Convert to grayscale
Duplicate the layer
Apply Gaussian blur using a large kernel (10x10) to the top layer
Calculate the image difference between the top and bottom layer
Threshold the image to yield a binary image
Blurred image:
Difference image:
Binary:
If you're doing it as a once-off, GIMP is probably good enough. If you expect to do this many times over, you could probably write an imagemagick script or code up your approach using something like Python and OpenCV.
Some problems with the above approach:
The purple text (CENTURY) gets lost because it isn't as contrasting as the other text. You could work your way around it by thresholding different parts of the image differently, or by using local histogram manipulation methods
The following shows a possible strategy for processing your image, and OCR it
The last step is doing an OCR. My OCR routine is VERY basic, so I'm sure you may get better results.
The code is Mathematica code.
Not bad at all!
In Imagemagick, you can use the -lat function to do that.
convert image.jpg -colorspace gray -negate -lat 50x50+5% -negate result.jpg
convert image.jpg -colorspace HSB -channel 2 -separate +channel \
-white-threshold 35% \
-negate -lat 50x50+5% -negate \
-morphology erode octagon:1 result2.jpg
You can apply blur to the image, so you get almost clear background. Then divide each color component of each pixel of original image by the corresponding component of pixel on the background. And you will get text on white background. Additional postprocessing can help further.
This method works in the case if text is darker then the background (in each color component). Otherwise you can invert colors and apply this method.
If your image is captured as RGB, just use the green image or quickly convert the bayer pattern which is probably #misha's convert to greyscale solutions probably do.
Hope this helps someone
Using one line code you can get is using OpenCV and python
#Load image as Grayscale
im = cv2.imread('....../Downloads/Gd3oN.jpg',0)
#Use Adaptivethreshold with Gaussian
th = cv2.adaptiveThreshold(im,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
Here's the result
Here's the link for Image Thresholding in OpenCV