archives

Recreating bokeh (just an idea)

Bokeh is that nice off-focus effect that good lenses can give you on portraits or photos of details, helping centering your attention on your subject and not on the background of the photo.

Unfortunately, “nice” bokeh is well-tied to “nice” (==expensive) lenses. And sometime simply you just have a photo shot with too much depth of field (maybe because there was too much light, or the photographer, for example my 6-years old daughter, just know to point and shot). So I thought… it’s possible to reproduce it on a photo after the fact?

The poor man solution is easy: select your foreground in GIMP or Photoshop or whatever you use to edit your photo, invert the selection, blur. But this does not look like bokeh at all. The problem is that with real bokeh, distant point of light are transformed in big disks, and nearer one (although off focus) on smaller ones. So I though… well, let’s try to play a bit.

So I prepared three “bokeh masks” that would represent how a point of light can be transformed at high, medium and low distances. See the example at the left.

Now the trick is to select one photo and to prepare a mask for it. Paint black the foreground, which will be left as-is, and in different shade of gray the other parts of the photo, from near (dark gray) to far (light gray) to very far (white). See the photo and the mask below.

(click on the photo to see the original photo, full size). now I have written a little program that apply the “blur” or, technically speaking, convolute the correct bokeh image on the photo.Here is the result:

(click for full size)

The program is written in python, it’s dog slow, it can be done better, the mask has been done with the electronic equivalent of a hammer, etc. etc. But this post is just to show the idea. In principle, a general program would interpolate among the bokeh images, to accept more than three levels of “distance”… but think the experiment you can do with it! Would you try an octagonal bokeh effect? Or a doughnut one?

Doughnut bokeh:

And this is one made with an hexagonal aperture pattern:

It’s a very subtle difference, but it’s there — I especially like the hexagonal one.

If you like I can share the program under GPLv3 license, for free inclusion in free software. For other kind of licenses, contact me :-)

BTW, it seems that I effectively reinvented the weel. See here…

Have a nice day!

 

How to modify a gnome shell theme

This is (I think) the correct way of doing what I tried to do with an extension. My objective was to reduce a bit the size of the gnome shell top bar, and especially the font size.

comparison of the top bar, before and after

The correct way is to use a “user” theme. What I did is to create a directory in the $HOME/.themes called “Romano” (guess why). You can find attached the files in the directory, but basically, the important file is the $HOME/.themes/Romano/gnome-shell/gnome-shell.css, which looks like that:

@import url("/usr/share/gnome-shell/theme/gnome-shell.css");

#panel {
    color: #ffffff;
    font-family: ubuntu, cantarell;
    font-size: 0.9em;
    font-weight: normal;
    height: 1.55em;
}

.panel-button {
    -natural-hpadding: 6px;
    -minimum-hpadding: 4px;
    font-family: MintSpirit, ubuntu, cantarrell;
    font-weight: bold;
    color: #ccc;
    transition-duration: 100;
}
.panel-button:hover {
    color: cyan;
    font-weight: bold;

}

The important line is the first, which loads the standard gnome-shell theme, and then the theme proceeds with modifying the font sizes and attributes. In this case, it set the font smaller, it reduces the space between buttons and indicators, and then changes the color when hovering atop the buttons with the mouse.

Obviously to use it you need the user-theme extension and then you have to activate it with the “advanced settings” (aka gnome-tweaks-tools; see for example here). And you need to restart gnome-shell to make the new theme “visible”:

Here you have the files, just to get started. Ah, yes, if you change the name of the theme, you have to modify the json file too…

download the mini-theme

romano-theme.tar.gz
Title : romano-theme.tar.gz
Caption :
File name : romano-theme.tar.gz
Size : 571 B

 

 

Randomly change the background of your Gnome desktop

One of the thing I never got to understand is why the Gnome desktop do not have the option (present in KDE) to use the content of a directory to randomly pick an image for the “wallpaper” of the desktop.

There are a lot of solution outside, but I would like to share with you mine: a simple python program, easily customizable and hackable, to do this task.

Enjoy!

A Gnome shell extension to change the top panel look

Notice: this is NOT the best way to achieve this. I have posted a much better way

This is my first Gnome Shell extension… so that will not be a coding style gem, but it works for me and I think it’s quite useful, so…

comparison of the top bar, before and after

The idea is to change the look of the top panel in Gnome Shell. The problem, with a netbook like my Asus EeePC, is that the font used is way too big, and (especially if you enable the classic tray) you get out of space for indicators icons very fast. In the image, you can see “after” and “before” my top panel bar.

So I made a bit of research and found not so much info on writing extensions, with a notable exception: the Finnbar P. Murphy blog, especially here and here.

I will explain here how to make that extension — this post will be not a step by step instruction, you need a bit of “hands on” to follow it. I hope to learn how to package it and made it available in simple terms.

The good thing is this is a general method to modify the “css” (style sheets) of the current theme, and has a lot of application more than changing the font size. Let’s go.

First of all you need the extension. I used gnome-shell-extension-tool to start, working around the known bug it has now. What you have in the end is a directory in your ~/.local/share/gnome-shell/extensions directory named ChangeTopFont@romano.rgtti.com with three files — extension.js, metadata.json, and stylesheet.css. You can download all of them from the archive at the end of this post.

The most important part is the extension.js code, which I have shamelessly copied from the aforementioned blog (this is my first JavaScript code ever!). It uses the file stylesheet.css to “patch” the style of the top panel (and you can use to modify whatever you want in the theme you have chosen: simply look at the /usr/share/gnome-shell/theme/gnome-shell.css file for hints.

I have concocted the extension so that you can modify the stylesheet.css file and see the changes by simply disable/enable the extension in gnome-tweak-tool, without any need to restart the shell.

NEWS: thanks to cbowman57 I have uploaded a second version of the extension, which  sports a better stylesheet. Thanks!

A last word: you have to enable the extension by add its UUID (the full name, in this case ChangeTopFont@romano.rgtti.com, to the org.gnome.shell “enabled-extensions” key (you can use dconf-editor for this).

extension.js code:

//
// Edited by Romano Giannetti <romano@rgtti.com>
// Copyright (c) 2011 Romano Giannetti
// 
// Original code by: http://blog.fpmurphy.com/2011/06/patching-a-gnome-shell-theme.html
//
// Copyright (c) 2011 Finnbarr P. Murphy
//

const St = imports.gi.St;
const Shell = imports.gi.Shell;
const Main = imports.ui.main;

let  defaultStylesheet, patchStylesheet;

function init(extensionMeta) {

    defaultStylesheet = Main._defaultCssStylesheet;
    patchStylesheet = extensionMeta.path + '/stylesheet.css';
}

function enable() {

    let themeContext = St.ThemeContext.get_for_stage(global.stage);
    let theme = new St.Theme ({ application_stylesheet: patchStylesheet,
                                theme_stylesheet: defaultStylesheet });
    try {
        themeContext.set_theme(theme);
    } catch (e) {
        global.logError('Stylesheet parse error: ' + e);
    }

}

function disable() {

 let themeContext = St.ThemeContext.get_for_stage(global.stage);
    let theme = new St.Theme ({ theme_stylesheet: defaultStylesheet });
    try {
        themeContext.set_theme(theme);
    } catch (e) {
        global.logError('Stylesheet parse error: ' + e);
    }
}

stylesheet.css code: (play with this!)

#panel {
    color: #ffffff;
    font-family: MintSpirit, ubuntu, cantarell;
    font-size: .80em;
    font-weight: normal;
    height: 1.55em;
}

.panel-button {
    -natural-hpadding: 4px;
    -minimum-hpadding: 4px;
    font-family: MintSpirit, ubuntu, cantarrell;
    font-weight: bold;
    color: #ccc;
    transition-duration: 100;
}
.panel-button:hover {
    color: white;
    font-weight: bold;

}

Files to download: (at your risk!)

extension code

change-top-font.tar.gz
Title : change-top-font.tar.gz
Caption :
File name : change-top-font.tar.gz
Size : 937 B
ChangeTopFont@romano.rgtti.com.tar.gz (v2)
Title : ChangeTopFont@romano.rgtti.com.tar.gz (v2)
Caption :
File name : ChangeTopFont@romano.rgtti_.com_.tar.gz
Size : 1,011 B

Xfig thumbnailers with Gnome3/Nautilus3

Ok, after the upgrade to Ubuntu 11.10 and the switch to Gnome 3, I discovered that my nice xfig thumbnailers stopped working. After a bit of struggle, I discovered why. The “gconf”  mechanism of  Gnome2 is being phased out, so now there is another way (simpler, when you know it) of doing the same thing.

Basically, you have to drop into /usr/share/thumbnailers (I tested with the $HOME/.local/share/thumbnailers to make the thing user-installable, but to no avail) a file of this kind:

[Thumbnailer Entry]
Exec=/usr/bin/xfig-thumb -s %s %i %o
MimeType=image/x-xfig;

with extension .thumbnailer … and it’s all.

I prepared a new package that should work in Gnome 2 and Gnome 3, but I have tested it only in Gnome 3. The main difference now is that you need to install/deinstall it using “sudo”. Please read the README.txt file in the package…

Download XFig thumbnailer package for Gnome3

xfig-thumbnailer3.tar.gz
Title : xfig-thumbnailer3.tar.gz
Caption :
File name : xfig-thumbnailer3.tar.gz
Size : 3 kB

Fast copy of photos — a solution!

configuring gthumb to run a script after a key pressSo, you have a (big) collection of photo, and you want to rapidly browse trough them and select a subset for further consideration for, for example, printing or editing them. What I would like was that,  while in the image viewer, with a simple key press I could copy the file to another, pre-chosen directory. No asking nor clicking, just a fast  keypress.

I know you can do it with digiKam, my preferred photo manager, by tagging or starring photo, then select and copy them, etc… but the problem is that I have my photos synchronized on different computers, and tags and stars are often specific of just one of them — so just physically copy the photos would be the better solution.

Firstly I tried to write a plugin for Eye of Gnome, the default viewer in my Ubuntu Natty. Unfortunately, following the instruction here did not show anything; I was not able to find instructions on how to create a plugin that really worked, and nothing on how to manage keyboard input. So I tried various other viewers and finally I decided for gThumb. It can add actions associated with key (only the numeric keypad ones, unfortunately) by selecting tools -> personalize… and adding a script there.

I saved the following script in my $HOME/bin/ directory, with name gthumb_copy_script.sh:

#! /bin/bash
#  arguments: <full path of the file> <just the file name>
finaldir=/home/multimedia/pictures/selected
if [[ -f $finaldir/$2 ]];
then
    zenity --warning --timeout=3 --title="failed COPY  to $finaldir" --text="$2 exists, skipped"
else
    cp $1 $finaldir
    zenity --info --timeout=1 --title="COPY to $finaldir" --text="$2 copied to $finaldir"
fi

Now you simply add this script to a the “tools -> personalize” widget and associate the command “gthumb_copy_script %F %B“(*) to a key, for example, key “2″ on the keypad — which in my laptop correspond to the key K for Kopy. A fast confirmation dialog is shown — I’d really preferred to use notify-send but Ubuntu developer, in their infinite wisdom, decided to ignore the -t argument, cause evidently no one wants notifications for less than 10 seconds…

Now I have a working solution. But the sad end of the story is that, while we have a better desktop in Linux, we are slowly but constantly losing the configurability that it was, once, its main selling point. Sigh…

(*) %F is the full path of the photo, %B just the file name.

Inherit the Stars: re-reading an old book

Cover of the italian translation of the bookThanks to my new kindle, I was re-reading Inherit the Stars by James P. Hogan, which is available for free at Baen. I was especially fond of this book when I was a teenager, for the crisp image of how the scientific thinking works towards the finding of solutions to apparently impossible problems.

Well, obviously a lot of things have changed since 1977, oneself for a start. So I do not find the book so good as when I was 14, but it can be still read.

The real good thing that I noticed is that the thing that most surprise you reading it is not the fact that URSS is still there, that  we should have personal jets available for renting at popular prices, or that the concept of personal computer is at least strange. No, the real things that makes the book sound dated is the fact that the scientists and the bosses are practically all male, and that all of them keep smoking as hell… even in the moon-to-jupiter starship.

The fact that this surprised me so much say a lot on how and much things are changing. This is, I think, a good news.

 

Adding a gnome/nautilus thumbnailer

thumbnails for xfig files

NOTE: this only works for Gnome 2. For Gnome 3, see an update here

What it is:

a thumbnail is the little image that Nautilus (the file manager of Gnome) shows to represent the file. Most of file types has a standard thumbnailer, and you see a representation of the file such as a little image or a preview of the contents. This is quite useful, but less common filetypes do not have a preview, you just see a standard icon, the same for all of them.

I will explain how to add a thumbnailer for a new or unsupported file type. In that case we will add a script to prepare the preview of Xfig files (.fig).

How it works:

When trying to prepare the preview, Nautilus do the following (at least, I think…):
First step: it tries to find a preview computed before. To do this, it generate a hash for the file and looks into the directories under your $HOME/.thumbnails dir. If it find it, or find an entry that said the generation of the thumbnail failed before, it uses it (or a defualt icon) and stop searching. That means that, if you want to change the thumbnail generation and see the effects, you have to delete this “cache” under $HOME/.thumbnails.
Second step: if the type of the file is known, it generates (and saves in the cache) the preview. To see which is the type of a file, run

xdg-mime query filetype <file>

For example:

% xdg-mime query filetype xfig-thumbnailer.tar.gz
application/x-compressed-tar

If the application type do not exist (blank or wrong answer), you can easily add your own filetype to the system.

Third step: if all this fail, it searches the “registry” (gconf database) for a couple of  entries called after the type; for example:

/desktop/gnome/thumbnailers/application@x-compressed-tar/command

/desktop/gnome/thumbnailers/application@x-compressed-tar/enable

with a @ char in the place of the "/". If “enable” is true, it then call the command to generate the thumbnail. “command” is in the form

my_thumbnailer -s %s %u %o

and when calling it %s is substituted for the vertical size of the preview image, %u for the URL of the file (if it’s a local file, in the form of file:///path/to/file), and %o the output file where the preview (in PNG format) has to be stored.

The practical case: adding a thumbnailer for Xfig files.

Acknowlegment: this is mostly extracted/reworked from  here.

In the <package> file, you have all you need to add the thumbnails for your xfig file. Move it in a directory and uncompress it.

You will see 4 files:

xfig-thumb-setup.sh : this script moves the script to your $HOME/bin directory and enables the gconf entry that will run it.

xfig-thumb-remove.sh: this script undoes what the previous did. Run it if you want to get rid of the thumbnailer.

xfig-thumb: this is the script that create the thumbnailer. It will need that you have xfig, fig2dev and imagemagick packages installed, otherwise it will fail (and you can see error messages in your $HOME/.xsession-errors file).

If you look at it, you will see that it creates a temporary file (there are some nice tricks there to safely delete it afterward), generate a PNG version of the file and then resizes it to the requested dimension and adds a little watermark text.
Enjoy!

Download it!

xfig-thumbnailer.tar.gz
Title : xfig-thumbnailer.tar.gz
Caption : thumbnailer for Xfig files
File name : xfig-thumbnailer.tar.gz
Size : 2 kB

Integrating a new application in Gnome

part of the screenshot defining the new application as defaultJust found out that the task of adding a new application for a new file-type on gnome (so that I can run it by double click, or from the mail client on an attachment, is easier than I suspected.

The problem: my bank send me my account information in files that are managed by an application that they supply. It’s a MS Windows application, but fortunately most of it runs ok under wine. The files are zip files, with a custom extension *.fm6.

The first step is to make gnome aware that .fm6 files are different from zip files, and are to be treated differently. That is called “adding a MIME type” to the system. To do so, I added a file, called fastm6.xml, in $HOME/.local/share/mime/packages/, and then issued the command

update-mime-database $HOME/.local/share/mime

Content of the $HOME/.local/share/mime/packages/fastm6.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-fastm6">
<glob pattern="*.fm6"/>
<comment>Caja de Ingenieros files</comment>
</mime-type>
</mime-info>

To add an icon for the new mime type, simply drop a png file (size 48×48) into $HOME/.local/share/icons called application-x-fastm6.png. Refresh your nautilus windows and voilà, you have them.

Now you can right click on the icons can choose the application you want to run for them (“open with another application” and the enter it as a default). In my case it was a shell script like that:

#!/bin/bash
#
fastdir=$HOME/.wine/dosdevices/c:/FAST/FASTM6
cp $1 $fastdir
cd $fastdir
wine c:/FAST/FASTM6/fastm6.exe $(basename $1)

but you mileage may, obviously, vary.

Giving up: I bought an ebook

the new kindle with a red laceI am quite a geek in a lot of thing, but I resisted, till now, to leave my papery books and do the jump at the new technology.

I have tons of book on the shelves of my home (and in my mother’s house, and in the office, and in boxes rotting away… they’re just too much to keep them all at hand).

So when the last book of the Void trilogy by Peter F. Hamilton come out, and I saw that it was quite a big book as ever, I decided that my back (and my pockets) would be pleased at the change. At the same time, Amazon.com sent out the new version of its Kindle ebook, and after a bit of thinking, comparison and googling, I went for it. Why it and not another one (like the very nice Nook) was a decision based on the fact that this was the only one that gave me a simple way to buy books even from outside the U.S. — hint for the competition.

After three weeks with it, I can draw some initial conclusions about the product. Globally, I am very pleased with it.

On the plus side, the readability, which is really excellent. It is really at they say, the same as a (good) sheet of paper. The management of the books, very nice and easy to use and understand, not intrusive at all. You can rapidly forget about the gadget and think only to the book. And it works very well in conjunction with Calibre in Linux, which is definitely a plus.

On the minus side, the PDF viewing could be better (the kindle can only zoom at fixed values, as 100%, 150%, etc), which makes reading sometime difficult. And on the experimental features, like web browsing and background audio playing, things could be better, but they are definitely usable.

In the end, I am quite happy. If only the flying authority would not force to shut it off during take-off and landing (I doubt that the kindle, with WiFi off, can emit much more RF than my digital wrist clock), I would say that about the reading experience, there are no downsides with respect to a paper book. I am so satisfied that if there were a way to (cheaply and legally) “move” my physical book to the kindle, and recycle the paper, I would do it with 90% of my books — basically all the paperback and one half of the hardcover.