Python matplotlib saving figures - python

I am not sure if I could describe this situation clearly, but let's say I have a function imported from a library:
from somelib import somefunc
somefunc(someinputs)
If I run this code on my local machine, a figure would pop up and pause the code until I manually close it. For some reason I cannot run it on my local machine so I had to run it remotely via ssh session. In this case the figure doesn't pop up anymore, I wanted to find a way to save this figure as png file and so I could download it from my remote machine to see it. I cannot modify the source files of this library. Anyone could give me some suggestions? Thanks you!

You can use multiple ways depending upon your image..
from PIL import Image
import PIL
# creating a image object (main image)
im1 = Image.open(r"C:\Users\System-Pc\Desktop\flower1.jpg")
# save a image using extension
im1 = im1.save("new.jpg")
using matplotlib
from matplotlib import pyplot as plt
plt.savefig('new.png')

Related

Use python to grab the screen(screenshot) But image is blurry, unsharp

Environment:
win10 PC notbook
python 3.9
PyQt5==5.15.4 pywin32==304 opencv-python==4.5.5.64 Pillow==9.1.0
Question:
I am trying to grab my whole screen by python. The function is easy, but I get a strange question.
The screenshot image is always blurry and unsharp.
screenshot by windows10 self: windows10 self (PNG)
screenshot by my codes: my codes (PNG)
My Codes as follows:
import cv2
import os
from PIL import ImageGrab
import numpy as np
if os.path.exists('xx.jpg'):
os.remove('xx.jpg')
captureImage = ImageGrab.grab()
width,height = captureImage.size
img = cv2.cvtColor(np.asarray(captureImage), cv2.COLOR_RGB2BGR)
cv2.imwrite('./xx.jpg',img)
cv2.waitKey(0)
I have try alternative method to achieve screenshot function, just like pyqt5/mss/pywin32/pyautogui, but every method leads to the same question.
And I google lots of articles, I found that my screen has scaled to 125%(as recommended), as follow:
PNG:
So I revise it to 100%, the screenshot is sharp. But 125% is normal and comfortable vision.
Then I doubt if it's DPI. But I try these codes, it does not work.
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
I am so crazy and curious that how those screenshot software resolve it. After all, we can not ensure every user has the 100% scale and has normal dpi.
I will be grateful if u could give me solutions or advices.
Solution:
Thanks to Christoph Rackwitz.
I had grab a PNG screen and update it and the two PNG image still same. But it's really different in my PC before upload.
I test a lot and I found that I open my codes screenshot by iQIYI Player. But I open windows10 self screenshot by windows Player.
It's the Reason!!!My codes is right and everything is right except iQIYI Player Software. Maybe it's the software bug.
Thanks again.

Importing and displaying a plotly graph saved as an html file into Jupyter Notebook

I am new to programming, so forgive the question if it seems simple.
I have been using Python and its libraries to make plots. I am attempting to import an interactive graph I plotted using the plotly graphing library and display it in the Jupyter notebook.
The graph is saved as an html file. I need the graph to remain as an html file to retain the graph's features and interactivity (zooming in/out, changing the scales, hiding certain data, etc).
So far, I have written out this bit of code:
import plotly
import os
os.chdir(r'C:\Users\alsha\Documents\CE-CERT - SIGI\CMC Data')
from IPython.display import HTML
HTML(filename="./CopperMountainCollegeEntireInterval.html")
I do not know what code to write to display the actual graph, I would appreciate any help I receive.
I am probably a year to late, but the easiest way would be to import IFrame from IPython.display and use it like
from IPython.display import IFrame
IFrame(src='path_to_html', width=500, height=500)
To complete Luca R answer (his solution works fine): In my case though, I noticed that to use IFrame, I needed to place the html file in the same directory as my notebook (or in a subdirectory under the notebook) and then use a relative path. It is probably due to common browser security settings (see more from https://community.plotly.com/t/displaying-html-file-generated-by-plotly-offline-in-a-jupyter-notebook/19586)

How to show autcompletion from objects created from imported libraries in pycharm?

I'm using Pycharm. I imported PIL Image library. I created an image object but is fails to show its corresponding methods/attributes in the autocompletion. Is there any way past this?
I've tried different interpreters, none work.
from PIL import Image
im = Image.open('resources/test.png')
im. #here it does not show autocompleting options
This happens because PyCharm doesn't know exactly the class/type of the im object.
If you add a type hint like this:
im: Image.Image = Image.open('resources/test.png')
then autocomplete will work.

Create GUI with cv2 python alone

I need to create a GUI (create a Button to select an image from directory or list the image from a particular directory and select one image for conversion)with python cv2.Because i can't install pyqt or tkinter like modules.
I have cv2 , numpy and other basic modules.How can i do this without installing any other modules ? (my device have space limitation ).
It looks like you can pass key inputs to opencv but not easily, as well as log slider events with generic functions. It doesn't look like there is a native file interface even in the latest version (https://docs.opencv.org/3.4.1/d7/dfc/group__highgui.html)
What about creating a command line GUI? You can use autocomplete in raw input as in this question and then pass the input to imread and then imshow the file.

Python: Using Pyscreenshot image to get RGB values (Linux)

I'm on python 3.5 on a Ubuntu machine. I'm writing a script where I want to grab the screen and search for certain pixel colors in the image.
Since it's not windows, PIL.ImageGrab doesn't work and after some research I started using pyscreenshot.
The following works:
import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox(1,1,100,100))
Now my problem is that the type of im is PIL.PngImagePlugin.PngImageFile which doesn't have the method .getpixel like PIL.Image does.
While I could save it to a file and load it again with PIL that seems super ugly and not efficient. How do I make a PIL.Image out of this?
I thought something along the lines of
im = Image.new(ImageGrab.grab(bbox(1,1,100,100)))
but that's obviously not it ;)
(Sidenote: If there are other/easier ways than pyscreenshot to get screenshots on Ubuntu, that's fine too)

Categories