Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bigcat88 committed Apr 17, 2022
1 parent 8ba9e88 commit 5ce3038
Show file tree
Hide file tree
Showing 23 changed files with 911 additions and 879 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-cache-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: manylinux preparations
if: matrix.cibw_buildlinux == 'manylinux'
run: echo INSTALL_OS_PACKAGES="yum update -y && yum install -y $OS_PACKAGES" >> $GITHUB_ENV
run: echo INSTALL_OS_PACKAGES="yum makecache && yum install -y $OS_PACKAGES" >> $GITHUB_ENV
env:
OS_PACKAGES: "git-all"

Expand Down
23 changes: 20 additions & 3 deletions .github/workflows/test-wheels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
]

steps:
- name: Delay, waiting Pypi to update.
if: ${{ github.event_name != 'workflow_dispatch' }}
run: sleep 60

- uses: actions/checkout@v3
- name: Set up QEMU
if: matrix.i['arch'] == 'arm64'
Expand Down Expand Up @@ -73,6 +77,11 @@ jobs:
python-version: ["3.7", "3.8", "3.9", "3.10"]

steps:
- name: Delay, waiting Pypi to update.
if: ${{ github.event_name != 'workflow_dispatch' }}
run: Start-Sleep -s 60
shell: powershell

- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
Expand All @@ -85,7 +94,7 @@ jobs:
python3 -m pip install --only-binary=:all: pillow_heif
- name: Test wheel
run: cd .. && python3 -m pytest -s pillow_heif
run: cd .. && python3 -m pytest -rs pillow_heif

macos-wheels:
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
Expand All @@ -96,6 +105,10 @@ jobs:
python-version: ["pypy-3.7", "pypy-3.8", "3.7", "3.8", "3.9", "3.10"]

steps:
- name: Delay, waiting Pypi to update.
if: ${{ github.event_name != 'workflow_dispatch' }}
run: sleep 60

- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
Expand All @@ -109,7 +122,7 @@ jobs:
python3 -m pip install --only-binary=:all: pillow_heif
- name: Test wheel
run: cd .. && python3 -m pytest -s pillow_heif
run: cd .. && python3 -m pytest -rs pillow_heif

manylinux-wheels:
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
Expand All @@ -120,6 +133,10 @@ jobs:
python-version: ["pypy-3.7", "pypy-3.8", "3.6", "3.7", "3.8", "3.9", "3.10"]

steps:
- name: Delay, waiting Pypi to update.
if: ${{ github.event_name != 'workflow_dispatch' }}
run: sleep 60

- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
Expand All @@ -133,4 +150,4 @@ jobs:
python3 -m pip install --only-binary=:all: pillow_heif
- name: Test wheel
run: cd .. && python3 -m pytest -s pillow_heif
run: cd .. && python3 -m pytest -rs pillow_heif
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file.

## [0.2.1 - 2022-04-18]
## [0.2.1 - 2022-04-17]

### Added

Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ if pillow_heif.is_supported('input.heic'):
heif_file = pillow_heif.open_heif('input.heic')
for img in heif_file: # you still can use it without iteration, like before.
img.scale(1024, 768) # `libheif` does not provide much operations, that can be done on image, so just scaling it.
# get save mask and set thumb_box=-1 to ignore all thumbs image have.
save_mask = heif_file.get_img_thumb_mask_for_save(pillow_heif.HeifSaveMask.SAVE_ALL, thumb_box=-1)
heif_file.add_thumbs_to_mask(save_mask, [768, 512, 256]) # add three new thumbnail boxes.
heif_file.add_thumbnails([768, 512, 256]) # add three new thumbnail boxes.
# default quality is probably ~77 in x265, set it a bit lower and specify `save mask`.
heif_file.save('output.heic', quality=70, save_mask=save_mask)
heif_file.save('output.heic', quality=70, save_all=False) #save_all is True by default.
exit(0)
```
### [More examples](https://github.com/bigcat88/pillow_heif/tree/master/examples)
Expand Down Expand Up @@ -85,7 +83,6 @@ The returned `HeifImageFile` by `Pillow` function `Image.open` has the following
* `metadata` - is a list of dictionaries with `type` and `data` keys, excluding `exif`. May be empty.
* `icc_profile` - contains data and present only when file has `ICC` color profile(`prof` or `rICC`).
* `nclx_profile` - contains data and present only when file has `NCLX` color profile.
* `img_id` - id of image, needed for encoding operations.

### The HeifFile object
The returned `HeifFile` by function `open_heif` or `from_pillow` has the following properties:
Expand Down
2 changes: 1 addition & 1 deletion docker/test_wheels.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ RUN python3 -m pip install --no-deps --only-binary=:all: pillow_heif

COPY . /pillow_heif

RUN python3 -m pytest -s -v pillow_heif/. && echo "**** Test Done ****" && python3 -m pip show pillow_heif
RUN python3 -m pytest -rs -v pillow_heif/. && echo "**** Test Done ****" && python3 -m pip show pillow_heif
9 changes: 5 additions & 4 deletions docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,25 @@ Notes:

1. Building for first time will take a long time, if in your system `cmake` version `>=3.16.1` is not present.
2. Arm7(32 bit):
* On Alpine you need install `aom-dev`.
* On Alpine need install `aom-dev`.
* On Ubuntu(22.04+) you need install `libaom-dev`.
* On Ubuntu less then 22.04 you can compile it from source, but `AV1` codecs will be not avalaible.
* On Ubuntu less 22.04 you can compile it from source, but `AV1` codecs will be not available.
* Encoder will not be available if you did not install `x265`. It is not build from source by default on armv7.

### MacOS
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install x265 libjpeg libde265 libheif
pip3 install --no-binary pillow_heif
pip3 install --no-binary=:all: pillow_heif
```

### Windows
By default, build script assumes that `vcpkg` builds libs in `C:\vcpkg\installed\x64-windows`.
If not, then set `VCPKG_PREFIX` environment variable to your custom path, e.g. `setx VCPKG_PREFIX "D:\vcpkg\installed\x64-windows"`
```bat
vcpkg install aom libheif --triplet=x64-windows
pip3 install --no-binary pillow_heif
pip3 install --no-binary=:all: pillow_heif
```
After that copy `heif.dll`, `aom.dll`, `libde265.dll` and `libx265.dll` from `vcpkg\installed\x64-windows\bin` to site-packages root.

Note: there is no support for 10/12 bit file formats for encoder now on Windows.
11 changes: 1 addition & 10 deletions examples/opener_display_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,8 @@
img = Image.open(image_path)
img.load()
for i, frame in enumerate(ImageSequence.Iterator(img)):
# `img.info["thumbnails"]` can be changed in future versions.
# Probably soon will be introduced a new method instead of `Image.frombytes` for thumbnails.
for thumb in img.info["thumbnails"]:
thumb_img = Image.frombytes(
thumb.mode,
thumb.size,
thumb.data,
"raw",
thumb.mode,
thumb.stride,
)
thumb_img = thumb.to_pillow()
thumb_img.show(title=f"Img={i} Thumbnail={thumb.info['thumb_id']}")
img.show(title=f"Image index={i}")
except Exception as e:
Expand Down
22 changes: 22 additions & 0 deletions examples/reader_add_thumbnails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import sys
import traceback
from pathlib import Path

import pillow_heif

if __name__ == "__main__":
os.chdir(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tests"))
target_folder = "../converted"
os.makedirs(target_folder, exist_ok=True)
image_path = Path("images/pug_1_0.heic")
try:
heif_image = pillow_heif.open_heif(image_path)
result_path = os.path.join(target_folder, f"{image_path.stem}.heic")
heif_image.add_thumbnails([256, 512])
heif_image.save(result_path, quality=35)
heif_image.close()
except Exception as e:
print(f"{repr(e)} during processing {image_path.as_posix()}", file=sys.stderr)
print(traceback.format_exc())
exit(0)
20 changes: 2 additions & 18 deletions examples/reader_display_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import traceback
from pathlib import Path

from PIL import Image

import pillow_heif

# This demo displays all thumbnails and all images.
Expand All @@ -19,23 +17,9 @@
print(f"number of images in file: {len(heif_image)}")
for image in heif_image:
for thumb in image.thumbnails:
thumbnail_img = Image.frombytes(
thumb.mode,
thumb.size,
thumb.data,
"raw",
thumb.mode,
thumb.stride,
)
thumbnail_img = thumb.to_pillow()
thumbnail_img.show(title=f"Thumbnail {thumb.info['thumb_id']}")
_img = Image.frombytes(
image.mode,
image.size,
image.data,
"raw",
image.mode,
image.stride,
)
_img = image.to_pillow()
_img.show(title=f"Image {image.info['img_id']}")
except Exception as e:
print(f"{repr(e)} during processing {image_path.as_posix()}", file=sys.stderr)
Expand Down
22 changes: 22 additions & 0 deletions examples/reader_remove_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import sys
import traceback
from pathlib import Path

import pillow_heif

if __name__ == "__main__":
os.chdir(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tests"))
target_folder = "../converted"
os.makedirs(target_folder, exist_ok=True)
image_path = Path("images/pug_2_0.heic")
try:
heif_image = pillow_heif.open_heif(image_path)
result_path = os.path.join(target_folder, f"{image_path.stem}.heic")
del heif_image[1]
heif_image.save(result_path, quality=35)
heif_image.close()
except Exception as e:
print(f"{repr(e)} during processing {image_path.as_posix()}", file=sys.stderr)
print(traceback.format_exc())
exit(0)
1 change: 0 additions & 1 deletion pillow_heif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
HeifCompressionFormat,
HeifErrorCode,
HeifFiletype,
HeifSaveMask,
)
from .error import HeifError

Expand Down
2 changes: 1 addition & 1 deletion pillow_heif/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" Version of pillow_heif """

__version__ = "0.2.0"
__version__ = "0.2.1"
15 changes: 9 additions & 6 deletions pillow_heif/as_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
Opener for Pillow library.
"""

from copy import deepcopy
from typing import Any

from PIL import Image, ImageFile

from ._options import options
from .error import HeifError
from .heif import from_pillow, is_supported, open_heif
from .heif import HeifImage, from_pillow, is_supported, open_heif
from .misc import reset_orientation


class HeifImageFile(ImageFile.ImageFile):
format = "HEIF"
format_description = "HEIF container for HEVC and AV1"
heif_file: Any
_close_exclusive_fp_after_loading = False

def __init__(self, *args, **kwargs):
self.heif_file = None
Expand All @@ -33,13 +35,14 @@ def _open(self):
def load(self):
if self.heif_file:
frame_heif = self._heif_file_by_index(self.tell())
frame_heif.load()
self.load_prepare()
self.frombytes(frame_heif.data, "raw", (self.mode, frame_heif.stride))
if self.is_animated or self.info["thumbnails"]:
if self.is_animated:
frame_heif.unload()
else:
self.info["thumbnails"] = deepcopy(self.info["thumbnails"])
self.heif_file = None
self._close_exclusive_fp_after_loading = True
return super().load()

def seek(self, frame):
Expand Down Expand Up @@ -73,7 +76,7 @@ def _seek_check(self, frame):
raise EOFError("attempt to seek outside sequence")
return self.tell() != frame

def _heif_file_by_index(self, index):
def _heif_file_by_index(self, index) -> HeifImage:
return self.heif_file[index]

def _init_from_heif_file(self, heif_image) -> None:
Expand All @@ -89,11 +92,11 @@ def _init_from_heif_file(self, heif_image) -> None:


def _save(im, fp, _filename):
from_pillow(im, load_one=True).save(fp, save_one=True, **im.encoderinfo)
from_pillow(im, load_one=True).save(fp, save_all=False, **im.encoderinfo)


def _save_all(im, fp, _filename):
from_pillow(im).save(fp, save_one=False, **im.encoderinfo)
from_pillow(im).save(fp, save_all=True, **im.encoderinfo)


def register_heif_opener(**kwargs):
Expand Down
13 changes: 1 addition & 12 deletions pillow_heif/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class HeifChannel(IntEnum):
ALPHA = 6
"""Alpha color channel"""
INTERLEAVED = 10
"""Interleaved color channels"""
"""Interleaved color channel"""


def encode_fourcc(fourcc):
Expand Down Expand Up @@ -172,17 +172,6 @@ class HeifCompressionFormat(IntEnum):
"""The compression format is AV1."""


class HeifSaveMask(IntEnum):
"""Possible mask types for saving HEIC."""

SAVE_ALL = 0
"""Mask where all images set to True."""
SAVE_ONE = 1
"""Mask where all images except main is set to False."""
SAVE_NONE = 2
"""Mask where all images set to False."""


# --------------------------------------------------------------------
# DEPRECATED CONSTANTS.
# pylint: disable=invalid-name
Expand Down
Loading

0 comments on commit 5ce3038

Please sign in to comment.