How to pad the location for the image on the label? - python

How to pad the location for the image on the label?
I am working on project in which the image is basically the size of the label and some text that I want to show isn't visible...when I remove the image, the label text is shown. I have to pad the location for the image on the label.

The label automatically resizes to account for the size of the image, so what you can do is, use the compound option to move the image to whichever position you prefer.
From a doc:
If you would like the Label widget to display both text and a graphic (either a bitmap or an image), the compound option specifies the relative orientation of the graphic relative to the text. Values may be any of tk.LEFT, tk.RIGHT, tk.CENTER, tk.BOTTOM, or tk.TOP. For example, if you specify compound=BOTTOM, the graphic will be displayed below the text.
So:
Label(root,text='Image 1',image=img,compound='top').pack()

Related

How to set the height of an ipywidgets.Text?

I am trying:
from ipywidgets import Text, Layout
Text('test', layout=Layout(height='15px'))
but, it doesn't shrink the height of the Text widget frame. Instead, it just shows the frame cropped. See here:
How to properly get a Text widget with a specified frame height?

Python-pptx compatibility with text boxes in LibreOffice

Just starting to try to use python-pptx, and have fallen at the first. Linux Mint 20.1, python 3.85, LibreOffice 6.4.
This is basically the 'Hello World' from the documentation.
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
print('before text ', txBox.left, txBox.top, txBox.width, txBox.height)
tf.text = "This is a long line of text inside a textbox"
print('after text ', txBox.left, txBox.top, txBox.width, txBox.height)
prs.save('test.pptx')
The text is more than a single line for the textbox. Printing out its bounds before and after text insertion shows that as far as python-pptx is concerned, the textbox width hasn't changed.
When the resulting presentation is viewed in LibreOffice, instead of wrapping the text within its boundaries, the textbox has expanded symmetrically around the mid point, pushing the start of the text off the lefthand edge of the page.
I was hoping the LibreOffice was only incompatible with powerpoint for rare edge cases, but text in text boxes is the meat and bread of presentations.
When I upload it to google slides, the text wraps within the left and right text box boundaries, but spills out of the bottom edge. The textbox shows up as 1" x 1" in the right place.
If I use onedrive.live.com, the text is left justified in the box and spills out of the righthand side without wrapping, and the textbox is shown as being 1" x 1" in the right place.
If I use onlinedocumentviewer.com, the display is the same as onedrive, though I can't get to see the text box.
Unfortunately I can't test the behaviour on a native powerpoint installation.
Maybe there's an autosize or fixed flag, which left unset leaves each viewer defaulting it in its own idiosyncratic way? How do we control text boxes / frames when targetting LibreOffice?
I possibly have a workaround to break up my text into single lines and use one per text box, but I'd rather understand the whether it can be done the proper way.
After some floundering around in the docs, I stumbled across the text_frame .word_wrap property. Setting to True gets the text to wrap in LibreOffice. While I was there, setting text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE reduces font size to get it all to fit in the box.
Is there a list of properties like .word_wrap in one place anywhere?

IPython.display: how to change width, height and resolution of a displayed image

I am displaying an image of a molecule using IPython.display in Jupyter.
The resolution of the image is quite low. Is there a way to specify the width and height of the displayed image and its resolution?
I googled it and could not find anything. All I need is something like this:
display(moleSmilemol, format='svg', width=1000, height=1000)
Any pointers would be appreciated.
Update: I could add custom css, which will blow up the picture that was generate, but it is still low quality. I am interested increasing the quality of the picture and its size too. So something deeper than CSS is needed.
Try changing the variables in the rdkit.Chem.Draw.IPythonConsole module:
from rdkit.Chem.Draw import IPythonConsole
IPythonConsole.molSize = (800, 800) # Change image size
IPythonConsole.ipython_useSVG = True # Change output to SVG
mol = Chem.MolFromSmiles('N#Cc1cccc(-c2nc(-c3cccnc3)no2)c1')
display(mol)
Otherwise, you need to use the rdMolDraw2D module to create the drawings yourself with the parameters you require.

How to save an .EPS file to PNG with transparency in Python

I'm building a Paint-like app Since I want the freedom to reposition and modify the shape properties later, I am using Tkinter to draw shapes on Canvas instead of PIL Draw or anything else. From other answers, I found how to save a canvas as PNG by 1st creating a postscript file and then converting it to PNG using PIL.
Now the problem is the EPS file has transparent spaces but the PNG file fills those voids with a White background color. I'm not sure where I am going wrong.
Below is the function I used.
def saveImg(event):
global canvas
canvas.postscript(file="my_drawing.eps", colormode='color')
imgNew = Image.open("my_drawing.eps")
imgNew.convert("RGBA")
imgNew.thumbnail((2000,2000), Image.ANTIALIAS)
imgNew.save('testImg.png', quality=90)
Looks like transparency is not supported. From the docs:
The EPS driver can read EPS images in L, LAB, RGB and CMYK mode, but Ghostscript may convert the images to RGB mode rather than leaving them in the original color space.
When you load in RGB (instead of RGBA) the alpha channel information is discarded and converting it to RGBA later will not recover it.
Your best shot is porting it to more recent toolkits like cairo or QT or converting the file using GhostScript directly as suggested by PM2Ring.
For the GS approach in order to set the width and height of the output file you must use the -rN switch where N is the resolution in PPI (pixels per inch). You must do the math in order to get target resolution from the EPS bounding box and the desired output size.
Or you can render to a fixed resolution first, lets say, 100 PPI, see the width you got and do the math in order to get the correct resolution. For example, if rendering with -r100 gives you a file 500 pixels wide but you want it to be 1024:
desired_resolution = initial_resolution * desired_width // initial_width
In order to get a file 1024 pixels wide:
>>> 100 * 1024 // 500
204
So you must render the EPS again using -r204.
Edit 1:
I got the solution from this Question
We can set custom width and height using -gNNNNxMMMM
but the dpi value crops only a small area. I tried with the usual 72dpi and I got a decent output(I'm not sure if it's perfect or not). Now I need to find how to execute this command every time when I run the program and provide the custom image size value. :\

Using Python, How can I hide a watermark inside another image using PIL?

The watermark needs to be encoded by making pixels from the image ‘even’ where the
corresponding watermark pixel is black and ‘odd’ where the corresponding watermark
pixel is white.
Function add_watermark must take two parameters, an image file name, e.g.,
StoryBridge.bmp, and a watermark file name, e.g., dog_watermark.bmp,
and it must create a new image file, e.g., StoryBridge_X.bmp, with the
watermark invisibly hidden in the image.
Function reveal_watermark accepts the name of a watermarked image file, e.g.,
BryceCanyon_X.bmp, and creates a new image file, e.g., Bryce-Canyon_X_O.bmp, that reveals the hidden watermark. Note that the watermarked
image file will be distinguished by a ‘_X’ suffix on its filename, and the new file to be
created should have an additional ‘_O’ suffix added.
How would I go about this?

Categories

Resources