Skip to content

Commit

Permalink
Merge pull request #15 from dlcs/feature/handle_imagemodes
Browse files Browse the repository at this point in the history
Handle alternative image_modes
  • Loading branch information
donaldgray authored Feb 21, 2023
2 parents d46a138 + ad5459f commit e591734
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
34 changes: 18 additions & 16 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ on:
tags:
- "*"
pull_request:

jobs:
build-push:
runs-on: ubuntu-latest

steps:
- name: Extract branch name for push
run: echo "normalized_branch_name=$(stripped=${GITHUB_REF##*/} && echo ${stripped/\//-})" >> $GITHUB_ENV
if: github.event_name == 'push'

- name: Extract branch name for pull request
run: echo "normalized_branch_name=$(stripped=${PR_REF#refs/heads/} && echo ${stripped/\//-})" >> $GITHUB_ENV
if: github.event_name == 'pull_request'
env:
PR_REF: ${{ github.event.pull_request.head.ref }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
Expand All @@ -36,22 +27,33 @@ jobs:
- name: Check out code
id: checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/dlcs/appetiser
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,enable=true,prefix=,format=long
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: |
ghcr.io/dlcs/appetiser:${{ github.sha }}
ghcr.io/dlcs/appetiser:${{ env.normalized_branch_name }}
labels: ${{ steps.docker_meta.outputs.labels }}
tags: ${{ steps.docker_meta.outputs.tags }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7-slim-stretch
FROM python:3.10-slim-bullseye

ENV KAKADU_APPS_LOCATION s3://dlcs-dlcservices-bootstrap-objects/kdu77-apps.tar.gz
ENV APPETISER_DIR /opt/appetiser
Expand Down
44 changes: 43 additions & 1 deletion app/jp2/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
ImageCms,
)

from .kdu import image_modes

logger = logging.getLogger(__name__)

# Allows images of any size to be processed without raising a
Expand Down Expand Up @@ -47,6 +49,40 @@ def get_img_info(filepath: pathlib.Path) -> (pathlib.Path, dict):
return filepath, img_info


def _convert_tiff_mode(filepath: pathlib.Path, img: Image, img_info: dict) -> (pathlib.Path, dict):
"""
Image is already a TIFF but with a mode that we don't have KDU commands for
Convert it to RGB + ensure XResolution + YResolution tags are set
"""
x_resolution_tag = 282
y_resolution_tag = 283

x_res = img.tag_v2.get(x_resolution_tag, -1)
y_res = img.tag_v2.get(y_resolution_tag, -1)

img = _convert_img_colour_profile(img, img.filename)
tiff_filepath = filepath.parent / ('raw_' + filepath.name)

image_data = io.BytesIO()

# write data to BytesIO and read again to get as PIL.TiffImagePlugin.TiffImageFile
img.save(image_data, format='TIFF', compression=None)
with Image.open(image_data, formats=['TIFF']) as new_img:
if not new_img.tag_v2.get(x_resolution_tag, None):
logger.debug('%s: has no XResolution tag, setting from original %s', filepath, x_res)
new_img.tag_v2[x_resolution_tag] = x_res

if not new_img.tag_v2.get(y_resolution_tag, None):
logger.debug('%s: has no YResolution tag, setting from original %s', filepath, y_res)
new_img.tag_v2[y_resolution_tag] = y_res

logger.debug('%s: saving as raw to %s', filepath, tiff_filepath)
new_img.save(tiff_filepath, compression=None)
img_info['mode'] = new_img.mode

return tiff_filepath, img_info


def _uncompress_tiff(filepath: pathlib.Path) -> (pathlib.Path, dict):
""" Checks whether a tiff file has been saved with compression,
and if so, will save an uncompressed version under a new name.
Expand All @@ -63,6 +99,12 @@ def _uncompress_tiff(filepath: pathlib.Path) -> (pathlib.Path, dict):
logger.debug('%s: saving as raw to %s', filepath, tiff_filepath)
img.save(tiff_filepath, compression=None)
filepath = tiff_filepath

img_mode = img_info.get('mode')
if img_mode not in image_modes:
logger.debug('%s: has unknown image mode: %s', filepath, img_mode)
return _convert_tiff_mode(filepath, img, img_info)

return filepath, img_info


Expand Down Expand Up @@ -168,7 +210,7 @@ def prepare_source_file(filepath: pathlib.Path) -> (pathlib.Path, dict):
'.tif': _uncompress_tiff,
'.tiff': _uncompress_tiff,
}
f = img_file_funcs.get(filepath.suffix, _convert_img_to_tiff)
f = img_file_funcs.get(filepath.suffix.lower(), _convert_img_to_tiff)
logger.debug('%s() to be applied to %s', f.__name__, filepath)
return f(filepath)

Expand Down
15 changes: 8 additions & 7 deletions app/jp2/kdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
# warning or an error.
Image.MAX_IMAGE_PIXELS = None

image_modes = {
'L': '-no_palette',
'1': '-no_palette',
'I;16B': '-no_palette',
'RGB': '-jp2_space sRGB',
'RGBA': '-jp2_space sRGB -jp2_alpha'
}


def _run_kdu_command(kdu_command: str, env: dict):
try:
Expand All @@ -40,13 +48,6 @@ def kdu_compress(source_path: pathlib.Path, dest_path: pathlib.Path,
(in BMP, RAW, PBM, PGM, PPM or TIFF formats) to a JPEG2000.
"""

image_modes = {
'L': '-no_palette',
'1': '-no_palette',
'RGB': '-jp2_space sRGB',
'RGBA': '-jp2_space sRGB -jp2_alpha'
}

if image_mode not in image_modes:
logger.warning(f"image_mode '{image_mode}' is not in known list")

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
awscli==1.25.53
Flask==2.2.2
Pillow==9.2.0
Pillow==9.4.0
pytest==7.1.2
pytest-mock==3.8.2
uwsgi==2.0.20

0 comments on commit e591734

Please sign in to comment.