View Issue Details

IDProjectCategoryView StatusLast Update
0015455ScribusScripterpublic2019-06-04 20:56
ReporterGibbz Assigned Tojghali  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version1.5.4 
Target VersionFixed in Version1.5.5.svn 
Summary0015455: Add transparency to image exporter in scripter
DescriptionI have added a patch to support transparency setting in images.
Also included a sample script which uses the new command
img.transparency = True
TagsNo tags attached.
PatchYes

Activities

Gibbz

2018-10-11 09:15

reporter  

transparency.patch (4,607 bytes)
diff --git a/scribus/plugins/scriptplugin/objimageexport.cpp b/scribus/plugins/scriptplugin/objimageexport.cpp
index 0c1bad200..1dd0eedc0 100644
--- a/scribus/plugins/scriptplugin/objimageexport.cpp
+++ b/scribus/plugins/scriptplugin/objimageexport.cpp
@@ -26,6 +26,7 @@ typedef struct
 	int dpi; // DPI of the bitmap
 	int scale; // how is bitmap scaled 100 = 100%
 	int quality; // quality/compression <1; 100>
+    bool transparency;
 } ImageExport;
 
 static void ImageExport_dealloc(ImageExport* self)
@@ -50,6 +51,7 @@ static PyObject * ImageExport_new(PyTypeObject *type, PyObject * /*args*/, PyObj
 		self->dpi = 72;
 		self->scale = 100;
 		self->quality = 100;
+        self->transparency = false;
 	}
 	return (PyObject *) self;
 }
@@ -63,6 +65,7 @@ static PyMemberDef ImageExport_members[] = {
 	{const_cast<char*>("dpi"), T_INT, offsetof(ImageExport, dpi), 0, imgexp_dpi__doc__},
 	{const_cast<char*>("scale"), T_INT, offsetof(ImageExport, scale), 0, imgexp_scale__doc__},
 	{const_cast<char*>("quality"), T_INT, offsetof(ImageExport, quality), 0, imgexp_quality__doc__},
+    {const_cast<char*>("transparency"), T_INT, offsetof(ImageExport, transparency), 0, imgexp_transparency__doc__},
 	{nullptr, 0, 0, 0, nullptr} // sentinel
 };
 
@@ -89,6 +92,28 @@ static int ImageExport_setName(ImageExport *self, PyObject *value, void * /*clos
 	return 0;
 }
 
+static bool ImageExport_getTransparency(ImageExport *self, void * /*closure*/)
+{
+    Py_INCREF(self->transparency);
+    return self->transparency;
+}
+
+static bool ImageExport_setTransparency(ImageExport *self, PyObject *value, void * /*closure*/)
+{
+    if (value == nullptr) {
+        PyErr_SetString(PyExc_TypeError, QObject::tr("Cannot delete image type settings.", "python error").toLocal8Bit().constData());
+        return -1;
+    }
+    if (!PyString_Check(value)) {
+        PyErr_SetString(PyExc_TypeError, QObject::tr("The image type must be a string.", "python error").toLocal8Bit().constData());
+        return -1;
+    }
+    Py_DECREF(self->transparency);
+    Py_INCREF(value);
+    self->transparency = value;
+    return 0;
+}
+
 static PyObject *ImageExport_getType(ImageExport *self, void * /*closure*/)
 {
 	Py_INCREF(self->type);
@@ -150,7 +175,10 @@ static PyObject *ImageExport_save(ImageExport *self)
 	* portrait and user defined sizes.
 	*/
 	double pixmapSize = (doc->pageHeight() > doc->pageWidth()) ? doc->pageHeight() : doc->pageWidth();
-	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), Pixmap_DrawBackground);
+    PageToPixmapFlag flags = Pixmap_DrawBackground;
+    if (self->transparency)
+        flags = Pixmap_NoFlags;
+    QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), flags);
 	int dpi = qRound(100.0 / 2.54 * self->dpi);
 	im.setDotsPerMeterY(dpi);
 	im.setDotsPerMeterX(dpi);
@@ -181,7 +209,10 @@ static PyObject *ImageExport_saveAs(ImageExport *self, PyObject *args)
 	* portrait and user defined sizes.
 	*/
 	double pixmapSize = (doc->pageHeight() > doc->pageWidth()) ? doc->pageHeight() : doc->pageWidth();
-	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), Pixmap_DrawBackground);
+    PageToPixmapFlag flags = Pixmap_DrawBackground;
+    if (self->transparency)
+        flags = Pixmap_NoFlags;
+    QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), flags);
 	int dpi = qRound(100.0 / 2.54 * self->dpi);
 	im.setDotsPerMeterY(dpi);
 	im.setDotsPerMeterX(dpi);
diff --git a/scribus/plugins/scriptplugin/objimageexport.h b/scribus/plugins/scriptplugin/objimageexport.h
index b05652000..aa99ee000 100644
--- a/scribus/plugins/scriptplugin/objimageexport.h
+++ b/scribus/plugins/scriptplugin/objimageexport.h
@@ -34,6 +34,7 @@ PyDoc_STRVAR(imgexp_quality__doc__, "Quality/compression: minimum 1 (poor), maxi
 PyDoc_STRVAR(imgexp_filename__doc__, "Filename of the image. With or without path. Read/write string.");
 PyDoc_STRVAR(imgexp_type__doc__, "Bitmap type. See allTypes list for more info. Read/write string.");
 PyDoc_STRVAR(imgexp_alltypes__doc__, "Available types. Read only list of strings.");
+PyDoc_STRVAR(imgexp_transparency__doc__, "Enable or disable transparency.");
 
 PyDoc_STRVAR(imgexp_save__doc__, "save() -> boolean\n\nSaves image under previously set 'name'.");
 PyDoc_STRVAR(imgexp_saveas__doc__, "saveAs('filename') -> boolean\n\nSaves image as 'filename'.");
transparency.patch (4,607 bytes)
png_transparent.py (314 bytes)

ale

2018-10-11 12:42

manager   ~0045511

the same patch, with tabs

transparency-2.patch (4,511 bytes)
diff --git a/scribus/plugins/scriptplugin/objimageexport.cpp b/scribus/plugins/scriptplugin/objimageexport.cpp
index 0c1bad200..1dd0eedc0 100644
--- a/scribus/plugins/scriptplugin/objimageexport.cpp
+++ b/scribus/plugins/scriptplugin/objimageexport.cpp
@@ -26,6 +26,7 @@ typedef struct
 	int dpi; // DPI of the bitmap
 	int scale; // how is bitmap scaled 100 = 100%
 	int quality; // quality/compression <1; 100>
+	bool transparency;
 } ImageExport;
 
 static void ImageExport_dealloc(ImageExport* self)
@@ -50,6 +51,7 @@ static PyObject * ImageExport_new(PyTypeObject *type, PyObject * /*args*/, PyObj
 		self->dpi = 72;
 		self->scale = 100;
 		self->quality = 100;
+		self->transparency = false;
 	}
 	return (PyObject *) self;
 }
@@ -63,6 +65,7 @@ static PyMemberDef ImageExport_members[] = {
 	{const_cast<char*>("dpi"), T_INT, offsetof(ImageExport, dpi), 0, imgexp_dpi__doc__},
 	{const_cast<char*>("scale"), T_INT, offsetof(ImageExport, scale), 0, imgexp_scale__doc__},
 	{const_cast<char*>("quality"), T_INT, offsetof(ImageExport, quality), 0, imgexp_quality__doc__},
+	{const_cast<char*>("transparency"), T_INT, offsetof(ImageExport, transparency), 0, imgexp_transparency__doc__},
 	{nullptr, 0, 0, 0, nullptr} // sentinel
 };
 
@@ -89,6 +92,28 @@ static int ImageExport_setName(ImageExport *self, PyObject *value, void * /*clos
 	return 0;
 }
 
+static bool ImageExport_getTransparency(ImageExport *self, void * /*closure*/)
+{
+	Py_INCREF(self->transparency);
+	return self->transparency;
+}
+
+static bool ImageExport_setTransparency(ImageExport *self, PyObject *value, void * /*closure*/)
+{
+	if (value == nullptr) {
+		PyErr_SetString(PyExc_TypeError, QObject::tr("Cannot delete image type settings.", "python error").toLocal8Bit().constData());
+		return -1;
+	}
+	if (!PyString_Check(value)) {
+		PyErr_SetString(PyExc_TypeError, QObject::tr("The image type must be a string.", "python error").toLocal8Bit().constData());
+		return -1;
+	}
+	Py_DECREF(self->transparency);
+	Py_INCREF(value);
+	self->transparency = value;
+	return 0;
+}
+
 static PyObject *ImageExport_getType(ImageExport *self, void * /*closure*/)
 {
 	Py_INCREF(self->type);
@@ -150,7 +175,10 @@ static PyObject *ImageExport_save(ImageExport *self)
 	* portrait and user defined sizes.
 	*/
 	double pixmapSize = (doc->pageHeight() > doc->pageWidth()) ? doc->pageHeight() : doc->pageWidth();
-	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), Pixmap_DrawBackground);
+	PageToPixmapFlag flags = Pixmap_DrawBackground;
+	if (self->transparency)
+		flags = Pixmap_NoFlags;
+	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), flags);
 	int dpi = qRound(100.0 / 2.54 * self->dpi);
 	im.setDotsPerMeterY(dpi);
 	im.setDotsPerMeterX(dpi);
@@ -181,7 +209,10 @@ static PyObject *ImageExport_saveAs(ImageExport *self, PyObject *args)
 	* portrait and user defined sizes.
 	*/
 	double pixmapSize = (doc->pageHeight() > doc->pageWidth()) ? doc->pageHeight() : doc->pageWidth();
-	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), Pixmap_DrawBackground);
+	PageToPixmapFlag flags = Pixmap_DrawBackground;
+	if (self->transparency)
+		flags = Pixmap_NoFlags;
+	QImage im = view->PageToPixmap(doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), flags);
 	int dpi = qRound(100.0 / 2.54 * self->dpi);
 	im.setDotsPerMeterY(dpi);
 	im.setDotsPerMeterX(dpi);
diff --git a/scribus/plugins/scriptplugin/objimageexport.h b/scribus/plugins/scriptplugin/objimageexport.h
index b05652000..aa99ee000 100644
--- a/scribus/plugins/scriptplugin/objimageexport.h
+++ b/scribus/plugins/scriptplugin/objimageexport.h
@@ -34,6 +34,7 @@ PyDoc_STRVAR(imgexp_quality__doc__, "Quality/compression: minimum 1 (poor), maxi
 PyDoc_STRVAR(imgexp_filename__doc__, "Filename of the image. With or without path. Read/write string.");
 PyDoc_STRVAR(imgexp_type__doc__, "Bitmap type. See allTypes list for more info. Read/write string.");
 PyDoc_STRVAR(imgexp_alltypes__doc__, "Available types. Read only list of strings.");
+PyDoc_STRVAR(imgexp_transparency__doc__, "Enable or disable transparency.");
 
 PyDoc_STRVAR(imgexp_save__doc__, "save() -> boolean\n\nSaves image under previously set 'name'.");
 PyDoc_STRVAR(imgexp_saveas__doc__, "saveAs('filename') -> boolean\n\nSaves image as 'filename'.");
transparency-2.patch (4,511 bytes)

jghali

2018-10-31 23:36

administrator   ~0045556

It took some time but now it's applied! I've made a few modifications to the patch tho, removed a few unnecessary function and most noticeably renamed the new "transparency" attribute to a more explicit "transparentBkgnd". Thanks!

ale

2018-11-01 08:19

manager   ~0045557

Bkgnd? mmm j'ach├Ęte trois vocales...

Issue History

Date Modified Username Field Change
2018-10-11 09:15 Gibbz New Issue
2018-10-11 09:15 Gibbz File Added: transparency.patch
2018-10-11 09:15 Gibbz File Added: png_transparent.py
2018-10-11 12:42 ale File Added: transparency-2.patch
2018-10-11 12:42 ale Note Added: 0045511
2018-10-31 23:29 jghali Summary [patch] Add transparency to image exporter in scripter => Add transparency to image exporter in scripter
2018-10-31 23:36 jghali Assigned To => jghali
2018-10-31 23:36 jghali Status new => resolved
2018-10-31 23:36 jghali Resolution open => fixed
2018-10-31 23:36 jghali Fixed in Version => 1.5.5.svn
2018-10-31 23:36 jghali Note Added: 0045556
2018-11-01 08:19 ale Note Added: 0045557
2019-06-04 20:56 cbradney Status resolved => closed