2023-04-13 23:27:58 -07:00
|
|
|
# SPDX-FileCopyrightText: 2023 James R. Barlow
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-04-14 13:44:47 -07:00
|
|
|
from ocrmypdf.imageops import bytes_per_pixel, calculate_downsample, downsample_image
|
|
|
|
|
2023-04-13 23:27:58 -07:00
|
|
|
|
|
|
|
def test_bytes_per_pixel():
|
|
|
|
assert bytes_per_pixel('RGB') == 4
|
|
|
|
assert bytes_per_pixel('RGBA') == 4
|
|
|
|
assert bytes_per_pixel('LA') == 2
|
|
|
|
assert bytes_per_pixel('L') == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_calculate_downsample():
|
|
|
|
im = Image.new('RGB', (100, 100))
|
2023-04-13 23:49:12 -07:00
|
|
|
assert calculate_downsample(im, max_size=(50, 50)) == (50, 50)
|
|
|
|
assert calculate_downsample(im, max_pixels=2500) == (50, 50)
|
|
|
|
assert calculate_downsample(im, max_bytes=10000) == (50, 50)
|
|
|
|
assert calculate_downsample(im, max_bytes=100000) == (100, 100)
|
2023-04-13 23:27:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_downsample_image():
|
|
|
|
im = Image.new('RGB', (100, 100))
|
|
|
|
im.info['dpi'] = (300, 300)
|
2023-04-13 23:49:12 -07:00
|
|
|
ds = downsample_image(im, (50, 50))
|
|
|
|
assert ds.size == (50, 50)
|
|
|
|
assert ds.info['dpi'] == (150, 150)
|