How do I add transparency to shape in python pptx? - python

def new_presentation():
prs=Presentation()
img="C:/Users/Dennis/Desktop/Tom-Hiddleston-4-1024x768.jpg"
mainsld=new_slide(prs, 6)
mainshp=mainsld.shapes
mainshp.add_picture(img, 0,0, Inches(10))
titleshape=mainshp.add_shape(MSO_SHAPE.RECTANGLE, Inches(0), Inches(1), Inches(10), Inches(1))
titleshape.fill.solid()
titleshape.fill.fore_color.rgb=RGBColor(0x00,0x64,0x00)
titleshape.fill.transparency = 0.25 ##doesnt work???##########################
titleshape.line.fill.background()
titlebox=mainshp.add_textbox(Inches(1), Inches(0.8),Inches(1), Inches(1)).text_frame.add_paragraph()
titlebox.text="TOM HIDDLESTON"
titlebox.font.name="Calibri"
titlebox.font.size=Pt(36)
titlebox.font.color.rgb=RGBColor(0x90,0x90,0x00)
prs.save('test.pptx')
The line marked with "######s" is supposed to make the shape more transparent as written in the pptx documentation - it is a shape.fill property . Everything else in the code works perfectly. I'm using python 2.7 and latest pptx. Thank you for your help in advance.

After much digging, I have been able to come up with a solution for this
from pptx import Presentation
from pptx.oxml.xmlchemy import OxmlElement
from pptx.util import Cm
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
def SubElement(parent, tagname, **kwargs):
element = OxmlElement(tagname)
element.attrib.update(kwargs)
parent.append(element)
return element
def _set_shape_transparency(shape, alpha):
""" Set the transparency (alpha) of a shape"""
ts = shape.fill._xPr.solidFill
sF = ts.get_or_change_to_srgbClr()
sE = SubElement(sF, 'a:alpha', val=str(alpha))
## Create presentation
prs = Presentation()
## Add a slide (empty slide layout)
slide = prs.slides.add_slide(prs.slide_layouts[6])
##Add a blue box to the slide
blueBox = slide.shapes.add_shape(autoshape_type_id=MSO_SHAPE.RECTANGLE,
left=Cm(0),
top=Cm(0),
height=Cm(10),
width=Cm(20))
## Make the box blue
blueBoxFill = blueBox.fill
blueBoxFill.solid()
blueBoxFillColour = blueBoxFill.fore_color
blueBoxFillColour.rgb = RGBColor(0,176,240)
## Set the transparency of the blue box to 56%
_set_shape_transparency(blueBox,44000)
## Save the presentation
prs.save(your_path)

FillFormat.transparency is not implemented yet. The part of the documentation where you saw that may have been an analysis page, which is a precursor to development.
This is the analysis page:
http://python-pptx.readthedocs.io/en/latest/dev/analysis/features/dml-fill.html?highlight=transparency
This is the FillFormat (.fill) API, as developed:
http://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects
You can however use lxml calls from the FillFormat object to manipulate the XML under it. You probably want to start with the spPr element in the .fill element:
spPr = titleshape.fill._xPr
print spPr.xml
Here's one example of doing that sort of thing:
https://groups.google.com/forum/#!msg/python-pptx/UTkdemIZICw/qeUJEyKEAQAJ
You'll find more if you search on various combinations of the terms python-pptx, OxmlElement, lxml, and workaround function.

Related

Vedo: Is there a way to add a camera to scenes and see images from perspective?

I'm using Vedo in Python to visualize some 3D scans of indoor locations.
I would like to, e.g., add a 'camera' at (0,0,0), look left 90 degrees (or where ever), and see the camera's output.
Can this be done with Vedo? If not, is there a different python programming framework where I can open .obj files and add a camera and view through it programmatically?
I usually use schema:
...
plt = Plotter(bg='bb', interactive=False)
camera = plt.camera
plt.show(actors, axes=4, viewup='y')
for i in range(360):
camera.Azimuth(1)
camera.Roll(-1)
plt.render()
...
plt.interactive().close()
Good Luck
You can plot the same object in an embedded renderer and control its behaviour via a simple callback function:
from vedo import *
settings.immediateRendering = False # can be faster for multi-renderers
# (0,0) is the bottom-left corner of the window, (1,1) the top-right
# the order in the list defines the priority when overlapping
custom_shape = [
dict(bottomleft=(0.00,0.00), topright=(1.00,1.00), bg='wheat', bg2='w' ),# ren0
dict(bottomleft=(0.01,0.01), topright=(0.15,0.30), bg='blue3', bg2='lb'),# ren1
]
plt = Plotter(shape=custom_shape, size=(1600,800), sharecam=False)
s = ParametricShape(0) # whatever object to be shown
plt.show(s, 'Renderer0', at=0)
plt.show(s, 'Renderer1', at=1)
def update(event):
cam = plt.renderers[1].GetActiveCamera() # vtkCamera of renderer1
cam.Azimuth(1) # add one degree in azimuth
plt.addCallback("Interaction", update)
interactive()
Check out a related example here.
Check out the vtkCamera object methods here.

Failing to place an image with pylatex

I am using pylatex to create a pdf with an image in it at the coordinates that I specify. I have used the code below but no matter what coordinates I enter, the image is always in the upper left corner. Please help me.
from pylatex import (Document, TikZ,
TikZNode, TikZCoordinate,
TikZOptions, StandAloneGraphic, NoEscape)
geometry_options = {"margin": "0cm"}
doc = Document(documentclass='standalone',document_options=('tikz'), geometry_options=geometry_options)
doc.append(NoEscape(r'\noindent'))
with doc.create(TikZ()) as pic:
# img = TikZNode(text='\includegraphics[width=0.8\\textwidth]{example-image-a}',
# at = TikZCoordinate(3,4),
# options=TikZOptions('inner sep=0pt,anchor=south west'))
img = TikZNode(text=StandAloneGraphic('example-image-a').dumps(),
at = TikZCoordinate(1,2),
options=TikZOptions('inner sep=0pt')
)
pic.append(img)
tex = doc.dumps()
doc.generate_pdf('basic',compiler='lualatex', clean_tex=False)
doc.generate_tex()
Tex-Code:
\documentclass[tikz]{standalone}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{geometry}%
\geometry{margin=0cm}%
\usepackage{tikz}%
%
%
%
\begin{document}%
\normalsize%
\noindent%
\begin{tikzpicture}%
\node[inner sep=0pt] at (1.0,2.0) {\includegraphics[width=0.8\textwidth]{example-image-a}};%
\end{tikzpicture}%
\end{document}
This looks pretty similar to the code in this post to me:
https://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz
The size of the tikz picture will adapt to the content and it is normally placed wherever you use it on the page, just as if you would write a normal letter.
You can see this if you add another, bigger element, to your picture. If you now play around with the coordinates of your node, you will see the image moving around.
Normally you could also position your nodes relative to the paper with the [remember picture, overlay] option, but you are using the standalone class, which will automatically adapt the paper size to the content, so in your case this does not help.
\documentclass[tikz]{standalone}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
%\usepackage{geometry}%
%\geometry{margin=0cm}%
\usepackage{tikz}%
%
%
%
\begin{document}%
\normalsize%
\noindent%
\begin{tikzpicture}%
\path (-20,-20) rectangle (20,20);
\node[inner sep=0pt] at (3.0,2.0) {\includegraphics[width=0.8\textwidth]{example-image-a}};%
\end{tikzpicture}%
\end{document}

Why does ReportLab not draw my lines where I want it to?

sorry for the title. I'm not quite sure what the problem is, so I'm unable to express it more precisely.
I'm trying to replicate an existing form (only available on paper) using Python in ReportLab.
I measure the existing form and use the values to draw using ReportLab. Here is an example:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.units import mm
def test():
c = canvas.Canvas('test.pdf', pagesize=landscape(A4))
h, w = A4
c.line(21*mm, 37*mm, 278*mm, 37*mm)
c.showPage()
c.save()
When I print this file (no scaling) and measure it, the line is not where it is supposed to be. It is horizontally offset to the right by about 5mm and vertically offset towards the top by about 3mm.
Why is that? Is there some border around the canvas/page that is not documented in the manual? Is the line wider than its visual representation?
I know that I can accomodate for this error by simply adding the offset in my code, but this is an already tedious task and I want to make sure that I am not missing some issue inherent to ReportLab or PDF generation in general.
Draw a ruler in a page A4 or in pagesize as you mentioned and you can see these coordinates in your page and then you can draw line where ever you needed in your page based on plotted coordinates
def drawMyRuler(c):
c.drawString(100,810, 'x100')
c.drawString(200,810, 'x200')
c.drawString(300,810, 'x300')
c.drawString(400,810, 'x400')
c.drawString(500,810, 'x500')
c.drawString(10,100, 'y100')
c.drawString(10,200, 'y200')
c.drawString(10,300, 'y300')
c.drawString(10,400, 'y400')
c.drawString(10,500, 'y500')
c.drawString(10,600, 'y600')
c.drawString(10,700, 'y700')
c.drawString(10,800, 'y800')
def test():
c = canvas.Canvas('test.pdf', pagesize=landscape(A4))
h, w = A4
drawMyRuler(c)
#remaining code as per drawMyRuler coordinates...

Updating data of an Image Plane Widget

I am working on a visualization of different vector fields.
For this purpose I am using the Mayavi Library in Python 2.7 (I think), to create a Image Plane Widget (IPW) and a slider to change the data of the vector field while the visualization is open, but my IPW won't change.
It works if I render the IPW new each time the slider is changed, but that's not what I want.
Is there a way to change the data of an IPW while the program is running without rendering a new Plane each time?
I have written following code:
import numpy as np
from mayavi import mlab
from matplotlib.scale import scale_factory
from traits.api import HasTraits, Range, Instance, Array, \
on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.pipeline_base import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor, \
MlabSceneModel
class Modell(HasTraits):
p = Array
n = Range(0, 9, 5)
#p is a 4 dimensional Array p[10][20][20][20]
scene = Instance(MlabSceneModel, ())
plot = Instance(PipelineBase)
#on_trait_change('n,scene.activated')
def update_plot(self):
self.src = mlab.pipeline.scalar_field(self.p[self.n])
if self.plot is None:
self.plot = self.scene.mlab.pipeline.image_plane_widget(self.src,
plane_orientation='z_axes',
slice_index=10,
vmin=0, vmax=120)
else:
'''here should be the update function, i tried using mlab_source.set(self.src=self.src)
and all variations of expressions in the brackets but nothing worked.
I also searched for functions in IPW itself but didn't find something that could help me.'''
#The layout of the dialog created
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=400, width=400, show_label=False),
Group('_', 'n'),
resizable=True,
)
my_model = Modell(p=p)
my_model.configure_traits()
I tried updating the pipeline and updating the data with self.plot.update_pipeline() and self.plot.update_data() but this doesn't work either.
Ok I found the solution for my problem, the trick is to change the data directly through the pipeline. So in my code I just have to set the following command into the else segment:
self.plot.parent.parent.scalar_data = self.p[self.n]

OpenGL Texturing - some jpg's are being distorted in a strange way

I am trying to draw a textured square using Python, OpenGL and GLFW.
Here are all the images I need to show you.
Sorry for the way of posting images, but I don't have enough reputation to post more than 2 links (and I can't even post a photo).
I am getting this:
[the second image from the album]
Instead of that:
[the first image from the album]
BUT if I use some different jpg files:
some of them are being displayed properly,
some of them are being displayed properly until I rotate them 90 degrees (I mean using numpy rot90 function on an array with RGB components) and then send them to the GPU. And it looks like that (colors don't change, I only get some distortion):
Before rotation:
[the third image from the album]
After rotation:
[the fourth image from the album]
It all depends on a file.
Does anybody know what I do wrong? Or see anything that I don't see?
Code:
First, I do the thing with initializing glfw, creating a window, etc.
if __name__ == '__main__':
import sys
import glfw
import OpenGL.GL as gl
import numpy as np
from square import square
from imio import imread,rgb_flag,swap_rb
from txio import tx2gpu,txrefer
glfw.glfwInit()
win =glfw.glfwCreateWindow(800,800,"Hello")
glfw.glfwMakeContextCurrent(win)
glfw.glfwSwapInterval(1)
gl.glClearColor(0.75,0.75,0.75,1.0)
Then I load an image using OpenCV imread function and I remember about swapping red with blue. Then I send the image to gpu - I will describe tx2gpu in a minute.
image = imread('../imtools/image/ummagumma.jpg')
if not rgb_flag: swap_rb(image)
#image = np.rot90(image)
tx_id = tx2gpu(image)
The swap_rb() function (defined in a different file, imported):
def swap_rb(mat):
X = mat[:,:,2].copy()
mat[:,:,2] = mat[:,:,0]
mat[:,:,0] = X
return mat
Then comes the main loop (in a while I will describe txrefer and square):
while not glfw.glfwWindowShouldClose(win):
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
txrefer(tx_id); square(2); txrefer(0)
glfw.glfwSwapBuffers(win)
glfw.glfwPollEvents()
And here is the end of the main function:
glfw.glfwDestroyWindow(win)
glfw.glfwTerminate()
NOW IMPORTANT THINGS:
A function that defines a square looks like that:
def square(scale=1.0,color=None,solid=True):
s = scale*.5
if type(color)!=type(None):
if solid:
gl.glBegin(gl.GL_TRIANGLE_FAN)
else:
gl.glBegin(gl.GL_LINE_LOOP)
gl.glColor3f(*color[0][:3]); gl.glVertex3f(-s,-s,0)
gl.glColor3f(*color[1][:3]); gl.glVertex3f(-s,s,0)
gl.glColor3f(*color[2][:3]); gl.glVertex3f(s,s,0)
gl.glColor3f(*color[3][:3]); gl.glVertex3f(s,-s,0)
else:
if solid:
gl.glBegin(gl.GL_TRIANGLE_FAN)
else:
gl.glBegin(gl.GL_LINE_LOOP)
gl.glTexCoord2f(0,0); gl.glVertex3f(-s,-s,0)
gl.glTexCoord2f(0,1); gl.glVertex3f(-s,s,0)
gl.glTexCoord2f(1,1); gl.glVertex3f(s,s,0)
gl.glTexCoord2f(1,0); gl.glVertex3f(s,-s,0)
gl.glEnd()
And texturing functions look like that:
import OpenGL.GL as gl
unit_symbols = [
gl.GL_TEXTURE0,gl.GL_TEXTURE1,gl.GL_TEXTURE2,
gl.GL_TEXTURE3,gl.GL_TEXTURE4,
gl.GL_TEXTURE5,gl.GL_TEXTURE6,gl.GL_TEXTURE7,
gl.GL_TEXTURE8,gl.GL_TEXTURE9,
gl.GL_TEXTURE10,gl.GL_TEXTURE11,gl.GL_TEXTURE12,
gl.GL_TEXTURE13,gl.GL_TEXTURE14,
gl.GL_TEXTURE15,gl.GL_TEXTURE16,gl.GL_TEXTURE17,
gl.GL_TEXTURE18,gl.GL_TEXTURE19,
gl.GL_TEXTURE20,gl.GL_TEXTURE21,gl.GL_TEXTURE22,
gl.GL_TEXTURE23,gl.GL_TEXTURE24,
gl.GL_TEXTURE25,gl.GL_TEXTURE26,gl.GL_TEXTURE27,
gl.GL_TEXTURE28,gl.GL_TEXTURE29,
gl.GL_TEXTURE30,gl.GL_TEXTURE31]
def tx2gpu(image,flip=True,unit=0):
gl.glActiveTexture(unit_symbols[unit])
texture_id = gl.glGenTextures(1)
gl.glBindTexture(gl.GL_TEXTURE_2D,texture_id)
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_WRAP_S,gl.GL_REPEAT)
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_WRAP_T,gl.GL_REPEAT)
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MAG_FILTER,gl.GL_LINEAR)
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MIN_FILTER,gl.GL_LINEAR)
yres,xres,cres = image.shape
from numpy import flipud
gl.glTexImage2D(gl.GL_TEXTURE_2D,0,gl.GL_RGB,xres,yres,0,gl.GL_RGB,gl.GL_UNSIGNED_BYTE,flipud(image))
gl.glBindTexture(gl.GL_TEXTURE_2D,0)
return texture_id
def txrefer(tex_id,unit=0):
gl.glColor4f(1,1,1,1);
gl.glActiveTexture(unit_symbols[unit])
if tex_id!=0:
gl.glEnable(gl.GL_TEXTURE_2D)
gl.glBindTexture(gl.GL_TEXTURE_2D,tex_id)
else:
gl.glBindTexture(gl.GL_TEXTURE_2D,0)
gl.glDisable(gl.GL_TEXTURE_2D)
The problem you have there are alignment issues. OpenGL initial alignment setting for "unpacking" images is that each row starts on a 4 byte boundary. This happens if the image width is not a multiple of 4 or if there are not 4 bytes per pixel. But it's easy enough to change this:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
would probably do the trick for you. Call it right before glTex[Sub]Image.
Another thing: Your unit_symbols list is completely unnecessary. The OpenGL specification explicitly says that GL_TEXTUREn = GL_TEXTURE0 + n. You can simply do glActiveTexture(GL_TEXTURE0 + n). However when loading a texture image the unit is completely irrelevant; the only thing it may matter is, that loading a texture only goes with binding one, which happens in a texture unit; a texture can be bound in any texture unit desired.
Personally I use the highest texture unit for loading images, to avoid accidently clobbering required state.

Categories