-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktopwallpaper.py
executable file
·6421 lines (6133 loc) · 232 KB
/
desktopwallpaper.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This Python script helps generate interesting variations on desktop
# wallpapers based on existing image files. Because they run on the CPU
# and are implemented in pure Python, the methods are intended for
# relatively small images that are suitable as tileable desktop wallpaper
# patterns, especially with dimensions 256 × 256 or smaller.
#
# This script is released to the public domain; in case that is not possible, the
# file is also licensed under the Unlicense: https://unlicense.org/
#
# NOTES:
#
# 1. Animation of tilings composed from a wallpaper image can be implemented by
# shifting, with each frame, the starting position for drawing the upper left
# corner of the wallpaper tiling (for example, from the upper-left corner of the image
# to some other position in the image).
# 2. In Windows, if both an 8 × 8 two-level pattern and a centered wallpaper
# are set as the desktop background, a tiling of the pattern and the wallpaper
# will be drawn on the desktop, the latter appearing above the former. Areas of the
# two-level pattern where the pixel is 1 are drawn as "black", and other areas are
# drawn as the desktop color.
# 3. I would welcome it if readers could contribute computer code (released
# to the public domain or under the Unlicense) to generate tileable—
# - noise,
# - procedural textures or patterns, or
# - arrangements of symbols or small images with partial transparency,
# without artificial intelligence, with a limited color palette and a small
# resolution, as long as the resulting images do not employ
# trademarks and are suitable for all ages. For details on the color and
# resolution options as well as a broader challenge to generate tileable
# classic wallpapers, see:
#
# https://github.com/peteroupc/classic-wallpaper
#
import os
import math
import random
import struct
import sys
def _listdir(p):
return [os.path.abspath(p + "/" + x) for x in os.listdir(p)]
_DitherMatrix4x4 = [ # Bayer 4 × 4 ordered dither matrix
0,
8,
2,
10,
12,
4,
14,
6,
3,
11,
1,
9,
15,
7,
13,
5,
]
_DitherMatrix = [ # Bayer 8 × 8 ordered dither matrix
0,
32,
8,
40,
2,
34,
10,
42,
48,
16,
56,
24,
50,
18,
58,
26,
12,
44,
4,
36,
14,
46,
6,
38,
60,
28,
52,
20,
62,
30,
54,
22,
3,
35,
11,
43,
1,
33,
9,
41,
51,
19,
59,
27,
49,
17,
57,
25,
15,
47,
7,
39,
13,
45,
5,
37,
63,
31,
55,
23,
61,
29,
53,
21,
]
# Returns an array of the 216 colors of the "safety palette", also known as the
# "Web safe" palette. The "safety palette" consists of 216 colors that are
# uniformly spaced in the red–green–blue color cube. Robert Hess's
# article "[The Safety Palette](https://learn.microsoft.com/en-us/previous-versions/ms976419(v=msdn.10))",
# 1996, described the advantage that images that use only colors in this palette
# won't dither when displayed by Web browsers on displays that can show up to 256
# colors at once. (See also [**Wikipedia**](http://en.wikipedia.org/wiki/Web_colors).
# Dithering is the scattering of colors in a limited set to simulate colors
# outside that set.)
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def websafecolors():
colors = []
for r in range(6):
for g in range(6):
for b in range(6):
colors.append([r * 51, g * 51, b * 51])
return colors
# Returns an array of the 64 colors displayable by EGA (extended graphics adapter) displays
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def egacolors():
colors = []
for r in range(4):
for g in range(4):
for b in range(4):
colors.append([r * 85, g * 85, b * 85])
return colors
# Canonical 16-color CGA palette
# see also: https://int10h.org/blog/2022/06/ibm-5153-color-true-cga-palette/
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def cgacolors():
return [
[0, 0, 0],
[0, 0, 170],
[0, 170, 0],
[0, 170, 170],
[170, 0, 0],
[170, 0, 170],
[170, 85, 0], # [170, 170, 0] is another variant, given
# that exact color values for CGA's 16 colors
# are unstandardized beyond the notion of 'RGBI'.
[170, 170, 170],
[85, 85, 85],
[85, 85, 255],
[85, 255, 85],
[85, 255, 255],
[255, 85, 85],
[255, 85, 255],
[255, 255, 85],
[255, 255, 255],
]
# 16-color VGA palette
def classiccolors():
return [
[0, 0, 0],
[128, 128, 128],
[192, 192, 192],
[255, 0, 0],
[128, 0, 0],
[0, 255, 0],
[0, 128, 0],
[0, 0, 255],
[0, 0, 128],
[255, 0, 255],
[128, 0, 128],
[0, 255, 255],
[0, 128, 128],
[255, 255, 0],
[128, 128, 0],
[255, 255, 255],
]
# 8-color palette where each color opponent is 0 or 255
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def ega8colors():
return [
[0, 0, 0],
[255, 0, 0],
[0, 255, 0],
[0, 0, 255],
[255, 0, 255],
[0, 255, 255],
[255, 255, 0],
[255, 255, 255],
]
# Colors in classiccolors() and their "half-and-half" versions.
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def classiccolors2():
colors = []
for a in [0, 64, 128, 192]:
for b in [0, 64, 128, 192]:
for c in [0, 64, 128, 192]:
cij = [a, b, c]
if cij not in colors:
colors.append(cij)
for a in [0, 128, 255]:
for b in [0, 128, 255]:
for c in [0, 128, 255]:
cij = [a, b, c]
if cij not in colors:
colors.append(cij)
for a in [96, 160]:
for b in [96, 160]:
for c in [96, 160]:
cij = [a, b, c]
if cij not in colors:
colors.append(cij)
for a in [96, 224]:
for b in [96, 224]:
for c in [96, 224]:
cij = [a, b, c]
if cij not in colors:
colors.append(cij)
return colors
# Returns an array containing the colors in the specified palette plus their
# "half-and half" versions.
# Each element in the return value is a color in the form of a 3-element array of its red,
# green, and blue components in that order, where each
# component is an integer from 0 through 255.
def paletteandhalfhalf(palette):
ret = [
[k & 0xFF, (k >> 8) & 0xFF, (k >> 16) & 0xFF]
for k in _getdithercolors(palette).keys()
]
ret.sort()
return ret
# Gets the "half-and half" versions of colors in the specified palette.
def _getdithercolors(palette):
if (not palette) or (len(palette) > 256): # too long palettes not supported
raise ValueError
colors = {}
for c in palette:
cij = c[0] | (c[1] << 8) | (c[2] << 16)
if cij not in colors:
colors[cij] = [cij, cij]
for i in range(len(palette)):
for j in range(i + 1, len(palette)):
ci = palette[i]
cj = palette[j]
ci1 = ci[0] | (ci[1] << 8) | (ci[2] << 16)
cj1 = cj[0] | (cj[1] << 8) | (cj[2] << 16)
cij = (
((ci[0] + cj[0] + 1) // 2)
| (((ci[1] + cj[1] + 1) // 2) << 8)
| (((ci[2] + cj[2] + 1) // 2) << 16)
)
if cij not in colors:
colors[cij] = [ci1, cj1]
return colors
def halfhalfditherimage(image, width, height, palette):
if width <= 0 or height <= 0 or not palette:
raise ValueError
cdcolors = _getdithercolors(palette)
for y in range(height):
yd = y * width
for x in range(width):
xp = yd + x
col = image[xp * 3] | (image[xp * 3 + 1] << 8) | (image[xp * 3 + 2] << 16)
cd = cdcolors[col]
if not cd:
raise ValueError
col = cd[0] if (x + y) % 2 == 0 else cd[1]
image[xp * 3] = col & 0xFF
image[xp * 3 + 1] = (col >> 8) & 0xFF
image[xp * 3 + 2] = (col >> 16) & 0xFF
# Returns a list of the unique colors in an image (disregarding
# the alpha channel, if any). The return value has the same
# format returned in the _reados2palette_ function.
def uniquecolors(image, width, height, alpha=False):
colors = {}
bytesperpixel = 4 if alpha else 3
for i in range(width * height):
c = (
image[i * bytesperpixel]
| (image[i * bytesperpixel + 1] << 8)
| (image[i * bytesperpixel + 2] << 16)
)
colors[c] = True
ck = [[k & 0xFF, (k >> 8) & 0xFF, (k >> 16) & 0xFF] for k in colors.keys()]
ck.sort()
return ck
def _isqrtceil(i):
r = math.isqrt(i)
return r if r * r == i else r + 1
# Returns an ImageMagick filter string to generate a desktop background from an image, in three steps.
# 1. If rgb1 and rgb2 are not nil, converts the input image to grayscale, then translates the grayscale
# palette to a gradient starting at rgb1 for grayscale level 0 (a 3-element array of the red,
# green, and blue components in that order; for example, [2,10,255] where each
# component is from 0 through 255) and ending at rgb2 for grayscale level 255 (same format as rgb1).
# Raises an error if rgb1 or rgb2 has a length less than 3.
# The output image is the input for the next step.
# 2. If hue is not 0, performs a hue shift, in degrees (-180 to 180), of the input image.
# The output image is the input for the next step.
# 3. If basecolors is not nil, ensures that that the image used only the colors
# given in 'basecolors', which is a list
# of colors (each color is of the same format as rgb1 and rgb2). If 'dither' is also True, the
# image's colors are then scattered so that they appear close to the original colors.
# Raises an error if 'basecolors' has a length greater than 256.
def magickgradientditherfilter(
rgb1=None, rgb2=None, basecolors=None, hue=0, dither=True
):
if hue < -180 or hue > 180:
raise ValueError
if rgb1 and len(rgb1) < 3:
raise ValueError
if rgb2 and len(rgb2) < 3:
raise ValueError
if basecolors:
if len(basecolors) > 256:
raise ValueError
for k in basecolors:
if (not k) or len(k) < 3:
raise ValueError
ret = []
huemod = (hue + 180) * 100.0 / 180.0
if rgb1 != None and rgb2 != None:
r1 = "#%02x%02x%02x" % (int(rgb1[0]), int(rgb1[1]), int(rgb1[2]))
r2 = "#%02x%02x%02x" % (int(rgb2[0]), int(rgb2[1]), int(rgb2[2]))
sz = 256
ret += [
"(",
"+clone",
"-grayscale",
"Rec709Luma",
")",
"(",
"-size",
"1x%d" % (sz),
"gradient:%s-%s" % (r1, r2),
")",
"-delete",
"0",
"-clut",
]
if hue != 0:
ret += ["-modulate", "100,100,%.02f" % (huemod)]
if basecolors and len(basecolors) > 0:
bases = ["xc:#%02X%02X%02X" % (k[0], k[1], k[2]) for k in basecolors]
# ImageMagick command to generate the palette image
ret += (
["(", "-size", "1x1"]
+ bases
+ ["+append", "-write", "mpr:z", "+delete", ")"]
)
# Apply Floyd-Steinberg error diffusion dither.
# NOTE: For abstractImage = True, ImageMagick's ordered 8 × 8 dithering
# algorithm ("-ordered-dither 8x8") is by default a per-channel monochrome
# (2-level) dither, not a true color dithering approach that takes much
# account of the color palette.
# As a result, for example, dithering a grayscale image with the algorithm will
# lead to an image with only black and white pixels, even if the palette contains,
# say, ten shades of gray. The number after "8x8" is the number of color levels
# per color channel in the ordered dither algorithm, and this number is taken
# as the square root of the palette size, rounded up, minus 1, but not less
# than 2.
# ditherkind = (
# "-ordered-dither 8x8,%d" % (min(2, _isqrtceil(len(basecolors)) - 1))
# if abstractImage
# else "-dither FloydSteinberg"
# )
# "+dither" disables dithering
if dither:
ret += ["-dither", "FloydSteinberg"]
else:
ret += ["+dither"]
ret += ["-remap", "mpr:z"]
return ret
# ImageMagick command for clearing an image with a solid color.
def solid(bg=[192, 192, 192]):
if bg == None or len(bg) < 3:
raise ValueError
bc = "#%02x%02x%02x" % (int(bg[0]), int(bg[1]), int(bg[2]))
# Fred Weinhaus suggested the following to me, which avoids
# having to know the input image size in advance:
# https://github.com/ImageMagick/ImageMagick/discussions/7423
# return ["(", "+clone", "-fill", "xc:" + bc, "-colorize", "100", ")"]
# another solution that works better with alpha channel images
return ["(", "+clone", "-size", "%wx%h", "xc:" + bc, "-delete", "-2", ")"]
# ImageMagick command.
def hautrelief(bg=[192, 192, 192], highlight=[255, 255, 255], shadow=[0, 0, 0]):
if bg == None or len(bg) < 3:
raise ValueError
if highlight == None or len(highlight) < 3:
raise ValueError
if shadow == None or len(shadow) < 3:
raise ValueError
bc = "#%02x%02x%02x" % (int(bg[0]), int(bg[1]), int(bg[2]))
hc = "#%02x%02x%02x" % (int(highlight[0]), int(highlight[1]), int(highlight[2]))
sc = "#%02x%02x%02x" % (int(shadow[0]), int(shadow[1]), int(shadow[2]))
return (
" -grayscale Rec709Luma -channel RGB -threshold 51%% +channel -write mpr:z "
+ '\\( -clone 0 -morphology Convolve "3:0,0,0 0,0,0 0,0,1" -write mpr:z1 \\) '
+ '\\( -clone 0 -morphology Convolve "3:1,0,0 0,0,0 0,0,0" -write mpr:z2 \\) -delete 0 '
+ "-compose Multiply -composite "
+ "\\( mpr:z1 mpr:z2 -compose Screen -composite -negate \\) -compose Plus -composite "
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut -write mpr:a1 -delete 0 "
+ 'mpr:z1 \\( mpr:z -negate -morphology Convolve "3:1,0,0 0,0,0 0,0,0" \\) -compose Multiply -composite '
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut -write mpr:a2 -delete 0 "
+ '\\( mpr:z -negate -morphology Convolve "3:0,0,0 0,0,0 0,0,1" \\) mpr:z2 -compose Multiply -composite '
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut mpr:a2 -compose Plus -composite mpr:a1 -compose Plus -composite "
) % (bc, hc, sc)
# ImageMagick command.
def shiftwrap(xOrigin, yOrigin):
return [
"(",
"+clone",
")",
"-append",
"(",
"+clone",
")",
"+append",
"-crop",
"50%%x50%%%s%d%s%d"
% ("+" if xOrigin >= 0 else "", xOrigin, "+" if yOrigin >= 0 else "", yOrigin),
]
# ImageMagick command to render an input image described in 'versatilePattern' in an unavailable appearance.
# If 'buttonShadow' is darker than 'buttonHighlight' (as is the default), then this method will result in
# the image's appearing engraved, that is, sunken onto the background, given the existence of a light
# source that shines from the upper-left corner.
# If 'buttonShadow' is lighter than 'buttonHighlight', the image instead appears embossed, that is, raised above the
# background, given the light source just described.
# If 'drawShiftedImageOver' is True, the image drawn in the 'buttonShadow' color is drawn above the
# image drawn in the 'buttonHighlight' color.
def unavailable(
bgColor=None, buttonShadow=None, buttonHighlight=None, drawShiftedImageOver=False
):
if not bgColor:
bgColor = [192, 192, 192]
if not buttonShadow:
buttonShadow = [128, 128, 128]
if not buttonHighlight:
buttonHighlight = [255, 255, 255]
mpre = "mpr:engrave"
return (
["-grayscale", "Rec709Luma", "-write", mpre, "-delete", "0", "(", mpre]
+ versatilePattern(buttonHighlight, None)
+ [")", "(", mpre]
+ versatilePattern(buttonShadow, None)
+ shiftwrap(1, 1)
+ [
")",
"-alpha",
"on",
"-compose",
"DstOver" if drawShiftedImageOver else "Over",
"-composite",
]
+ backgroundColorUnder(bgColor)
)
# ImageMagick command to emboss an input image described in 'versatilePattern' into a 3-color (black/gray/white) image.
# If 'fgColor' is lighter than 'hiltColor' (as is the default), then embossing an outline will result in its
# appearing raised above the background, given the existence of a light source that shines from the upper
# lower-left corner.
# If 'fgColor' is darker than 'hiltColor', the outline instead appears engraved, that is, sunken into the
# background, given the light source just described.
# In this description, lower-intensity values are generally "darker", higher-intensity values "lighter".
def emboss(bgColor=None, fgColor=None, hiltColor=None):
return unavailable(
bgColor if bgColor else [128, 128, 128],
hiltColor if hiltColor else [255, 255, 255],
fgColor if fgColor else [0, 0, 0],
True,
)
# ImageMagick command.
def versatileForeground(foregroundImage):
return [
"-negate",
"-write",
"mpr:vfg",
"-delete",
"-1",
"tile:" + foregroundImage,
"mpr:vfg",
"-alpha",
"Off",
"-compose",
"copyopacity",
"-composite",
]
# ImageMagick command for setting a foreground pattern, whose black parts
# are set in the specified foreground color, on a background that can optionally
# be colored.
# 'fgcolor' and 'bgcolor' are the foreground and background color, respectively.
# The input image this command will be applied to is assumed to be an SVG file
# which must be black (all zeros) in the nontransparent areas (given that ImageMagick renders the
# SVG, by default, on a background colored white, or (255,255,255)) or a raster image with only
# gray tones, where the closer the gray level is to 0, the less transparent.
# 'bgcolor' can be None so that an alpha
# background is used. Each color is a
# 3-element array of the red, green, and blue components in that order; for example,
# [2,10,255] where each component is from 0 through 255.
# Inspired by the technique for generating backgrounds in heropatterns.com.
def versatilePattern(fgcolor, bgcolor=None):
return [
"-negate",
"-background",
"#%02x%02x%02x" % (int(fgcolor[0]), int(fgcolor[1]), int(fgcolor[2])),
"-alpha",
"shape",
] + backgroundColorUnder(bgcolor)
# ImageMagick command for setting a light gray (192,192,192) foreground pattern on a white (255,255,255) background.
def lightmodePattern():
return versatilePattern([192, 192, 192], [255, 255, 255])
# ImageMagick command for setting a gray (128,128,128) foreground pattern on a black (0,0,0) background.
def darkmodePattern():
return versatilePattern([128, 128, 128], [0, 0, 0])
# ImageMagick command.
# bg is treated as [192, 192, 192] if None.
# highlight is treated as [255, 255, 255] if None.
# shadow is treated as [0, 0, 0] if None.
def basrelief(bg=None, highlight=None, shadow=None):
if bg == None:
bg = [192, 192, 192]
if len(bg) < 3:
raise ValueError
if highlight == None:
highlight = [255, 255, 255]
if len(highlight) < 3:
raise ValueError
if shadow == None:
shadow = [0, 0, 0]
if len(shadow) < 3:
raise ValueError
bc = "#%02x%02x%02x" % (int(bg[0]), int(bg[1]), int(bg[2]))
hc = "#%02x%02x%02x" % (int(highlight[0]), int(highlight[1]), int(highlight[2]))
sc = "#%02x%02x%02x" % (int(shadow[0]), int(shadow[1]), int(shadow[2]))
return (
" -grayscale Rec709Luma -channel RGB -threshold 51%% -write mpr:z "
+ '\\( -clone 0 -morphology Convolve "3:0,0,0 0,0,0 0,0,1" -write mpr:z1 \\) '
+ '\\( -clone 0 -morphology Convolve "3:1,0,0 0,0,0 0,0,0" -write mpr:z2 \\) -delete 0--1 '
+ "mpr:z2 \\( mpr:z -negate \\) -compose Multiply -composite -write mpr:a10 "
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut -write mpr:a2 -delete 0 "
+ "\\( mpr:z -negate \\) mpr:z1 -compose Multiply -composite -write mpr:a20 "
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut -write mpr:a1 -delete 0 "
+ "mpr:a10 mpr:a20 -compose Plus -composite -negate "
+ "\\( -size 1x1 xc:black xc:%s +append \\) -clut mpr:a2 -compose Plus -composite "
+ "mpr:a1 -compose Plus -composite "
) % (sc, hc, bc)
# ImageMagick command.
def magickgradientditherfilterrandom():
rgb1 = None
rgb2 = None
hue = 0
basecolors = None
while rgb1 == None and rgb2 == None and hue == 0 and basecolors == None:
if random.randint(0, 99) == 1:
return magickgradientditherfilter()
rgb1 = None
rgb2 = None
hue = 0
basecolors = None
if random.randint(0, 9) < 6:
rgb1 = [0, 0, 0]
rgb2 = [
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
]
if random.randint(0, 9) < 3:
hue = random.randint(0, 50) - 180
r = random.randint(0, 9)
if r < 3:
basecolors = classiccolors()
elif r < 6:
basecolors = websafecolors()
elif r < 8 and rgb1 != None:
basecolors = [rgb1, rgb2]
return magickgradientditherfilter(rgb1, rgb2, basecolors, hue=hue)
def _chopBeforeHAppendArray(withFarEnd=True):
if withFarEnd:
# Remove the left and right column
return [
"+repage", # If absent, chop might fail
"-gravity",
"West",
"-chop",
"1x0",
"-gravity",
"East",
"-chop",
"1x0",
"+gravity",
]
# Remove the left column
return ["+repage", "-gravity", "West", "-chop", "1x0", "+gravity"]
def _chopBeforeVAppendArray(withFarEnd=True):
if withFarEnd:
# Remove the top and bottom row
return [
"+repage", # If absent, chop might fail
"-gravity",
"North",
"-chop",
"0x1",
"-gravity",
"South",
"-chop",
"0x1",
"+gravity",
]
# Remove the top row
return ["+repage", "-gravity", "North", "-chop", "0x1", "+gravity"]
# ImageMagick command to generate a Pmm wallpaper group tiling pattern.
# This command can be applied to arbitrary images to render them
# seamlessly tileable.
# NOTE: "-append" is a vertical append; "+append" is a horizontal append;
# "-flip" reverses the row order; "-flop" reverses the column order.
# NOTE: Of the seventeen wallpaper groups, four can be
# applied to areas with arbitrary contents to create seamlessly tileable images:
# Pmm (1/4 of a rectangle is reflected and repeated).
# P4m (1/8 of a rectangle).
# P3m1 (1/6 of a hexagon).
# P6m (1/12 of a hexagon).
def tileable():
return (
["(", "+clone", "-flip"]
+ _chopBeforeVAppendArray()
+ [")", "-append", "(", "+clone", "-flop"]
+ _chopBeforeHAppendArray()
+ [")", "+append"]
)
# ImageMagick command to generate a P2 wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last row's first half is a mirror of its second half.
def groupP2():
return (
["(", "+clone", "-flip", "-flop"] + _chopBeforeVAppendArray() + [")", "-append"]
)
# ImageMagick command to generate a Pm wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last row's first half is a mirror of its second half.
def groupPm():
return ["(", "+clone", "-flop"] + _chopBeforeHAppendArray() + [")", "+append"]
# ImageMagick command to generate a Pg wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last column's first half is a mirror of its second half.
def groupPg():
return ["(", "+clone", "-flip"] + _chopBeforeVAppendArray() + [")", "-append"]
# ImageMagick command to generate a Pgg wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last row's and last column's first half is a mirror of its
# second half.
def groupPgg():
return (
[
"-write",
"mpr:wpgroup",
"-delete",
"0",
"(",
"mpr:wpgroup",
"(",
"mpr:wpgroup",
"-flip",
"-flop",
]
+ _chopBeforeHAppendArray()
+ [
")",
"+append",
")" "(",
"(",
"mpr:wpgroup",
"-flip",
"-flop",
")",
"(",
"mpr:wpgroup",
]
+ _chopBeforeHAppendArray()
+ [")", "+append"]
+ _chopBeforeVAppendArray()
+ [")", "-append"]
)
# ImageMagick command to generate a Cmm wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last row's and last column's first half is a mirror of its
# second half.
def groupCmm():
return (
[
"(",
"+clone",
"-flip",
")",
"-append",
"-write",
"mpr:wpgroup",
"-delete",
"0",
"(",
"mpr:wpgroup",
"(",
"mpr:wpgroup",
"-flop",
]
+ _chopBeforeHAppendArray()
+ [
")",
"+append",
")",
"(",
"(",
"mpr:wpgroup",
"-flop",
")",
"(",
"mpr:wpgroup",
]
+ _chopBeforeHAppendArray()
+ [")", "+append"]
+ _chopBeforeVAppendArray()
+ [")", "-append"]
)
# ImageMagick command to put a background color behind the input image.
# 'bgcolor' is the background color,
# either None or a 3-element array of the red,
# green, and blue components in that order; for example, [2,10,255] where each
# component is from 0 through 255; default is None, or no background color.
def backgroundColorUnder(bgcolor=None):
return (
solid(bgcolor)
+ [
"-compose",
"DstOver",
"-composite",
]
if bgcolor
else []
)
# ImageMagick command to generate a diamond tiling pattern (or a brick tiling
# pattern if the image the command is applied to has only its top half
# or its bottom half drawn). For best results, the command should be applied
# to images with an even width and height.
def diamondTiling():
ret = [
"(",
"+clone",
"(",
"+clone",
")",
"-append",
"(",
"+clone",
")",
"+append",
"-chop",
"25%x25%",
")",
"-compose",
"Over",
"-composite",
]
return ret
def _bottomPadding():
return [
"-background",
"transparent",
"+repage",
"-gravity",
"SouthEast",
"-splice",
"0x%h",
"+gravity",
"+repage",
]
def _rightPadding():
return [
"-background",
"transparent",
"+repage",
"-gravity",
"NorthEast",
"-splice",
"%wx0",
"+gravity",
"+repage",
]
def diamondTiledSize(width, height, kind):
if kind == 1:
return (width, height * 2)
if kind == 2:
return (width * 2, height)
return (width + (width // 2) * 2, height + (height // 2) * 2)
# kind=0: image drawn in middle and padded
# kind=1: brick drawn at top
# kind=2: brick drawn at left
def diamondTiled(bgcolor=None, kind=0):
return (
(
_bottomPadding()
if kind == 1
else (
_rightPadding()
if kind == 2
else [
"+repage",
"-bordercolor",
"transparent",
# Don't allow whatever '-compose'
# setting there is to leak into
# the '-border' option
"-compose",
"Over",
"-border",
"50%x50%",
"-bordercolor",
"white",
]
)
)
+ diamondTiling()
+ backgroundColorUnder(bgcolor)
)
# ImageMagick command to generate a Pmg wallpaper group tiling pattern.
# For best results, the command should be applied to images whose
# last column's first half is a mirror of its
# second half.
def groupPmg():
return (
[
"-write",
"mpr:wpgroup",
"-delete",
"0",
"(",
"mpr:wpgroup",
"(",
"mpr:wpgroup",
"-flip",
"-flop",
]
+ _chopBeforeHAppendArray()
+ [
")",
"+append",
")",
"(",
"(",
"mpr:wpgroup",
"-flip",
")",
"(",
"mpr:wpgroup",
"-flop",
]
+ _chopBeforeHAppendArray()
+ [")", "+append"]
+ _chopBeforeVAppendArray()
+ [")", "-append"]
)
# ImageMagick command to generate a brushed metal texture from a noise image.
# A brushed metal texture was featured in Mac OS X Panther and
# Tiger (10.3, 10.4) and other Apple products
# around the time of either operating system's release.
def brushedmetal():
sz = 50
return [
"(",
"+clone",
")",
"+append",
"-morphology",
"Convolve",
("%dx1+%d+0:" % (sz, sz - 1)) + (",".join([str(1 / sz) for i in range(sz)])),
"+repage",
"-crop",
"50%x0+0+0",
"+repage",
]
def simplebox(image, width, height, color, x0, y0, x1, y1, wraparound=True):
borderedbox(
image, width, height, None, color, color, x0, y0, x1, y1, wraparound=wraparound
)
# Draw a wraparound hatched box on an image.
# Image has the same format returned by the blankimage() method with alpha=False.
# 'color' is the color of the hatch, drawn on every "black" pixel (defined below)
# in the pattern's tiling.
# 'pattern' is an 8-element array with integers in the interval [0,255].
# The first integer represents the first row from the top;
# the second, the second row, etc.
# For each integer, the eight bits from most to least significant represent
# the column from left to right (right to left if 'msbfirst' is False).
# If a bit is set, the corresponding position
# in the pattern is a "black" pixel; if clear, a "white" pixel.
# Either can be set to None to omit pixels of that color in the pattern
# 'msbfirst' is the bit order for each integer in 'pattern'. If True,
# the Windows convention is used; if False, the X pixmap convention is used.
# Default is True.
# 'drawborder' means to draw the box's border with the hatch color;
# default is False.
def hatchedbox(
image,
width,
height,
color,
pattern,
x0,
y0,
x1,
y1,
msbfirst=True,
drawborder=False,
wraparound=True,
):
if x0 < 0 or y0 < 0 or x1 < x0 or y1 < y0:
raise ValueError
if width <= 0 or height <= 0:
raise ValueError
if (not color) or len(color) != 3:
raise ValueError
if x0 == x1 or y0 == y1:
return
cr = color[0] & 0xFF
cg = color[1] & 0xFF
cb = color[2] & 0xFF
if not wraparound:
x0 = max(x0, 0)
y0 = max(y0, 0)
x1 = min(x1, width)
y1 = min(y1, height)
if x0 >= x1 or y0 >= y1: