Browse code

slicer.pike: add transparency support

Dario Rodriguez authored on 27/01/2024 14:33:22
Showing 1 changed files
... ...
@@ -6,6 +6,7 @@
6 6
  *
7 7
  * History:
8 8
  *      28/06/2019 Creation
9
+ *      27/01/2024 Support transparency
9 10
  *
10 11
  * Author: Dario Rodriguez dario@softhome.net
11 12
  * This program is distributed under the terms of the GNU GPL v2.1+
... ...
@@ -19,7 +20,9 @@ main(int argc, array(string) argv)
19 20
         string origfilename;
20 21
         string resprefix;
21 22
         int x,y,total;
22
-        Image.Image img,cropped;
23
+        mapping imagemapping;
24
+        Image.Image image,alpha,cropped,croppedalpha;
25
+        Image.Layer l;
23 26
         string outfilename;
24 27
         if(argc<4 || (argc>1 && argv[1]=="--help")) {
25 28
                 Stdio.werror("Syntax:  "+argv[0]+" <width_in_a4pages> <height_in_a4_pages> <origimage.png>\n");
... ...
@@ -32,16 +35,21 @@ main(int argc, array(string) argv)
32 35
         origfilename=argv[3];
33 36
         resprefix=(origfilename/".png")[0];
34 37
         write("* Loading "+origfilename+"...\n");
35
-        img=Image.load(origfilename);
36
-        px=img->xsize()/a4w;
37
-        py=img->ysize()/a4h;
38
+        imagemapping=Image._load(origfilename);
39
+        image=imagemapping["image"];
40
+        alpha=imagemapping["alpha"];
41
+        px=image->xsize()/a4w;
42
+        py=image->ysize()/a4h;
38 43
         for(total=0,y=0;y<a4h;y++) {
39 44
                 for(x=0;x<a4w;x++) {
40 45
                         total++;
41
-                        cropped=img->copy(x*px,y*py,(x+1)*px-1,(y+1)*py-1,255,255,255);
46
+                        cropped=image->copy(x*px,y*py,(x+1)*px-1,(y+1)*py-1,255,255,255);
47
+                        croppedalpha=alpha->copy(x*px,y*py,(x+1)*px-1,(y+1)*py-1,255,255,255);
48
+                        l=Image.Layer(cropped,croppedalpha,"normal");
49
+                        l->set_offset(0,0);
42 50
                         outfilename=sprintf("%s_%05d.png",resprefix,total);
43 51
                         write("* Writing "+outfilename+"...\n");
44
-                        Stdio.write_file(outfilename,Image.PNG.encode(cropped));
52
+                        Stdio.write_file(outfilename,Image.PNG.encode(l->image(),(["alpha":l->alpha()])));
45 53
                 }
46 54
         }
47 55
         write(""+total+" slices written. Process finished.\n");
Browse code

Initial commit

Dario Rodriguez authored on 28/10/2020 22:49:01
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,50 @@
1
+#!/usr/bin/pike
2
+/*
3
+ * slicer.pike
4
+ *
5
+ * Slices a png in NxM different pngs
6
+ *
7
+ * History:
8
+ *      28/06/2019 Creation
9
+ *
10
+ * Author: Dario Rodriguez dario@softhome.net
11
+ * This program is distributed under the terms of the GNU GPL v2.1+
12
+ */
13
+
14
+int
15
+main(int argc, array(string) argv)
16
+{
17
+        int a4w,a4h;
18
+        int px,py;
19
+        string origfilename;
20
+        string resprefix;
21
+        int x,y,total;
22
+        Image.Image img,cropped;
23
+        string outfilename;
24
+        if(argc<4 || (argc>1 && argv[1]=="--help")) {
25
+                Stdio.werror("Syntax:  "+argv[0]+" <width_in_a4pages> <height_in_a4_pages> <origimage.png>\n");
26
+                Stdio.werror("Example: "+argv[0]+" 4 2 orig.png\n");
27
+                Stdio.werror("Example result: orig_0001.png ... orig_0008.png\n");
28
+                return(1);
29
+        }
30
+        a4w=(int)argv[1];
31
+        a4h=(int)argv[2];
32
+        origfilename=argv[3];
33
+        resprefix=(origfilename/".png")[0];
34
+        write("* Loading "+origfilename+"...\n");
35
+        img=Image.load(origfilename);
36
+        px=img->xsize()/a4w;
37
+        py=img->ysize()/a4h;
38
+        for(total=0,y=0;y<a4h;y++) {
39
+                for(x=0;x<a4w;x++) {
40
+                        total++;
41
+                        cropped=img->copy(x*px,y*py,(x+1)*px-1,(y+1)*py-1,255,255,255);
42
+                        outfilename=sprintf("%s_%05d.png",resprefix,total);
43
+                        write("* Writing "+outfilename+"...\n");
44
+                        Stdio.write_file(outfilename,Image.PNG.encode(cropped));
45
+                }
46
+        }
47
+        write(""+total+" slices written. Process finished.\n");
48
+        return(0);
49
+}
50
+