summarylogtreecommitdiffstats
path: root/dxf-thumbnailer.patch
blob: ef32b826379d6f35fc436078ad78aea8d86b44fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--- dxf-thumbnailer.py
+++ dxf-thumbnailer.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 #***************************************************************************
 #*                                                                         *
@@ -30,7 +30,7 @@
 '''
 Linux DXF thumbnailer.
 
-This application generates a 128x128px PNG image showing the contents of a DXF
+This application generates a 512x512px PNG image showing the contents of a DXF
 file. In order to speed up the image generation process, only a few types of
 entities are read, so complex files might not get rendered accurately or even
 fail to render.
@@ -39,13 +39,49 @@
 as a thumbnailer for dxf file type (mime type: image/x-dxf).
 '''
 
-import sys, getopt, gnomevfs, Image, ImageDraw
-
-opt,par = getopt.getopt(sys.argv[1:],'-s:')
-inpfile = gnomevfs.get_local_path_from_uri(par[0])
-outfile = par[1]
-# inpfile = par[0]
-# outfile = 'test.png'
+import os
+import sys
+import getopt
+from urllib.parse import urlparse, unquote
+
+from PIL import Image, ImageDraw
+
+
+def local_path_from_uri(arg):
+    """Normally receive file:// anyway so this is more portable"""
+    if arg.startswith("file:"):
+        parsed = urlparse(arg)
+        return unquote(parsed.path or "")
+    return arg
+
+
+DEFAULT_THUMB = 512
+
+opts, par = getopt.getopt(sys.argv[1:], "s:", ["size="])
+thumb = DEFAULT_THUMB
+for o, a in opts:
+    if o in ("-s", "--size"):
+        try:
+            thumb = int(a)
+        except ValueError:
+            print("error: -s/--size must be an integer", file=sys.stderr)
+            sys.exit(2)
+        if thumb < 1:
+            print("error: size must be at least 1", file=sys.stderr)
+            sys.exit(2)
+
+if not par:
+    print(
+        "usage: dxf-thumbnailer [-s N|--size=N] <input.dxf> [output.png]",
+        file=sys.stderr,
+    )
+    sys.exit(2)
+inpfile = local_path_from_uri(par[0])
+if len(par) >= 2:
+    outfile = par[1]
+else:
+    root, _ = os.path.splitext(inpfile)
+    outfile = root + ".png"
 
 try:
     objects = []
@@ -156,49 +192,49 @@
                     if num > ymax: ymax = num
                 store = None
 
-    print len(objects),"objects"
-    print "bounding box",xmin,ymin,xmax,ymax
+    print(len(objects), "objects")
+    print("bounding box", xmin, ymin, xmax, ymax)
     
     sizex = xmax-xmin
     sizey = ymax-ymin
-    image = Image.new('RGB',(128,128))
+    image = Image.new("RGB", (thumb, thumb))
     draw = ImageDraw.Draw(image)
 
-    draw.rectangle((0,0,128,128),fill='#ffffff')
+    draw.rectangle((0, 0, thumb, thumb), fill="#ffffff")
     
     for ob in objects:
-        print "drawing",ob
+        print("drawing", ob)
         if ob[0] == 'line':
-            x1 = int(((ob[1]-xmin)/sizex)*128)
-            y1 = 128-int(((ob[2]-ymin)/sizey)*128)
-            x2 = int(((ob[3]-xmin)/sizex)*128)
-            y2 = 128-int(((ob[4]-ymin)/sizey)*128)
-            draw.line((x1,y1,x2,y2),fill=0)
+            x1 = int(((ob[1] - xmin) / sizex) * thumb)
+            y1 = thumb - int(((ob[2] - ymin) / sizey) * thumb)
+            x2 = int(((ob[3] - xmin) / sizex) * thumb)
+            y2 = thumb - int(((ob[4] - ymin) / sizey) * thumb)
+            draw.line((x1, y1, x2, y2), fill=0)
         elif ob[0] == 'polyline':
-            x1 = int(((ob[1]-xmin)/sizex)*128)
-            y1 = 128-int(((ob[2]-ymin)/sizey)*128)
+            x1 = int(((ob[1] - xmin) / sizex) * thumb)
+            y1 = thumb - int(((ob[2] - ymin) / sizey) * thumb)
             done = False
             for p in ob[3:]:
                 if not done:
                     lp = p
                 else:
-                    x2 = int(((lp-xmin)/sizex)*128)
-                    y2 = 128-int(((p-ymin)/sizey)*128)
-                    draw.line((x1,y1,x2,y2),fill=0)
+                    x2 = int(((lp - xmin) / sizex) * thumb)
+                    y2 = thumb - int(((p - ymin) / sizey) * thumb)
+                    draw.line((x1, y1, x2, y2), fill=0)
                     x1 = x2
                     y1 = y2
                 done = not(done)
         elif ob[0] == 'circle':
-            x1 = int((((ob[1]-ob[3])-xmin)/sizex)*128)
-            y1 = 128-int((((ob[2]-ob[3])-ymin)/sizey)*128)
-            x2 = int((((ob[1]+ob[3])-xmin)/sizex)*128)
-            y2 = 128-int((((ob[2]+ob[3])-ymin)/sizey)*128)
-            draw.ellipse((x1,y2,x2,y1),outline=0)
+            x1 = int((((ob[1] - ob[3]) - xmin) / sizex) * thumb)
+            y1 = thumb - int((((ob[2] - ob[3]) - ymin) / sizey) * thumb)
+            x2 = int((((ob[1] + ob[3]) - xmin) / sizex) * thumb)
+            y2 = thumb - int((((ob[2] + ob[3]) - ymin) / sizey) * thumb)
+            draw.ellipse((x1, y2, x2, y1), outline=0)
         elif ob[0] == 'arc':
-            x1 = int((((ob[1]-ob[3])-xmin)/sizex)*128)
-            y1 = 128-int((((ob[2]-ob[3])-ymin)/sizey)*128)
-            x2 = int((((ob[1]+ob[3])-xmin)/sizex)*128)
-            y2 = 128-int((((ob[2]+ob[3])-ymin)/sizey)*128)
+            x1 = int((((ob[1] - ob[3]) - xmin) / sizex) * thumb)
+            y1 = thumb - int((((ob[2] - ob[3]) - ymin) / sizey) * thumb)
+            x2 = int((((ob[1] + ob[3]) - xmin) / sizex) * thumb)
+            y2 = thumb - int((((ob[2] + ob[3]) - ymin) / sizey) * thumb)
             start = int(ob[4])
             end = int(ob[5])
             draw.arc((x1,y2,x2,y1),start,end,fill=0)