1#!/usr/bin/env python3 2# SPDX-License-Identifier: Apache-2.0 3# ----------------------------------------------------------------------------- 4# Copyright 2019-2020 Arm Limited 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); you may not 7# use this file except in compliance with the License. You may obtain a copy 8# of the License at: 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 15# License for the specific language governing permissions and limitations 16# under the License. 17# ----------------------------------------------------------------------------- 18""" 19The ``astc_test_image_dl`` utility provides a means to programatically download 20test images that are available online, avoiding the need to duplicate them in 21the git repository. 22""" 23 24 25import os 26import sys 27import urllib.request 28 29from PIL import Image 30 31 32TEST_IMAGE_DIR = os.path.join("Test", "Images") 33 34 35def download(testSet, index, srcUrl, dstPath): 36 """ 37 Download a single image. 38 39 Args: 40 testSet (str): The test set name. 41 index (int): The download index. 42 srcUrl (str): The download URL. 43 dstPath (str): The destination path. 44 """ 45 dirName = os.path.dirname(dstPath) 46 if not os.path.exists(dirName): 47 os.makedirs(dirName) 48 49 # Skip downloads if the file already exists 50 if not os.path.exists(dstPath): 51 print("%s image %u: Downloading" % (testSet, index)) 52 urllib.request.urlretrieve(srcUrl, dstPath) 53 else: 54 print("%s image %u: Skipping" % (testSet, index)) 55 56 57def make_landscape(imgPath): 58 """ 59 Make an image on disk landscape aspect (edit in place) 60 61 Args: 62 imgPath: The pth of the image on disk. 63 """ 64 img = Image.open(imgPath) 65 if img.size[0] < img.size[1]: 66 img = img.rotate(90, expand=True) 67 img.save(imgPath) 68 69 70def make_mixed_image(imgPathA, imgPathB, dstPath): 71 """ 72 Make image consisting of RGB from A's RGB, and alpha from B's luminance. 73 74 Args: 75 imgPathA: The path of input A on disk. 76 imgPathB: The path of input B on disk. 77 dstPath: The path of the destination. 78 """ 79 imgA = Image.open(imgPathA) 80 imgB = Image.open(imgPathB).convert("L") 81 82 imgA.putalpha(imgB) 83 84 dirs = os.path.dirname(dstPath) 85 if not os.path.exists(dirs): 86 os.makedirs(dirs) 87 88 imgA.save(dstPath) 89 90 91def make_montage(imageDir, dstPath): 92 """ 93 Make a single mosaic montage consisting of all of the Kodak images. 94 95 Args: 96 imgDir: The directory path of the Kodak images on disk. 97 dstPth: The file path of the resulting montage. 98 """ 99 cols = 6 100 rows = 4 101 102 width = 768 103 height = 512 104 105 images = os.listdir(imageDir) 106 images.sort() 107 108 montage = Image.new('RGB', (width * cols, height * rows)) 109 110 for i, src in enumerate(images): 111 im = Image.open(os.path.join(imageDir, src)) 112 col = i % cols 113 row = i // cols 114 montage.paste(im, (width * col, height * row)) 115 116 dirs = os.path.dirname(dstPath) 117 if not os.path.exists(dirs): 118 os.makedirs(dirs) 119 120 montage.save(dstPath) 121 122 123def retrieve_kodak_set(): 124 """ 125 Download the public domain Kodak image set. 126 127 To make test set mosaics easier to build we rotate images to make 128 everything landscape. 129 """ 130 testSet = "Kodak" 131 132 # Download the original RGB images 133 for i in range(1, 25): 134 fle = "ldr-rgb-kodak%02u.png" % i 135 dst = os.path.join(TEST_IMAGE_DIR, "Kodak", "LDR-RGB", fle) 136 src = "http://r0k.us/graphics/kodak/kodak/kodim%02u.png" % i 137 download(testSet, i, src, dst) 138 139 # Canonicalize image aspect 140 make_landscape(dst) 141 142 # Make some correlated alpha RGBA images 143 fle = "ldr-rgb-kodak%02u.png" # Expand later 144 pattern = os.path.join(TEST_IMAGE_DIR, "Kodak", "LDR-RGB", fle) 145 146 for i in (22, 23): 147 imgA = pattern % i 148 fle = "ldr-rgba-kodak%02u+ca.png" % i 149 dst = os.path.join(TEST_IMAGE_DIR, "KodakSim", "LDR-RGBA", fle) 150 make_mixed_image(imgA, imgA, dst) 151 152 # Make some non-correlated alpha RGBA images 153 for i, j in ((22, 24), (23, 20)): 154 imgA = pattern % i 155 imgB = pattern % j 156 fle = "ldr-rgba-kodak%02u+%02u+nca.png" % (i, j) 157 dst = os.path.join(TEST_IMAGE_DIR, "KodakSim", "LDR-RGBA", fle) 158 make_mixed_image(imgA, imgB, dst) 159 160 # Make a large montage 161 srcDir = os.path.join(TEST_IMAGE_DIR, "Kodak", "LDR-RGB") 162 fle = "ldr-rgb-montage.png" 163 dst = os.path.join(TEST_IMAGE_DIR, "KodakMnt", "LDR-RGB", fle) 164 make_montage(srcDir, dst) 165 166 167def main(): 168 """ 169 The main function. 170 171 Returns: 172 int: The process return code. 173 """ 174 retrieve_kodak_set() 175 return 0 176 177 178if __name__ == "__main__": 179 sys.exit(main()) 180