I need to process some pdfs to transparent pngs the pdfs are text-only with colored backgrounds.
The pdfs have varied colored backgrounds, Some are lightgray, some are white etc. but they are consistent throughout the pdf.
Is there a way in imagemagick where I can identify the left topmost pixel color and pass the acquired color to the command -transparent "identifiedColor" while executing the imagemagick command?
Thanks !
Solution 1: matte floodfill
convert input.pdf -fill none -fuzz 10% -draw "matte 0,0 floodfill" output.png
Probably you'll have to adjust fuzz percentage
more details here: http://www.imagemagick.org/Usage/draw/#matte
Solution 2: color replace
I could not find a way to do it in a single command, but these two commands should work:
replace background color (taken from pixel[0,0]) with white:
convert input.pdf -fill white -draw "color 0,0 replace" temp.png
make white pixels transparent:
convert temp.png -transparent white output.png
more details here http://www.imagemagick.org/Usage/draw/#color
Tested with ImageMagick 6.6.0-1 on Windows
Related
I am trying to convert a LaTeX-generated PDF file to a PNG file with anti-aliasing and a transparent background (white text on a black background). After having read the answer to this post and one of the comments to the answer, I compared the convert function of ImageMagick against pdftoppm. So far, the highest quality anti-aliased images that I can generate are using pdftoppm (for a given DPI resolution). I use the following command:
pdftoppm -png -r 2000 text.pdf > text.png
The equivalent command (or so I think) using ImageMagick was:
convert +antialias -interpolate Nearest -density 2000 text.pdf -quality 90 -colorspace RGB text.png
However, I did not get as good-quality anti-aliasing using ImageMagick as I did with pdftoppm. In fact there hardly seems to be any anti-aliasing in the ImageMagick-generated image. See the close-ups below:
pdftoppm image:
ImageMagick image:
So where this leaves me is that I am satisfied with the anti-aliasing that pdftoppm provides. However, ImageMagick seems to have more functionality in now converting the anti-aliased image such that the black background is transparent. I have applied the approaches detailed in this post using ImageMagick, but they compromise the quality of the anti-aliasing that was previously satisfactory.
Can anyone advise me on how to solve the issue of obtaining a transparent background (which will always be black in color) while not affecting the anti-aliasing quality? Additionally, if the ImageMagick command that I used above was sub-optimal for generating a high-quality anti-aliased image, is there a way that I can achieve both anti-aliasing as well as background transparency by using ImageMagick alone? Any form of advice/tips would be much appreciated!
P.s. Since this question is partially LaTeX-related (I use LuaLaTeX to compile the PDF), I have posted a related question here regarding whether there is a much more straightforward way of directly generating the PDF file with a transparent background.
EDIT:
I've managed to fix the issue of transparency based on some comments on the question I posted on the TeX stack exchange. Now it's just about how I can improve the quality of anti-aliasing. Is there a way that I can achieve the same quality anti-aliasing that I get from pdftoppm?
The pdf file that I am converting can be found on this Dropbox link. Note that the font colour is white, and the background shows as white too (in my pdf viewer anyway), but is transparent. This is the converted PNG file.
You should use -density to increase the anti-aliasing of your PDF to PNG conversion. I note that your image is opaque white and the text is simply in the alpha channel.
convert -density 600 text.pdf -alpha extract x.png
If on Imagemagick 7, change convert to magick.
If you want to keep the transparency and keep your text white, then
convert -density 600 text.pdf y.png
The image is above, but will look completely white and blend with the white background. So you will need to download it.
If you want black text on transparency, then
convert -density 600 text.pdf -alpha extract -alpha copy -channel rgb -negate +channel z.png
I have no experience on any image processing/editing tool. And I am doing a project, which requires me to use different shapes. I could create different shapes using visio. But however not able to get rid of white background behind. I need only shape not squared white background.Tried online out of my ways but not successfull.
Any help will be greatly appreciated.
Thanks,
Ganesh
Absolutely any image file has to be contained within a rectangular frame, this includes png and SVG.
Some image file formats can have what are called alpha channel backgrounds this allows you to see through transparent areas.
What you want to do is remove the white background to expose the alpha channel background in Photoshop (or similar tool) which can then be saved out as transparent.
For example in Photoshop:
If you open this image directly and have no other layers, double click the layer that says background and OK the confirmation box. This turns your flat image into a layered image
Select the magic wand tool and ensure you have a high tolerance set (3)
with the wand selected click the white area to bring up a marquee around your selection (the white background) and hit delete to remove it.
Your image should now have a chequered background which is the transparency showing through.
If you now go to file > save as and select png, your image should now be saved out with an alpha background.
Please note: There are further optimisations to make if this is for web, including file formats and file size but that is beyond the scope of this question but I encourage you to read up on the Gif format and it's restrictions, the difference between 8bit and 24bit pngs and how to use SVG.
You can do it pretty simply at the command-line using ImageMagick which is free and installed on most Linux distros and is available for OSX and Windows.
Basically, you want to make your whites transparent, so you would do
convert shape.png -transparent white result.png
If your whites are a little bit off-white, you could allow for some variation with a little fuzz as follows:
convert shape.png -fuzz 10% -transparent white result.png
I added the checkerboard background just so you can see it on StackOverflow's white background - it is not really there.
By the way, you may like to trim to the smallest bounding rectangle while you are there:
convert shape.png -fuzz 10% -transparent white -trim result.png
By the way, you can also draw your shapes with ImageMagick:
convert -size 150x150 xc: -fill none -stroke "rgb(74,135,203)" -draw 'stroke-width 90 ellipse 0,0 80,80 30,80' arc.png
See Anthony Thyssen's excellent examples here.
I'm trying to do it using Ghostscript:
gs -sOutputFile=gray.pdf \
-sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray \
-dProcessColorModel=/DeviceGray \
-dNOPAUSE -dBATCH \
-dAutoRotatePages=/None \
color.pdf
But this doesn't result in using only black ink:
gs -q -o - -sDEVICE=inkcov gray.pdf
0.15365 0.15365 0.15365 0.09419 CMYK OK
I can make the conversion successfully using Adobe products but I'd like to be able to do this in a more automatable fashion.
This is (mostly) due to the way that inkcov works.
Your PDF file contains a transparency group with a DeviceRGB blending space. Note that this does not actually create any RGB output, it merely means that any objects which are blended must be done in RGB space. So anything not in DeviceRGB (eg all the objects in Gray) must first be converted to RGB, then blended, then converted to device space for rendering.
Because inkcov is a CMYK device, this means that the blended RGB objects must then be converted to CMYK. This results in less than perfect black. The PDF file does not in fact contain anything except gray colour specifications except for specifying the blending space of the transparency as RGB.
Looking at what Acrobat appears to do, it seems the simple solution is the one to go for, change the transparency blending space into DeviceGray as well. We know from previous experience that blending in different spaces does result in differences in rendering. On the other hand, so does changing the colour space of all the colour specifications......
Given time to do some more investigation I'll probably go down ths road in the next release.
I would like to know if there is a way to resize a picture while keeping it's pixelisation
convert ./TEST.png -resize 723x523 -compose Copy -gravity center -extent 723x523 -quality 92 ./TEST_big.png
Illustration from 20x20 to 100x100 here: image
`
Thanks
No one has answered this so I will put what I've done.
I've actually made a Python script who, for each pixel is making a square of n-pixels (10 for example). I'm making the output larger than the picture size I actually want(because I'm only doing squares) with GDAL and then resize it with imageMagick.
convert input.png -extent 100x100 -gravity center -background white output.png
If the input is 50x50 the surrounding background is white. Can I somehow set this to transparent without declaring any color within input as transparent?
Use this instead:
convert \
input.png \
-background none \
-gravity center \
-extent 100x100 \
output.png
Note well: The order of the parameters is significant! (To convince yourself, just put -background none at the end of the parameters instead of the start...)
Updated: Thanks to #jesmith who noticed that the commandline I originally provided does no longer work as intended. More recent versions of convert additionally require that the -gravity center is called before -extent 100x100. (This was one of the changes introduced to one ImageMagick's most recent versions [at the time of originally writing this answer]).
Kurt's note is ironically spot on, order matters greatly. Kurt's command results in gravity not being applied to the extent, so the transparent 'border' will all be to the bottom and/or right of the image.
Moving gravity before extent will correctly create equal transparent 'borders' on all applicable sides.
convert input.jpg -background none -gravity Center -extent 100x50
output.png