7288/7290: image_dpi and cropping fixes for ODFdoc
Port my fixes based on the patch from Matthias Basler from gramps34 [2eca30] 7290: use simpler math in image_dpi [2f5e22] 7290: align warning with the image_dpi fix logic [36f84e] 7290: image_dpi default calc based on gtk.gdk [161ce3] 7290: ImgManip.image_dpi shouldn't return None [460e63] 7288/7290 refactor out crop_percentage_to_pixel [a8a38f] 7288/7290 refactor out crop_percentage_to_subpixel [0d61bb] 7288/7290: refactor fix from matthiasbasler [088146] rm relict RCS kwd [76df5d] 7288/7290: image cropping fixes in ODFDoc Using legacy gtk interface in image.py at the moment, need to port to the new style. Tested with python2.
This commit is contained in:
parent
b1c1466a15
commit
37f4b411e3
@ -47,6 +47,20 @@ import tempfile
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from .file import get_unicode_path_from_env_var
|
from .file import get_unicode_path_from_env_var
|
||||||
|
|
||||||
|
def crop_percentage_to_subpixel(width, height, crop):
|
||||||
|
"""
|
||||||
|
Convert from Gramps cropping coordinates [0, 100] to
|
||||||
|
pixels, given image width and height. No rounding to pixel resolution.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
crop[0]/100.0*width,
|
||||||
|
crop[1]/100.0*height,
|
||||||
|
crop[2]/100.0*width,
|
||||||
|
crop[3]/100.0*height )
|
||||||
|
|
||||||
|
def crop_percentage_to_pixel(width, height, crop):
|
||||||
|
return map (int, crop_percentage_to_subpixel(width, height, crop))
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# resize_to_jpeg
|
# resize_to_jpeg
|
||||||
@ -73,12 +87,9 @@ def resize_to_jpeg(source, destination, width, height, crop=None):
|
|||||||
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
||||||
|
|
||||||
if crop:
|
if crop:
|
||||||
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
(start_x, start_y, end_x, end_y
|
||||||
start_x = int((crop[0]/100.0)*img.get_width())
|
) = crop_percentage_to_pixel(
|
||||||
start_y = int((crop[1]/100.0)*img.get_height())
|
img.get_width(), img.get_height(), crop)
|
||||||
end_x = int((crop[2]/100.0)*img.get_width())
|
|
||||||
end_y = int((crop[3]/100.0)*img.get_height())
|
|
||||||
|
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
||||||
else:
|
else:
|
||||||
@ -96,35 +107,44 @@ def resize_to_jpeg(source, destination, width, height, crop=None):
|
|||||||
# image_dpi
|
# image_dpi
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
|
MM_PER_INCH = 25.4
|
||||||
def image_dpi(source):
|
def image_dpi(source):
|
||||||
"""
|
"""
|
||||||
Return the dpi found in the image header. None is returned if no dpi attribute
|
Return the dpi found in the image header. Use a sensible
|
||||||
is available.
|
default of the screen DPI or 96.0 dpi if N/A.
|
||||||
|
|
||||||
:param source: source image file, in any format that PIL recognizes
|
:param source: source image file, in any format that PIL recognizes
|
||||||
:type source: unicode
|
:type source: unicode
|
||||||
:rtype: int
|
:rtype: int
|
||||||
:returns: the DPI setting in the image header
|
:returns: (x_dpi, y_dpi)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
import PIL.Image
|
import PIL.Image
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import logging
|
import logging
|
||||||
logging.warning(_("WARNING: PIL module not loaded. "
|
logging.warning(_("WARNING: PIL module not loaded. "
|
||||||
"Image cropping in report files will not be available."))
|
"Image cropping in report files will be impaired."))
|
||||||
|
|
||||||
dpi = None
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
img = PIL.Image.open(source)
|
img = PIL.Image.open(source)
|
||||||
except IOError:
|
except IOError:
|
||||||
dpi = None
|
pass
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
dpi = img.info["dpi"]
|
dpi = img.info["dpi"]
|
||||||
|
return dpi
|
||||||
except (AttributeError, KeyError):
|
except (AttributeError, KeyError):
|
||||||
dpi = None
|
pass
|
||||||
|
try:
|
||||||
|
import gtk
|
||||||
|
dpi = (
|
||||||
|
gtk.gdk.screen_width() * MM_PER_INCH / gtk.gdk.screen_width_mm(),
|
||||||
|
gtk.gdk.screen_height() * MM_PER_INCH / gtk.gdk.screen_height_mm()
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
dpi = (96.0,96.0) #LibOO 3.6 assumes this if image contains no DPI info
|
||||||
|
# This isn't safe even within a single platform (Windows), but we
|
||||||
|
# can't do better if all of the above failed. See bug# 7290.
|
||||||
return dpi
|
return dpi
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -207,12 +227,9 @@ def resize_to_buffer(source, size, crop=None):
|
|||||||
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
||||||
|
|
||||||
if crop:
|
if crop:
|
||||||
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
(start_x, start_y, end_x, end_y
|
||||||
start_x = int((crop[0]/100.0)*img.get_width())
|
) = crop_percentage_to_pixel(
|
||||||
start_y = int((crop[1]/100.0)*img.get_height())
|
img.get_width(), img.get_height(), crop)
|
||||||
end_x = int((crop[2]/100.0)*img.get_width())
|
|
||||||
end_y = int((crop[3]/100.0)*img.get_height())
|
|
||||||
|
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
||||||
else:
|
else:
|
||||||
@ -250,11 +267,9 @@ def resize_to_jpeg_buffer(source, size, crop=None):
|
|||||||
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
img = GdkPixbuf.Pixbuf.new_from_file(source)
|
||||||
|
|
||||||
if crop:
|
if crop:
|
||||||
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
(start_x, start_y, end_x, end_y
|
||||||
start_x = int((crop[0]/100.0)*img.get_width())
|
) = crop_percentage_to_pixel(
|
||||||
start_y = int((crop[1]/100.0)*img.get_height())
|
img.get_width(), img.get_height(), crop)
|
||||||
end_x = int((crop[2]/100.0)*img.get_width())
|
|
||||||
end_y = int((crop[3]/100.0)*img.get_height())
|
|
||||||
|
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
img = img.new_subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
||||||
|
@ -24,8 +24,6 @@
|
|||||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
#
|
#
|
||||||
|
|
||||||
# $Id$
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ODFDoc : used to generate Open Office Document
|
ODFDoc : used to generate Open Office Document
|
||||||
"""
|
"""
|
||||||
@ -93,7 +91,8 @@ from gramps.plugins.lib.libodfbackend import OdfBackend
|
|||||||
from gramps.gen.const import PROGRAM_NAME
|
from gramps.gen.const import PROGRAM_NAME
|
||||||
from gramps.version import VERSION
|
from gramps.version import VERSION
|
||||||
from gramps.gen.plug.report import utils as ReportUtils
|
from gramps.gen.plug.report import utils as ReportUtils
|
||||||
from gramps.gen.utils.image import image_size, image_dpi, image_actual_size
|
from gramps.gen.utils.image import (image_size, image_dpi, image_actual_size,
|
||||||
|
crop_percentage_to_subpixel)
|
||||||
from gramps.gen.errors import ReportError
|
from gramps.gen.errors import ReportError
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -1035,27 +1034,30 @@ class ODFDoc(BaseDoc, TextDoc, DrawDoc):
|
|||||||
pos = pos.title() if pos in ['left', 'right', 'single'] else 'Row'
|
pos = pos.title() if pos in ['left', 'right', 'single'] else 'Row'
|
||||||
|
|
||||||
if crop:
|
if crop:
|
||||||
|
(start_x, start_y, end_x, end_y
|
||||||
|
) = crop_percentage_to_subpixel(x, y, crop)
|
||||||
|
|
||||||
|
# Need to keep the ratio intact, otherwise scaled images look stretched
|
||||||
|
# if the dimensions aren't close in size
|
||||||
|
(act_width, act_height) = image_actual_size(
|
||||||
|
x_cm, y_cm, int(end_x-start_x), int(end_y-start_y)
|
||||||
|
)
|
||||||
|
|
||||||
dpi = image_dpi(file_name)
|
dpi = image_dpi(file_name)
|
||||||
|
|
||||||
if dpi:
|
# ODF wants crop measurements in inch and as margins from each side
|
||||||
(act_width, act_height) = image_actual_size(
|
left = start_x/dpi[0]
|
||||||
x_cm, y_cm, crop[2] - crop[0], crop[3] - crop[1]
|
right = (x - end_x)/dpi[0]
|
||||||
)
|
top = start_y/dpi[1]
|
||||||
|
bottom = (y - end_y)/dpi[1]
|
||||||
|
crop = (top, right, bottom, left)
|
||||||
|
|
||||||
left = ((crop[0]/100.0)*x)/dpi[0]
|
self.StyleList_photos.append(
|
||||||
right = (x - ((crop[2]/100.0)*x))/dpi[0]
|
[pos, crop]
|
||||||
top = ((crop[1]/100.0)*y)/dpi[1]
|
)
|
||||||
bottom = (y - ((crop[3]/100.0)*y))/dpi[1]
|
|
||||||
|
|
||||||
crop = (top, right, bottom, left)
|
pos += "_" + str(crop)
|
||||||
|
|
||||||
self.StyleList_photos.append(
|
|
||||||
[pos, crop]
|
|
||||||
)
|
|
||||||
|
|
||||||
pos += "_" + str(crop)
|
|
||||||
else:
|
|
||||||
(act_width, act_height) = image_actual_size(x_cm, y_cm, x, y)
|
|
||||||
else:
|
else:
|
||||||
(act_width, act_height) = image_actual_size(x_cm, y_cm, x, y)
|
(act_width, act_height) = image_actual_size(x_cm, y_cm, x, y)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user