Hyppää sisältöön

Learning by fixing

A few days ago I tried out Nikola galleries. And then noticed that some images in the gallery were a bit weird. Here is one example:


It was a small bug in Nikola, and while fixing it I learned something new about some technologies and programs.

I had took the picture with my phone. Shotwell showed the picture correctly while I cropped and exported it. So did Gnome image viewer and Nautilus. But when I added it to the gallery, the top and the bottom of the tower were missing and both sides were black.

It turned out that the picture was actually sideways. The phone had embedded EXIF orientation metadata to the image, and so the programs could show it correctly. Also Nikola gallery read that metadata and rotated the image.

Nikola uses Pillow (PIL) library to handle images and piexif library to handle EXIF metadata. The image was rotated by Image.rotate method, but it by default does not change the size of the image. To updated the size it should be used with expand flag, or — as in this case — it is possible (or even recommented) to use Image.transpose method.

So the actual fix was simple, changes in four lines. But to find out the cause and to figure out the fix I learned following things:

  • Camera (or phone) has sensors to detect the orientation.
  • The orientation is stored in to the picture in EXIF metadata.
  • Some programs use and can show the EXIF metadata, for example Gnome image viewer and exiv2 command line utility.
  • Nikola strips off the metadata (as if configured), and rotates the final image and thumbnail.
  • PIL seems to be very handy library to manipulate images, or even draw images. I generated some test images for the fix.
  • I used piexif to set orientation to test images.
  • To deliver fix to Nikola project I (re-)learned to use Git version controls system. GitHub and pull requests there were also rather new to me.

This was fun and now the bug is fixed. Also the new version of Nikola containing the fix has been just released.