Skip to content

Instantly share code, notes, and snippets.

@kleisauke
Last active July 25, 2018 10:54
Show Gist options
  • Save kleisauke/7bf56f583ae32d0551991d841c481b36 to your computer and use it in GitHub Desktop.
Save kleisauke/7bf56f583ae32d0551991d841c481b36 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from pyvips import ffi, vips_lib, Image, Error, _to_bytes
def has_loader(file):
name = vips_lib.vips_foreign_find_load(_to_bytes(file))
if name == ffi.NULL:
return False
return True
# tmp file content = https://www.welt.de/favicon.ico
tmp_file = 'x'
loader = has_loader(tmp_file)
if loader:
print('.ico file has loader')
print('try to load it with new_from_file')
try:
image = Image.new_from_file(tmp_file)
print('Successfully load .ico with new_from_file. Loader: {}'.format(image.get('vips-loader')))
except Error:
print('Failed to load .ico with new_from_file')
print('try to load it with thumbnail')
try:
thumb = Image.thumbnail(tmp_file, 200)
print('Successfully load .ico with thumbnail. Loader: {}'.format(thumb.get('vips-loader')))
except Error:
print('Failed to load .ico with thumbnail')
else:
print('no loader for this image format')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment