[Sugar-devel] pixels to centimeters

Walter Bender walter.bender at gmail.com
Wed Aug 15 15:05:12 EDT 2012


2012/8/13 Flavio Danesse <fdanesse at gmail.com>:
> Hey.
>
> I'm trying to build a GUI for touch screen and want the widgets have certain
> dimensions in inches or millimeters, so I need to convert from pixels to
> centimeters or millimeters.
>
> So the query is:
>
> How do I convert pixels to centimeters?
>
> What I found on the web is about to 1 pixel measured 0.03 inches
> apromadamente, but in practice the numbers did not close.
>
> I tried doing something like this:
>
> gi import
> gi.repository import from GdkX11
>
>  screen = GdkX11.X11Screen()
>
> After which, with: screen.width () get the horizontal resolution 1366, which
> multiplied by 0.03 gives me 40.98 while my monitor actually measures 34.5
> cm.
>
> Another function of GdkX11: screen.width_mm () returns me 361 millimeters.
>
>
> Hola.
>
> Estoy intentando construir una interfaz gráfica para touch screen y quiero
> que los widgets tengan determinadas dimensiones en centímetros o milímetros,
> por lo cual necesito convertir de pixels a centímetros o a milímetros.
>
> De modo que la consulta es:
>
> ¿Cómo convierto pixels a centímetros?
>
> Lo que he encontrado en la web gira en torno a que 1 pixel mide
> apromadamente 0.03 centímetros, pero en la práctica los números no me
> cierran.
>
> He intentado hacer algo como esto:
>
> import gi
> from gi.repository import GdkX11
>
> screen = GdkX11.X11Screen()
>
> Luego de lo cual, con: screen.width() obtengo la resolución horizontal 1366,
> lo cual multiplicado por 0.03 me da 40.98 mientras que mi monitor mide en
> realidad 34.5 cm.
>
> Otra función de GdkX11: screen.width_mm() me devuelve 361 milímetros.
>
>
>
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel
>

As per CJL's suggestion, here is what I do in Ruler:

def calc_dpi():
    '''Looking for 'dimensions' line in xdpyinfo
       dimensions:    1280x800 pixels (339x212 millimeters)'''
    (status, output) = commands.getstatusoutput('/usr/bin/xdpyinfo')
    if status == 0:
        strings = output[find(output, 'dimensions:'):].split()
        w = int(strings[1].split('x')[0])  # e.g., 1280x800
        mm = int(strings[3][1:].split('x')[0])  # e.g., (339x212)
        return int((w * 25.4 / mm) + 0.5), True
    else:
        # just in case the above fails
        return 96, False

regards.

-walter


-- 
Walter Bender
Sugar Labs
http://www.sugarlabs.org


More information about the Sugar-devel mailing list