OpenCores
URL https://opencores.org/ocsvn/phr/phr/trunk

Subversion Repositories phr

[/] [phr/] [trunk/] [placas/] [PHRboard/] [references/] [logo2pcb/] [output/] [converter/] [img2mod.py] - Blame information for rev 397

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 88 guanucolui
#!/usr/bin/env python
2
#
3
# img2mod - A script to convert an image file into a PCB module file for Kicad.
4
# Version 1.02 - June 19, 2013
5
# Now works with the new .kicad_mod s-expression file format
6
#
7
# Written by Matthew Beckler for Wayne and Layne, LLC
8
# Copyright (c) 2011, Wayne and Layne, LLC
9
# http://img2mod.wayneandlayne.com/ - wayneandlayne at wayneandlayne dot com
10
#
11
# This program is free software; you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation; either version 2 of the License, or
14
# (at your option) any later version.
15
 
16
 
17
from PIL import Image, ImageOps
18
 
19
# Text for the file header, the parameter is the name of the module, ex "LOGO".
20
header = """(module %(name)s
21
  (layer F.Cu)
22
  (at 0 0)
23
  (fp_text reference "VAL***" (at 0 10) (layer F.SilkS) hide
24
    (effects (font (thickness 0.3)))
25
  )
26
  (fp_text reference "%(name)s" (at 0 5) (layer F.SilkS) hide
27
    (effects (font (thickness 0.3)))
28
  )
29
"""
30
 
31
# Text for the file footer, the only parameter is the name of the module
32
footer = """)
33
"""
34
 
35
# Places a pixel with (x, y) at the upper-left corner
36
# Size is in units of mm
37
def make_pixel(x, y, px_size):
38
    return """  (fp_poly
39
    (pts
40
      (xy %(0)s %(1)s)
41
      (xy %(2)s %(1)s)
42
      (xy %(2)s %(3)s)
43
      (xy %(0)s %(3)s)
44
      (xy %(0)s %(1)s)
45
    )
46
    (layer F.SilkS)
47
    (width 0.01)
48
  )
49
""" % {"0": x, "1": y, "2": x + px_size, "3": y + px_size}
50
 
51
def conv_image_to_module(image, module_name, scale_factor):
52
    """ Returns the text for the module, and the size: (x, y) in mm. """
53
    w, h = image.size
54
    #print "Original image dimensions: {0} x {1}".format(w, h)
55
    #print "Writing module file to \"{0}\"".format(output_filename)
56
    module = header % {"name": module_name}
57
    for y in range(h):
58
        for x in range(w):
59
            #print image.getpixel((x,y))
60
            if image.getpixel((x, y)) == 0:
61
                module += make_pixel(scale_factor * x, scale_factor * y, scale_factor)
62
    module += footer % {"name": module_name}
63
    return module, (scale_factor * w, scale_factor * h)
64
 
65
 
66
def main():
67
    import sys
68
 
69
    if len(sys.argv) < 5:
70
        print "Usage: %s input_image output_filename module_name scale_factor" % sys.argv[0]
71
        print "  input_image is the filename of the input image"
72
        print "  output_filename is the name of the output module file"
73
        print "  module_name is the name to use for the module, traditionally LOGO"
74
        print "  scale_factor is the size of each output pixel, in units of mm\""
75
        sys.exit(1)
76
 
77
    input_image = sys.argv[1]
78
    output_filename = sys.argv[2]
79
    module_name = sys.argv[3]
80
    scale_factor = int(sys.argv[4])
81
 
82
    print "Reading image from \"%s\"" % input_image
83
 
84
    # If we .convert("1") it makes a binary image with a 127 threshold
85
    # Keeping it at greyscale allows us to make the threshold configurable
86
    image = Image.open(input_image).convert("L")
87
    # The above conversion will assume a black background for removing transparency.
88
    # If we want to use a different background color, use this:
89
    #white = Image.new("RGB", image.size, (255,255,255))
90
    #r,g,b,a = image.split()
91
    #image = Image.composite(image, white, a)
92
 
93
    # If you want to invert the image:
94
    #image = ImageOps.invert(image)
95
 
96
    # If you want to do non-127 thresholding, change the 127 below
97
    image = image.point(lambda i: 0 if i < 127 else 255)
98
 
99
    module, size = conv_image_to_module(image, module_name, scale_factor)
100
    print "Output image size: %f x %f mm" % (size[0], size[1])
101
 
102
    fid = open(output_filename, "w")
103
    fid.write(module)
104
    fid.close()
105
 
106
if __name__ == "__main__":
107
    main()
108
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.