<commit_before>#! /usr/bin/env python3
#
# Copyright (c) 2016, 2017, 2018 Forschungszentrum Juelich GmbH
# Author: <NAME> <<EMAIL>>
#
# This software is made available under the MIT licence, see LICENCE.txt.

import io

import numpy as np
import PIL.Image


def encode_chunk(chunk, jpeg_quality, jpeg_plane):
    assert 0 <= jpeg_quality <= 100
    assert jpeg_plane in ("xy", "xz")
    num_channels = chunk.shape[0]
    if jpeg_plane == "xy":
        reshaped_chunk = chunk.reshape(
            num_channels, chunk.shape[1] * chunk.shape[2], chunk.shape[3])
    elif jpeg_plane == "xz":
        reshaped_chunk = chunk.reshape(
            num_channels, chunk.shape[1], chunk.shape[2] * chunk.shape[3])

    if num_channels == 1:
        reshaped_chunk = np.squeeze(reshaped_chunk, 0)
    else:
        # Channels (RGB) need to be along the last axis for PIL
        reshaped_chunk = np.swapaxes(reshaped_chunk, 0, 3)

    img = PIL.Image.fromarray(reshaped_chunk)
    io_buf = io.BytesIO()
    img.save(io_buf, format="jpeg", quality=jpeg_quality,
             optimize=True, progressive=True)
    return io_buf.getvalue()<commit_msg>fix bug in RGB JPEG encoding<commit_after>#! /usr/bin/env python3
#
# Copyright (c) 2016, 2017, 2018 Forschungszentrum Juelich GmbH
# Author: <NAME> <<EMAIL>>
#
# This software is made available under the MIT licence, see LICENCE.txt.

import io

import numpy as np
import PIL.Image


def encode_chunk(chunk, jpeg_quality, jpeg_plane):
    assert 0 <= jpeg_quality <= 100
    assert jpeg_plane in ("xy", "xz")
    num_channels = chunk.shape[0]
    if jpeg_plane == "xy":
        reshaped_chunk = chunk.reshape(
            num_channels, chunk.shape[1] * chunk.shape[2], chunk.shape[3])
    elif jpeg_plane == "xz":
        reshaped_chunk = chunk.reshape(
            num_channels, chunk.shape[1], chunk.shape[2] * chunk.shape[3])

    if num_channels == 1:
        reshaped_chunk = np.squeeze(reshaped_chunk, 0)
    else:
        # Channels (RGB) need to be along the last axis for PIL
        reshaped_chunk = np.moveaxis(reshaped_chunk, 0, -1)

    img = PIL.Image.fromarray(reshaped_chunk)
    io_buf = io.BytesIO()
    # Chroma sub-sampling is disabled because it can create strong artefacts at
    # the border where the chunk size is odd. Progressive is enabled because it
    # generally creates smaller JPEG files.
    img.save(io_buf, format="jpeg", quality=jpeg_quality,
             optimize=True, progressive=True, subsampling=0)
    return io_buf.getvalue()
{'fraction_non_alphanumeric': 0.10978043912175649, 'fraction_numerical': 0.022754491017964073, 'mean_word_length': 3.2546689303904923, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2734085', 'n_tokens_mistral': 896, 'n_tokens_neox': 822, 'n_words': 232}