Skip to content

Commit

Permalink
Beginning support of AVIF; fix some image bugs (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
aw-was-here committed May 7, 2023
1 parent 2f65bc8 commit a493072
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

* Experimental feature: Given an option to use Musicbrainz to fill in missing
metadata based only on artist and title (and album if available).
* Add support for AVIF graphics. At some point, all of the templates will be
updated to handle multiple formats so be prepared!
* On Windows, the ability to read from Windows Media Transport compatible
software, such as Amazon Music, Soundcloud, and likely others. (Ironically,
Windows Media Player doesn't appear to use it for whatever reason.)
Expand Down
32 changes: 29 additions & 3 deletions nowplaying/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jinja2
import normality
import PIL.Image
import pillow_avif # pylint: disable=unused-import

STRIPWORDLIST = ['clean', 'dirty', 'explicit', 'official music video']
STRIPRELIST = [
Expand Down Expand Up @@ -145,13 +146,38 @@ def image2png(rawdata):
return rawdata

try:
origimage = rawdata
imgbuffer = io.BytesIO(origimage)
imgbuffer = io.BytesIO(rawdata)
logging.getLogger('PIL.TiffImagePlugin').setLevel(logging.CRITICAL + 1)
logging.getLogger('PIL.PngImagePlugin').setLevel(logging.CRITICAL + 1)
image = PIL.Image.open(imgbuffer)
imgbuffer = io.BytesIO(rawdata)
if image.format != 'PNG':
image.convert(mode='RGB').save(imgbuffer, format='png')
image.convert(mode='RGB').save(imgbuffer, format='PNG')
except Exception as error: #pylint: disable=broad-except
logging.debug(error)
return None
logging.debug("Leaving image2png")
return imgbuffer.getvalue()


def image2avif(rawdata):
''' convert an image to png '''

if not rawdata:
return None

if rawdata.startswith(b'\x00\x00\x00 ftypavif'):
logging.debug('already AVIF, skipping convert')
return rawdata

try:
imgbuffer = io.BytesIO(rawdata)
logging.getLogger('PIL.TiffImagePlugin').setLevel(logging.CRITICAL + 1)
logging.getLogger('PIL.PngImagePlugin').setLevel(logging.CRITICAL + 1)
image = PIL.Image.open(imgbuffer)
imgbuffer = io.BytesIO(rawdata)
if image.format != 'AVIF':
image.convert(mode='RGB').save(imgbuffer, format='AVIF')
except Exception as error: #pylint: disable=broad-except
logging.debug(error)
return None
Expand Down
3 changes: 2 additions & 1 deletion requirements-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ nltk==3.8.1
normality==2.4.0
pid==3.0.4
pillow==9.5.0
pillow_avif_plugin==1.3.1
pyacoustid==1.2.2
pypresence==4.2.1
PySide6==6.5.0
Expand Down Expand Up @@ -43,4 +44,4 @@ python-dateutil
#
# required to install
#
versioningit==2.2.0
versioningit==2.2.0
Binary file added tests/images/1x1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,28 @@ def test_basicstrip_ovm4():
title = nowplaying.utils.titlestripper_basic(title=metadata['title'])
assert metadata['title'] == 'Clean - Official Music Video'
assert title == 'Clean'


def test_image2png(getroot):
''' check png image conversion '''
filename = getroot.joinpath('tests', 'images', '1x1.jpg')
with open(filename, 'rb') as fhin:
image = fhin.read()

pngdata = nowplaying.utils.image2png(image)

pngdata2 = nowplaying.utils.image2png(pngdata)
assert pngdata.startswith(b'\211PNG\r\n\032\n')
assert pngdata2 == pngdata


def test_image2avif(getroot):
''' check png image conversion '''
filename = getroot.joinpath('tests', 'images', '1x1.jpg')
with open(filename, 'rb') as fhin:
image = fhin.read()

avifdata = nowplaying.utils.image2avif(image)
avifdata2 = nowplaying.utils.image2avif(avifdata)
assert avifdata.startswith(b'\x00\x00\x00 ftypavif')
assert avifdata2 == avifdata

0 comments on commit a493072

Please sign in to comment.