can't show an image using PIL on google colab - image-processing

I am trying to use PIL to show an image. I know that I can use other modules to do that. I am working on google colab. But I can't figure out why PIL is not showing output image.
% matplotlib inline
import numpy as np
import PIL
im=Image.open('/content/drive/My Drive/images-process.jpeg')
print(im.width, im.height, im.mode, im.format, type(im))
im.show()
output: 739 415 RGB JPEG < class 'PIL.JpegImagePlugin.JpegImageFile'>

Instead of
im.show()
Try just
im
Colab should try to display it on its own. See example notebook

Use
display(im)
instead of im.show() or im.
When using these options after multiple lines or in a loop, im won't work.

After you open an image(which you have done using Image.open()), try converting using im.convert() to which ever mode image is in then do display(im)
It will work

Related

Displaying frames in a form of video on google colab

I am a trying video processing on google colab. My code read the video and break it into frames and after the processing on the frame I want to display the video as frames are processed. Like what cv2.imshow does (on local computer). But cv2.imshow gives error in colab so as it suggested I used cv2_imshow using from google.colab.patches import cv2_imshow . It is displaying the frames but in an column(like separate images) but replacing the previous displayed. Here is my colab link: https://colab.research.google.com/drive/1RUOGahcGngTWG9nBoisrsPzCLQ1Jq88v?usp=sharing
You can see the output at the end of the page where multiple images are.
Any help is really appreciated :)
try:
from google.colab.patches import cv2_imshow
from IPython.display import clear_output
from time import sleep
clear_output()
cv2_imshow(img)
sleep(0.1)
it's far from perfect (since there are some frame drops for some reason), but that's the closest thing I could find.

How to use TinkerPop to print out an image?

For example, given this graph:
gremlin> graph = TinkerFactory.createModern() (1)
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal(standard()) (2)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').out('knows').values('name') (3)
I want to print out a PNG file for this graph from the console. The documentation doesn't say how to do it. Thanks in advance.
TinkerPop doesn't provide graph visualization options directly. The best you can do is either:
Use the Gephi plugin to visualize your graph and then save that visualization to an image.
Export your graph to GraphML and then import it into a visualization tool, like Gephi, Cytoscape, etc. and then export an image generated from there.
If you use Python at all, the networkx and matploitlib libraries can read a GraphML file and render it. I have used this in a Python Notebook with Gremlin Python running where I extracted a subgraph to GraphML and rendered it using Python.
The code would be along the lines of:
import matplotlib
import matplotlib.pyplot as plt
import networkx as nx
G = nx.parse_graphml(mygraph)
label = nx.get_node_attributes(G, "code")
plt.figure(figsize=(11,11))
nx.draw(G, node_color="#ffaa00",node_size=1200,labels=label,arrows=False)
Apologies in advance if my Python is ugly I'm more of a Ruby and Groovy guy :-)

Tooltip on bokeh violin chart built from seaborn package

I was trying to use violin chart in Bokeh but unable to find this type of chart. So, I use seaborn library to build the violin chart and use it into bokeh.
Code:
import seaborn as sns
from bokeh import mpl
import pandas as pd
df = pd.DataFrame({"x": [1,1,1,2], "y":4,5,8,9})
sns.violinplot(x="x", y="y", data=df)
plot = mpl.to_bokeh()
plot.plot_width = 500
plot.plot_height = 300
Now, I want to add tooltip on bokeh converted chart from seaborn. I googled a lot but not able to find a way.
This is no longer possible, at least in this form. The MPL compatibilityin bokeh, including mpl.to_bokeh() was deprecated and completely removed some time ago. You might have a look at Holoviews which is a very high level API built on top of Bokeh that I believe just added a Violin plot.

saving figures seaborn to sageplot

I have been trying to save statistical graphs (boxplot, barchart, histogram etc) of some random data generated using Seaborn into LaTeX without saving them into a file first. I use \sageplot[width=8cm][png]{(Python_Graphics_Format)} from SageTex package to do this.
For example, when I draw a Box Plot using Seaborn it generates all kinds of format (name.gcf(), name.show(), name.plot(), name.draw() etc) but Graphics format. Is there any way to do this without using 'name.savefig()' or likes?
Why is it important for me?
I would like to generate a list of predefined sage functions in a separate tex file together with bunch of randomly generated data and input them on top of my TeX code after \maketitle. This way, I will be able to generate multiple problems of similar nature and upload them to the online HW system Ximera.
Here is a code that I took from stackoverflow:
import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"])
Your help is most appreciated.

Using ImageMagick/ZBar to read QR codes

I've got scanned image files that I perform some preprocessing on and get them looking something like this:
My phone's ZBar app can read this QR code fine, but zbarimg seems to be unable to figure it out. I've tried all sorts of things in ImageMagick to make it smoother (-smooth, -morphology) but even with slightly better-looking results, zbarimg still comes up blank.
Why would my phone's ZBar be so much better than my computer's (zbar-0.10)? Is there anything I can do to get zbarimg to read this successfully?
You can try morphological closing.
Python code:
# -*- coding: utf-8 -*-
import qrtools
import cv2
import numpy as np
imgPath = "Fdnm1.png"
img = cv2.imread(imgPath, 0)
kernel = np.ones((5, 5), np.uint8)
processed=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('test.png', processed)
d = qrtools.QR(filename='test.png')
d.decode()
print d.data
Result:
1MB24

Resources