Download workflow/native_dataset_image.py from loffenauer/streetshots: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/datasets/loffenauer/streetshots/resolve/main/workflow/native_dataset_image.py
- Command line
-
hf download hf://datasets/loffenauer/streetshots/workflow/native_dataset_image.py
-
curl -L -o native_dataset_image.py https://huggingface.co/datasets/loffenauer/streetshots/resolve/main/workflow/native_dataset_image.py
1.8 kB
| """Exact source functions copied from crop_datasets.py. | |
| Source file SHA256: 2a4d69d5a146cc883e9bf4eefc76196846e5bf3aca9225ecaab3420344845aca | |
| No changes to raster/PNG behavior. | |
| """ | |
| import hashlib | |
| import io | |
| import struct | |
| import warnings | |
| from PIL import Image | |
| def sha(data): | |
| return hashlib.sha256(data).hexdigest() | |
| def native_image(data, *, jpeg_only=False, encode_png=True): | |
| """Hash the registered raster, without resizing, EXIF rotation or metadata.""" | |
| with warnings.catch_warnings(): | |
| warnings.simplefilter('error', Image.DecompressionBombWarning) | |
| with Image.open(io.BytesIO(data)) as original: | |
| if (jpeg_only and original.format != 'JPEG') or getattr(original, 'n_frames', 1) != 1: | |
| raise ValueError('dataset_image_format_invalid') | |
| if original.width * original.height > 16_000_000: | |
| raise ValueError('dataset_image_too_large') | |
| original.load() | |
| # Copy raw RGB channels to an image without info/EXIF/ICC metadata. | |
| mode = 'RGB' | |
| if 'A' in original.getbands() or 'transparency' in original.info: | |
| if original.convert('RGBA').getchannel('A').getextrema()[0] < 255: | |
| mode = 'RGBA' | |
| pixels = original.convert(mode) | |
| image = Image.frombytes(mode, pixels.size, pixels.tobytes()) | |
| prefix = b'youspeed-native-rgb-v1\0' if image.mode == 'RGB' else b'youspeed-native-rgba-v1\0' | |
| pixel_sha = sha(prefix + struct.pack('>II', *image.size) + image.tobytes()) | |
| identity = dict(pixel_sha256=pixel_sha, width=image.width, height=image.height) | |
| if not encode_png: | |
| return identity,None | |
| output = io.BytesIO() | |
| image.save(output, format='PNG', optimize=False, compress_level=6) | |
| return identity,output.getvalue() | |