PDF to PNG using PythonMagick

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 documentation is of little help. 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 72. Big thanks to PyBlosxom for providing a working example of code and restoring my sanity ;-).

FAIL
import PythonMagick
img = PythonMagick.Image("Desktop/test.PDF")
img.density("300") # too late, already read as 72 dpi when image instantiated
img.write("Desktop/test.PNG")

SUCCEED
import PythonMagick
img = PythonMagick.Image()
img.density("300")
img.read("Desktop/test.PDF") # read in at 300 dpi
img.write("Desktop/test.PNG")

Apparently, PIL is no good for this task as it can only write PDFs (http://www.velocityreviews.com/forums/t563423-convert-pdf-to-png.html)

And if you are confused about how to supply the Geometry argument, that is because there are several ways of doing it:

image.density(Geometry(150,150)); // could also use image.density("150x150")
Magick::Image Class
image manipulation with python

Here are some links that may be useful:

PythonMagick Readme

Ubuntu system from HDD to SSD OS and HDD Home

I finally took the leap and changed from a HDD-only system to a machine running the OS (Ubuntu Precise) on a nice fast SSD (60GB) and the home folder on a 3TB HDD. I also took the opportunity to do a totally clean install of Precise to remove any cruft that had built up over all the upgrades from Intrepid onwards. The project was a success and took almost 1.5 days for every single configuration, cron job, package installation, theme tweak etc to be completed.

The benefits: 1) the system boots, runs, and closes down much faster. I didn’t think my system was slow before but I can never go back 😉 2) the system is presumably less buggy or complicated because all the years of experimental installations, repositories, etc are gone. System updates should also be more streamlined, and 3) the system/home split should make it easier to repair the system and otherwise manage it.

OK – now to the nuts and bolts as best I can recall them from my scribbled notes. What follows is a mix of what I actually did and how I would do it if doing it again 😉

Format SSD to ext4

ext4 is a perfectly good choice – see Installing Ubuntu on a SSD so that’s what I chose.

I added the SSD to my system ahead of the existing HDD so it would be sda not b. That is because it is going to be the boot drive for the final system. It also provides a logic for the order chosen which makes it less likely confusion will result in the wrong disk being wiped.

Being only 60GB there was no problem using cfdisk for this purpose. The main thing was to correctly identify which disk was the one to wipe AND NOT TO WIPE THE MAIN SYSTEM BY ACCIDENT! Merely swapping a and b would be enough for a disaster. But of course you have a complete bit for bit copy of the original system HDD made using the dd command right ;-). The instructions at http://p-s.co.nz/wordpress/using-cfdisk-to-prepare-a-usb-hdd-to-store-backups/ are useful but remember to check the order. In my case I was wanting sda to be the SSD so I wanted to format sda NOT sdb in my case. The best check is the presence or absence of partitions as per the screen shot in the referenced blog post. The different sizes was a final check.

Install Ubuntu Precise onto SSD

At this point you may wish to physically remove your original HDD so there is no risk of installing Ubuntu onto the wrong device. Of course, you would never do anything so dumb but … OK, now you may need to enter BIOS during boot up and ensure you can boot from USB or CD (where your Ubuntu installer ISO is).

Now just install Ubuntu onto the SSD. It will be small (in my case only 60GB) so it should be easy to tell you are installing onto the correct device. Don’t worry that the installation is setting your home folder up on the SSD at this stage. We will change that over later.

Format 3TB HDD to ext4

Remove the old HDD if still there and put in the 3TB drive. Boot into the BIOS and change order or HDDs so you boot into the SSD not the HDD. OK – you’re in a clean install of Ubuntu. From here we can attach to the 3TB system and format it to ext4. Make sure you don’t have the 3TB system mounted before you try to format it or you won’t be able to. In my case there was the added problem that the brand new HDD had some junk installed on the first 100MB or so. If so, wipe that before doing a conventional format using parted (cfdisk is not available because of the disk size remember).

Note – since you have removed your precious original HDD there is no risk of accidentally destroying it with the next step. The worst that can happen is you mess up your SSD and have to reinstall Ubuntu on it (no big deal these days). But nonetheless, make sure you wipe the correct disk. In my case, the main boot system is sda (a is first is the logic) and the 3TB HDD is b. So if we run the following from the terminal on the SSD Ubuntu system we clean the 3TB HDD of manufacturer cruft:

sudo dd if=/dev/zero of=/dev/sdb bs=1M count=100

Being over 2TB in my case, we can’t use cfdisk to format what is going to be our home system. See Linux Creating a Partition Size Larger Than 2TB. Follow the instructions there. Don’t forget to run print at the end of the parted steps to check everything is OK. In my case I wanted ext4 so my final step was:

sudo mkfs.ext4 /dev/sdb1

Note: “b” is the HDD in my case because I have used “a” for my boot OS. Make sure you don’t mkfs onto the wrong drive.

Transfer fresh home contents to 3TB HDD

OK we now have two disks – SSD as “a”, and the 3TB HDD as “b”. We start by copying all the home contents from the clean install on “a” across to “b” so that we can use “b” as our home folder.

Some good instructions are in Partitioning/Home/Moving

Install packages onto SSD

The following list is for my benefit, but it gets across the idea that there are a lot of packages in play. For every package added there is probably another which got left behind by the clean install process.

  • synaptic (sometime makes installation easier if you know the packages you need)
  • cups-pdf (for printing to PDF)
  • bazaar (for development)
  • dh-make (for making deb packages)
  • python-nose (for testing Python code)
  • virtualbox
  • sqlite database browser
  • vlc (video and podcasts)
  • f-spot (photos)
  • php5
  • mysql (server and client)
  • php5-mysql
  • php5-xdebug
  • inkscape
  • shutter (for screenshots)
  • eclipse
  • netbeans
  • idle
  • gimp
  • k3b (disk burning – the only reliable way to avoid making coasters)
  • hardinfo (for getting system info)
  • gnome-utils (includes the brilliant Baobab visual disk usage utility)
  • dropbox
  • all the pre-reqs of my SOFA Statistics package
  • scheduled tasks
  • scribus
  • 7-zip (p7zip)
  • gFTP
  • samba (for shared folders)
  • wine (so I can install Windows program SQLyog I use for MySQL management and scheduled remote one-way syncronisation backups; and RegexBuddy)
  • Set up emblems, and added some very useful menu options such as open as root, set as wallpaper etc, as per Add emblems to Nautilus’ Files & Folders in Ubuntu 12.04 & 11.10. Installs nautilus-actions-extra.
  • System load indicator and System monitor indicator (indicator-sysmonitor) for my panel. NB after adding ppa for alexeftimie manually change from precise to oneiric via the GUI (see original instructions in Things To Tweak / Fix After Installing Ubuntu 11.04 Natty Narwhal). Set both up so they start on startup
  • libreoffice-style-galaxy (then follow simple steps in [Solved] LibO Toolbar Icon choice?)
  • ttf-mscorefonts-installer
  • gnome-games-extra-data (and then set card theme in aisleriot to Paris

Extra Setup

  • Get PHP plugin for netbeans (had to make update centres active before I could access the PHP plugin)
  • Get NZ dictionary for LibreOffice (the OpenOffice dictionary is what you need)
  • Set up sharing on shared folders
  • I added a custom command to the Open With dialog as per Add custom command in the open with dialog? so that SQLite databases could automatically open with SQLite Database Browser. Did same with IDLE and python scripts.
  • Get PyDev (Note – use actual link not the initial link that redirects) and Pylint for Eclipse. Pylint was overly allergic to gettext’s _() and I managed to solve that by running PyDev>Code Analysis fresh. Not sure exactly what it was I did that solved the annoying problem of excessive error messages.
  • Fixed up minor dual monitor issue – see Could Not Apply The Stored Configuration For Monitors
  • Manually added bookmarks to Nautilus so that they appear as quicklists against Home folder on launcher.
  • Bring across old ~/.gftp folder with all bookmarks etc)
  • Got all my scheduled cron jobs going. Found old copy of text generated by crontab – e which had been saved as “paste into crontab -e.txt”. Pasted it back into crontab -e and wrote to file. The following explains why to use crontab -e How do I? Edit Crontab using a Script?
  • Set up apache so localhost refers to a different folder (in my case my projects folder), not /var/www. Use sudo gedit /etc/apache2/apache2.conf. Change DocumentRoot and the Directory setting for that path in /etc/apache2/sites-available

    DocumentRoot /home/g/projects
    <Directory /home/g/projects/>

  • Get xdebug working so we can debug PHP applications in netbeans. Edit php.ini (/etc/php5/php.ini) so that it has the following lines at the top by sudo gedit /etc/php5/apache2/php.ini:

    xdebug.remote_enable=1
    xdebug.remote_handler=dbgp
    xdebug.remote_mode=req
    xdebug.remote_host=localhost
    xdebug.remote_port=9000

    Check in /etc/php5/conf.d/xdebug.ini that the path is actually exactly correct (esp the date part of the path):

    zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so

    Note: restart apache to see changes:

    sudo /etc/init.d/apache2 start
    sudo /etc/init.d/apache2 stop

    Or the following style does the same:

    sudo service apache2 start

  • To remove overlay scrollbar (I have a large screen setup so would rather have convenience than save screen real estate) sudo apt-get remove overlay-scrollbar, then go into synaptic and manually remove the appropriate liboverlay-scrollbar-n.n-n
  • To remove global menus as per Disable the Global Menu in Ubuntu 12.04 (Precise Pangolin)
    sudo apt-get autoremove appmenu-gtk appmenu-gtk3 appmenu-qt
  • Speed up GRUB (I set GRUB_TIMEOUT=2) and tidy it up re: left overs from when old HDD was attached
    sudo gedit /etc/default/grub

    GRUB_TIMEOUT=2

    sudo sudo grub-mkconfig (not sure if this is needed or if grub.cfg automatically created as part of next step anyway making it redundant)
    sudo update-grub

Transfer user files and extra config from original HDD

I attached the original HDD as a USB HDD. It was automatically mounted and I was able to copy across folders and files using the GUI. For larger jobs. If, for some reason, a large copying process is interrupted, I found it easy to use rsync to copy the resources across again).

sudo rsync -avr /media/<uuid of mounted USB HDD>/ /home/

Note: From the rsync man file:

A trailing slash on the source changes this behavior to avoid creating
an additional directory level at the destination. You can think of a
trailing / on a source as meaning “copy the contents of this directory”
as opposed to “copy the directory by name”, but in both cases the
attributes of the containing directory are transferred to the contain?
ing directory on the destination. In other words, each of the follow?
ing commands copies the files in the same way, including their setting
of the attributes of /dest/foo:

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

Also see the brilliantly titled: Rsync: Enough Rope…

In my case I wanted the following:

  • Music (over 5,000 large files so it took a while)
  • Pictures (including photos so it took a while)
  • Projects (all my development work)
  • VirtualBox VMs and also .VirtualBox (massive files)
  • Misc (includes lots of legacy files)
  • .wine (several important programs including SQLyog and RegexBuddy)
  • .local/share/rhythmbox/playlists.xml (I would sorely miss these)
  • .ssh (otherwise launchpad/bzr push won’t work anymore)
  • Lots of settings files from mozilla firefox and thunderbird (brilliant – all my bookmarks, extensions, history, toolbar buttons, email accounts, email history) etc
  • .gnome/gnome-schedule/crontab (all the titles for my cron jobs – see Creating a Named Cron Job)
  • .dropbox (Note – I had to enter my email and password etc into the Dropbox dialog which popped up later so that it could refresh its connection)
  • .bazaar
  • MySQL – with root permissions copy folders and files from /var/lib/mysql. Will need to mass change owner and groups using
    chown -R mysql:mysql /var/lib/mysql
    I needed to operate in the command line for this as root IIRC – see RootSudo
    Need frms etc for MyISAM and idbdata1 for innodb tables. See Copying MySQL Databases to Another Machine but basically do lots of comparison and checking until everything is working. Check you can see tables of all sorts and all databases.