#pix0meter # #!/usr/bin/python # -*- encoding:utf-8 -*- """pixOmeter, a tiny tool to calculate sizes in mm and px and definition in dpi""" # Author : Ginko # Date : 31/12/2014 # Version : 0.1.a # License : zlib/libpng # The zlib/libpng License # Copyright (c) 2014 Ginko Aloe - ginkobox.fr # This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. # Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: # 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. ISO_formats = {'A0': (841, 1189), # from https://en.wikipedia.org/wiki/ISO_216 'B0': (1000, 1414), 'A1': (594, 841), 'B1': (707, 1000), 'A2': (420, 594), 'B2': (500, 707), 'A3': (297, 420), 'B3': (353, 500), 'A4': (210, 297), 'B4': (250, 353), 'A5': (148, 210), 'B5': (176, 250), 'A6': (105, 148), 'B6': (125, 176), 'A7': (74, 105), 'B7': (88, 125), 'A8': (52, 74), 'B8': (62, 88), 'A9': (37, 52), 'B9': (44, 62), 'A10': (26, 37), 'B10': (31, 44), 'C0': (917, 1297), 'C1': (648, 917), 'C2': (458, 648), 'C3': (324, 458), 'C4': (229, 324), 'C5': (162, 229), 'C6': (114, 162), 'C7/6': (81, 162), 'C7': (81, 114), 'C8': (57, 81), 'C9': (40, 57), 'C10': (28, 40), 'DL': (110, 220) } class Dimension(object): def __init__(self, size=0, definition=0, pixel=0): if definition: self.definition = definition if size and definition: self.size = size self.pixel = self.definition * self.size / 25.4 # def in inch to def in mm elif definition and pixel: self.pixel = pixel self.size = self.pixel / self.definition * 25.4 elif size and pixel: self.size = size; self.pixel = pixel self.definition = self.pixel / self.size * 25.4 def __str__(self): return("{size}mm @ {definition:.0f}dpi = {pixel:.0f}px".format(**self.__dict__)) def iso(_format, definition): if _format not in ISO_formats.keys(): print("Unknow ISO format") else: l, w = ISO_formats[_format] L = Dimension(l, definition) W = Dimension(w, definition) print("{format}: {L.size} x {W.size} mm @ {definition:.0f}dpi = {L.pixel:.0f} x {W.pixel:.0f} px".format( format=_format, L=L, W=W, definition=definition)) def main(): DEFAULT_DEF = 200 import argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-i", "--iso", help="Paper size ISO format, -l for complete list", dest="format") parser.add_argument("-l", "--list", help="List of supported ISO formats", action='store_true') parser.add_argument("-s", "--size", help="Size in millimeters", dest="size", type=float) parser.add_argument("-d", "--definition", help="Definition in dpi", dest="definition", type=int) parser.add_argument("-p", "--pixel", help="Size in pixel", dest="pixel", type=int) args = parser.parse_args() if args.list: for k, (w, l) in sorted(ISO_formats.items()): print("{}: {} x {}".format(k, w, l)) if args.format: if not args.definition: print("Default definition used: {}dpi".format(DEFAULT_DEF)) iso(args.format, DEFAULT_DEF) else: iso(args.format, args.definition) else: defined = [i for i in (args.size, args.definition, args.pixel) if i] if len(defined) < 2: print("Missing arguments") elif len(defined) > 2: print("Too many arguments") else: print(Dimension(args.size, args.definition, args.pixel)) if __name__ == '__main__': main()