summaryrefslogtreecommitdiff
path: root/xcf2jpeg
blob: 56866bb79545561e7ea7257d35b5155bb631163d (plain) (blame)
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
#!/bin/bash
# Taken from: http://billauer.co.il/blog/2009/07/gimp-xcf-jpg-jpeg-convert-bash-script/

SELFNAME=$(basename $0)
SIZE=800
QUALITY=95
OVERWRITE=0
OUTPUT_DIR=
GIMP="gimp"

die()
{
	printf "%s\n" "$1" >&2
	exit 1
}

print_help()
{
	cat >&2 << EOF
Usage: ./$SELFNAME [OPTION]... [FILE]...

	Convert XCF images to JPEG.

Options:
	-s, --size     size in pixels of biggest size of image (default: $SIZE)
	-q, --quality  JPEG image quality in percents (0-100) (default: $QUALITY)
	-f, --force    force convert, even if result file exists
	-h, --help     print this help
	--output-dir   name of output directory where place files. By default files
	               placed in same directory with source.

Example:
	Convert all XCF files in directory 
		./$SELFNAME *.xcf

	Convert XCF file to 900px image
		./$SELFNAME -s 900 image.xcf
EOF
}

while [[ $1 = -* ]]
do
	case "$1" in
		-s|--size) 
			SIZE="$2"
			[[ "$SIZE" -gt 0 ]] || die "Error: Size not integer: $SIZE"
			shift
			;;
		-q|--quality) 
			QUALITY="$2"
			[[ "$QUALITY" -gt 0 && "$QUALITY" -le 100 ]] || die "Error: Quality not percent: $QUALITY"
			shift
			;;
		-h|--help)
			print_help
			exit
			;;
		-f|--force)
			OVERWRITE=1
			;;
		--output-dir)
			OUTPUT_DIR="$2"
			shift
			;;
		*) 
			die "Error: Unknown option: $1"
			;;
	esac
	shift
done

RATE_STEP=${RATE_STEP:=.7}

{
cat << EOF
(define (resize image size)
    (let* (
           (rate-step $RATE_STEP)
           (cur-width (car (gimp-image-width image)))
           (cur-height (car (gimp-image-height image)))
          )
          (while (> (max cur-width cur-height) $SIZE)
              (if (< (max (* cur-width rate-step) (* cur-height rate-step)) $SIZE)
                (set! rate-step (min (/ $SIZE cur-height) (/ $SIZE cur-width)))
                1
              )
              (set! cur-width (* cur-width rate-step))
              (set! cur-height (* cur-height rate-step))

              (gimp-image-scale image cur-width cur-height)
          )
    )
)

(define (convert-xcf-to-jpeg filename outfile)
  (let* (
         (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
         (drawable (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
         )

    (resize image $SIZE)

    (file-jpeg-save RUN-NONINTERACTIVE image drawable outfile outfile (/ $QUALITY 100) 0 0 0 " " 0 1 0 1)
    (gimp-image-delete image) ; ... or memory will explode
    )
  )
(gimp-message-set-handler 1) ; Message to standart output
EOF

for file
do
	FILENAME=$file
	OUT_FILENAME=${FILENAME%%.xcf}.jpg
	if [[ -n "$OUTPUT_DIR" ]]
	then
		OUT_FILENAME="$OUTPUT_DIR/$(basename "$OUT_FILENAME")"
	fi
	if [[ $OVERWRITE = 0 && -f "$OUT_FILENAME" ]]
	then
		printf "Warning: File $OUT_FILENAME exists, skipping.\n" >&2
		return
	fi
	echo "(convert-xcf-to-jpeg \"$FILENAME\" \"$OUT_FILENAME\")"
done

echo "(gimp-quit 0)"
} | "$GIMP" -i -b -