From fe24c9cce04684adeae75813ce0cf28b80878fda Mon Sep 17 00:00:00 2001
From: Hugo Wetterberg <hugo@wetterberg.nu>
Date: Thu, 4 Sep 2025 17:48:31 +0200
Subject: [PATCH] script getter for actual image PPI

---
 scribus/plugins/scriptplugin/cmdgetprop.cpp   | 19 +++++++++++++++++++
 scribus/plugins/scriptplugin/cmdgetprop.h     | 11 +++++++++++
 scribus/plugins/scriptplugin/scriptplugin.cpp |  1 +
 3 files changed, 31 insertions(+)

diff --git scribus/plugins/scriptplugin/cmdgetprop.cpp scribus/plugins/scriptplugin/cmdgetprop.cpp
index dd44aca96..a16f75afa 100644
--- scribus/plugins/scriptplugin/cmdgetprop.cpp
+++ scribus/plugins/scriptplugin/cmdgetprop.cpp
@@ -379,6 +379,25 @@ PyObject *scribus_getimagescale(PyObject* /* self */, PyObject* args)
 	return Py_BuildValue("(ff)", item->imageXScale() / 72.0 * item->pixm.imgInfo.xres, item->imageYScale() / 72.0 * item->pixm.imgInfo.yres);
 }
 
+PyObject *scribus_getimageppi(PyObject* /* self */, PyObject* args)
+{
+	PyESString name;
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", name.ptr()))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+	PageItem *item = GetUniqueItem(QString::fromUtf8(name.c_str()));
+	if (item == nullptr)
+		return nullptr;
+	if (!item->isImageFrame())
+	{
+		PyErr_SetString(ScribusException, QObject::tr("Specified item not an image frame.","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	return Py_BuildValue("(ii)", qRound(72.0 / item->imageXScale()), qRound(72.0 / item->imageYScale()));
+}
+
 PyObject *scribus_getimagefile(PyObject* /* self */, PyObject* args)
 {
 	PyESString name;
diff --git scribus/plugins/scriptplugin/cmdgetprop.h scribus/plugins/scriptplugin/cmdgetprop.h
index 1e39a0593..bceb0a284 100644
--- scribus/plugins/scriptplugin/cmdgetprop.h
+++ scribus/plugins/scriptplugin/cmdgetprop.h
@@ -277,6 +277,17 @@ Returns a (x, y) tuple containing the scaling values of the image frame\n\
 /*! Returns image scale of the object */
 PyObject *scribus_getimagescale(PyObject * /*self*/, PyObject* args);
 
+/*! docstring */
+PyDoc_STRVAR(scribus_getimageppi__doc__,
+	QT_TR_NOOP("getImagePpi([\"name\"]) -> (x,y)\n\
+\n\
+Returns a (x, y) tuple containing the actual PPI of the image frame\n\
+\"name\".  If \"name\" is not given the currently selected item is used.\n\
+"));
+/*! Returns actual image ppi of the object */
+PyObject *scribus_getimageppi(PyObject * /*self*/, PyObject* args);
+
+
 /*! docstring */
 PyDoc_STRVAR(scribus_getposition__doc__,
 QT_TR_NOOP("getPosition([\"name\"]) -> (x,y)\n\
diff --git scribus/plugins/scriptplugin/scriptplugin.cpp scribus/plugins/scriptplugin/scriptplugin.cpp
index 20f2b34cb..30743afe1 100644
--- scribus/plugins/scriptplugin/scriptplugin.cpp
+++ scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -381,6 +381,7 @@ PyMethodDef scribus_methods[] = {
 	{ "getImagePreviewResolution", scribus_getimagepreviewresolution, METH_VARARGS, tr(scribus_getimagepreviewresolution__doc__)},
 	{ "getImagePageCount", scribus_getimagepagecount, METH_VARARGS, tr(scribus_getimagepagecount__doc__)},
 	{ "getImageScale", scribus_getimagescale, METH_VARARGS, tr(scribus_getimagescale__doc__)},
+	{ "getImagePpi", scribus_getimageppi, METH_VARARGS, tr(scribus_getimageppi__doc__)},
 	{ "getInfo", (PyCFunction) scribus_getinfo, METH_VARARGS, tr(scribus_getinfo__doc__)},
 	{ "getItemPageNumber", scribus_getitempagenumber, METH_VARARGS, tr(scribus_getitempagenumber__doc__)},
 	{ "getLastLinkedFrame", scribus_getlastlinkedframe, METH_VARARGS, tr(scribus_getlastlinkedframe__doc__)},
-- 
2.51.0

