{"id":973,"date":"2012-06-10T21:27:12","date_gmt":"2012-06-10T09:27:12","guid":{"rendered":"http:\/\/p-s.co.nz\/wordpress\/?p=973"},"modified":"2012-06-10T21:28:24","modified_gmt":"2012-06-10T09:28:24","slug":"pdf-to-png-using-pythonmagick","status":"publish","type":"post","link":"http:\/\/p-s.co.nz\/wordpress\/pdf-to-png-using-pythonmagick\/","title":{"rendered":"PDF to PNG using PythonMagick"},"content":{"rendered":"<p>The problem: you have a large, crisp PDF image and you use PythonMagick to write it as a PNG but it comes out as a small, low-resolution, blurry PNG. You mess around with density, size, and quality settings to no avail. And the <a href=\"http:\/\/shallowsky.com\/blog\/programming\/py-image-manipulation.html\" target=\"_blank\">documentation is of little help<\/a>. The answer is subtle, and here it is: you must only read the PDF image AFTER you have set the density to something high like 300. Otherwise it is the default dpi of only <a href=\"http:\/\/www.imagemagick.org\/discourse-server\/viewtopic.php?f=3&#038;t=19938\" target=\"_blank\">72<\/a>. Big thanks to <a href=\"http:\/\/eccentric.cx\/blog\/python\/pythonmagick_pdf_to_jpeg.ecc\" target=\"_blank\">PyBlosxom<\/a> for providing a working example of code and restoring my sanity ;-).<\/p>\n<p><strong>FAIL<\/strong><br \/>\n<code>import PythonMagick<br \/>\nimg = PythonMagick.Image(\"Desktop\/test.PDF\")<br \/>\nimg.density(\"300\") # too late, already read as 72 dpi when image instantiated<br \/>\nimg.write(\"Desktop\/test.PNG\")<\/code><\/p>\n<p><strong>SUCCEED<\/strong><br \/>\n<code>import PythonMagick<br \/>\nimg = PythonMagick.Image()<br \/>\nimg.density(\"300\")<br \/>\nimg.read(\"Desktop\/test.PDF\") # read in at 300 dpi<br \/>\nimg.write(\"Desktop\/test.PNG\")<\/code><\/p>\n<p>Apparently, PIL is no good for this task as it can only write PDFs (<a href=\"http:\/\/www.velocityreviews.com\/forums\/t563423-convert-pdf-to-png.html\" target=\"_blank\">http:\/\/www.velocityreviews.com\/forums\/t563423-convert-pdf-to-png.html<\/a>)<\/p>\n<p>And if you are confused about how to supply the Geometry argument, that is because there are several ways of doing it:<\/p>\n<p><code>image.density(Geometry(150,150));   \/\/ could also use image.density(\"150x150\") <\/code><br \/>\n<a href=\"http:\/\/www.imagemagick.org\/Magick++\/Image.html#Image%20Manipulation%20Methods\" target=\"_blank\">Magick::Image Class<\/a><br \/>\n<a href=\"http:\/\/kerbtier.ch\/2009\/03\/11\/image-manipulation-with-python\" target=\"_blank\">image manipulation with python<\/a><\/p>\n<p>Here are some links that may be useful:<\/p>\n<p><a href=\"http:\/\/www.imagemagick.org\/download\/python\/README.txt\" target=\"_blank\">PythonMagick Readme<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem: you have a large, crisp PDF image and you use PythonMagick to write it as a PNG but it comes out as a small, low-resolution, blurry PNG. You mess around with density, size, and quality settings to no &hellip; <a href=\"http:\/\/p-s.co.nz\/wordpress\/pdf-to-png-using-pythonmagick\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[],"class_list":["post-973","post","type-post","status-publish","format-standard","hentry","category-graphics","category-python"],"_links":{"self":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/973"}],"collection":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/comments?post=973"}],"version-history":[{"count":6,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/973\/revisions"}],"predecessor-version":[{"id":979,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/973\/revisions\/979"}],"wp:attachment":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/media?parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}