What is the smartest way to find out if a PNG has transparency or not in Ruby? Is it OK if I just check if there's an alpha-channel? If yes: How do I check for an alpha-channel?
I'm writing a script that will convert all PNGs without transparency into smaller JPGs.
Thanks a lot for your help!
UPDATE: In the meantime I've written that script and you can find it at Github.
Checking the alpha channel seems the most sensible thing to do. Is it safe to assume you are using RMagick? did you read the documentation?
>> require 'RMagick'
>> image = Magick::Image.read("a.png").first
>> image.alpha?
=> true
http://www.imagemagick.org/RMagick/doc/image1.html#alpha_q
Related
I am currently in the process of building a Brachiograph plotter. I am 75 years old and have a minor disability with my hands. I would like to find out if anybody can tell me how I can output Turtle Graphics to a file that can be read by the Brachiograph plotter. I believe that the linedraw.py converts a .svg to a .json file that is read by the Brachiograph. I would like to create some fractal image files and print them with the Brachiograph.
Thank you for any help that you can offer with this project.
Dick Burkhartzmeyer
Welcome to Stackoverflow. In your OP you state "I believe that the linedraw.py converts a .svg to a .json file[...]". Looking at the documentation it looks like linedraw.py will convert a bitmap to an svg file and a json file.
From the documentation:
The main use you will have for linedraw is to take a bitmap image file
as input, and save the vectorised version as:
an SVG file, to check it
a JSON file, to draw with the BrachioGraph.
The way I would approach this is to create an svg with Turtle and then in your Python script convert that to a png. Then linedraw.py can be used to convert that to your JSON BrachioGraph file. I found a solution to do that in another SO thread. The answer in that thread is using Cairosvg to convert the SVG to PNG format.
I'm looking for a way to re-use my prawn code in other views (HAML). It can also be an image, but I don't need the whole PDF (with the layout and other pages), just a small graphic (chart) that I render in Prawn using a method.
EDIT:
because I'm using Heroku and I'm not saving the file (it's a response to a web request), I'm don't really want to open a pdf file with ImageMagick for example and process it into an image, unless it is my last option (besides coding the graphic again in HTML+CSS).
I don't know about a gem that might do the trick but you can always use RMagick or MiniMagick to covert the generated pdf to an image and scale it down:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
image = pdf.scale(300, 300)
image.write "doc.png"
Hopefully you'll find this useful.
My HTML file has some paragraphs where I am using an 8pt font-size. After converting the HTML to PDF, the paragraphs gain extra letter spacing and some letters are overlapped.
I tried all the suggestions from the following (like using dpi and xserver) but none are working.
http://code.google.com/p/wkhtmltopdf/issues/detail?id=72&q=letter
I realize that this question is a little bit old. But having received my fair share of frustration from this wkhtmltopdf letter-spacing issue, I want to drop a potential workaround:
'--dpi 96'
Add this option to the binary parameters, and you might be able to smile as I eventually did!
viz:
wkhtmltopdf --option1 --option2 --dpi 96 "www.host.com/page.html" file.pdf
In 2021 I fixed this by using 200 dpi.
Guys if you want to to get your PDF View as it looks in your Web Page copy the following code in your config/initializer/pdfkit.rb(create this file in initializer folder)
PDFKit.configure do |config|
config.wkhtmltopdf ='/usr/local/bin/wkhtmltopdf'
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"Ledger",
:zoom => '1.3',
:disable_smart_shrinking=>false
}
end
Plese check this link for more ":page_size "attributes
wkhtmltopdf: What paper sizes are valid?
And discover new options by visiting this link
http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html
Hope This all will help you. Thanks
Update wkhtmltopdf.
I found the best way to fix these letter spacing issues is to just ensure you're using the latest version of wkhtmltopdf (0.12.2.1 at the time of writing).
In Rails you can simply use the edge binary gem of this, like so:
gem 'wkhtmltopdf-binary-edge'
Give that a quick try first and see if your issues are fixed. Mine were.
It looks like ImageMagick does not always convert a single favicon.ico file to a predictable single png file - for some favicon's, it generates a bunch of other favicon-01.png, favicon-02.png, etc... Is there a way to figure out which one's the actual converted favicon you want - or figure out how many got generated, to delete the unwanted ones?
I came across with the same problem while I was trying to convert blogger's favicon and I solved it by using -flatten parameter of Imagemagick like this:
convert "favicon.ico" -thumbnail 16x16 -alpha on -background none -flatten "favicon.png"
This likely happens because there are multiple images in the icon file - this is to provide differet resolutions for different contexts. Presumably you'd want to run a search in the target directory for favicon*.png, then check the dimensions of each one to find the one you wanted (deleting the others as you go).
I guess some of these are animated gifs. You can take the first one as described here:
http://www.imagemagick.org/script/command-line-processing.php
i.e.:
$magick> convert 'images.gif[0]' image.png
I don't have ImageMagic installed, but you might try the above for all favicon.ico, it might work fine.
Otherwise, you would probably need to write a script to check for favicon-01.png and, if it exists, rename it to favicon.png and delete favicon-*.png (provided you don't have anything else named like that in the working folder).
I'd like to take a PDF file and convert it to images, each PDF page becoming a separate image.
"Convert a .doc or .pdf to an image and display a thumbnail in Ruby?" is a similar post, but it doesn't cover how to make separate images for each page.
Using RMagick itself, you can create images for different pages:
require 'RMagick'
pdf_file_name = "test.pdf"
im = Magick::Image.read(pdf_file_name)
The code above will give you an array arr[], which will have one entry for corresponding pages. Do this if you want to generate a JPEG image of the fifth page:
im[4].write(pdf_file_name + ".jpg")
But this will load the entire PDF, so it can be slow.
Alternatively, if you want to create an image of the fifth page and don't want to load the complete PDF file:
require 'RMagick'
pdf_file_name = "test.pdf[5]"
im = Magick::Image.read(pdf_file_name)
im[0].write(pdf_file_name + ".jpg")
ImageMagick can do that with PDFs. Presumably RMagick can do it too, but I'm not familiar with it.
The code from the post you linked to:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
pdf is an ImageList object, which according to the documentation delegates many of its methods to Array. You should be able to iterate over pdf and call write to write the individual images to files.
Since I can't find a way to deal with PDFs on a per-page basis in RMagick, I'd recommend first splitting the PDF into pages with pdftk's burst command, then dealing with the individual pages in RMagick. This is probably less performant than an all-in-one solution, but unfortunately no all-in-one solution presents itself.
There's also PDF::Toolkit for Ruby that hooks into pdftk but I've never used it.