Skip to content

Commit

Permalink
Use imagesize rather than magic
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Hall committed Sep 19, 2023
1 parent 2a8a1d4 commit 8ccb380
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
22 changes: 5 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,11 @@ The following packages are optional:
Python bindings of fontconfig are required for displaying
genealogical symbols

* **magic**

Python magic bindings required to have better performances with image
processing.
If this module is not available, we continue to use Gdk.
This avoid to load the image in memory. This is a real improvement
when we have many big images.
Used in odfdoc, rtfdoc and webreport and tested with png, gif, jpeg, bmp, tiff
#
# file size with magic without (Gdk) ratio
# example 1 : 256k 0.00080 0.00575 7
# example 2 : 21M 0.00171 0.55860 326

Debian, Ubuntu, ... : python3-magic
Fedora, Redhat, ... : python3-magic
openSUSE : python-magic
ArchLinux : python-magic
* **python-imagesize**

Provides better image processing performance. If this module is not available,
we continue to use Gdk. This provides a real improvement when we need to
process many big images.

Optional packages required by Third-party Addons
------------------------------------------------
Expand Down
29 changes: 5 additions & 24 deletions gramps/gen/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,12 @@ def image_size(source):
from gi.repository import GLib

try:
import re
import magic

# For performance reasons, we'll try to get image size from magic.
# This avoid to load the image in memory. This is a real improvement
# when we have many big images.
# Used in odfdoc, rtfdoc and webreport and tested with png, gif, jpeg,
# bmp, tiff
#
# file size with magic without (Gdk) ratio
# example 1 : 256k 0.00080 0.00575 7
# example 2 : 21M 0.00171 0.55860 326
img = magic.from_file(source)
found = img.find("TIFF")
if found == 0:
width = re.search("width=(\d+)", img).groups()
height = re.search("height=(\d+)", img).groups()
return (int(width[0]), int(height[0]))
found = img.find("precision")
if found > 0:
img = img[found:]
size = re.search("(\d+)\s*x\s*(\d+)", img).groups()
return (int(size[0]), int(size[1]))
# For performance reasons, we'll try to get image size from imagesize.
import imagesize

return iamgesize.get(source)
except (ImportError, FileNotFoundError):
# python-magic is not installed or the file does not exist.
# python-imagesize is not installed or the file does not exist.
# So Trying to get image size with Gdk.
try:
img = GdkPixbuf.Pixbuf.new_from_file(source)
Expand Down

0 comments on commit 8ccb380

Please sign in to comment.