How to transfer python canvas into a png image in python [duplicate] - python

I want to make canvas and user will draw some picture in that which I want to save as Image. I am using Python 3.3.2. It doesn't support PIL or Image module. Can someone guide me on this..?
Thanks.

Maybe it's a bit too late to answer this question. But, I'll still go ahead.
from tkinter import *
import pyscreenshot as ImageGrab
r=Tk()
canvas = Canvas(r,height=1000,width=2000,bg="snow")
def getter():
x2=r.winfo_rootx()+canvas.winfo_x()
y2=r.winfo_rooty()+canvas.winfo_y()
x1=x2+canvas.winfo_width()
y1=y2+canvas.winfo_height()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("./test.jpg")
b1=Button(r,text="Save",command=lambda:getter())
b1.grid()
Let me know if this doesn't work. Hope it helps..

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.

Tkinter Background Image not showing up

I am trying to create a weather App in Tkinter GUI. It is working fine. I want to add a background image to it but it wont show up. initially i did it without the reference line but it didnt work. On some website they said to keep reference of the image but that too didnt work.
Also my Tkinter window size is 1920x1080 and the image was of same dimension but it still doesnt show up. I tried to reduce image size than window but still not working. Any suggestions accepted. Also there is no error.
bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
Its sad that tkinter.PhotoImage does not support JPEG files, but it does support PNG in the newer version and has proper support for GIF too. To use JPEG you need PIL installed.
In the terminal say:
pip install Pillow
After that import it like:
from PIL import Image,ImageTk
Then now, to open the image with PIL, say:
img_file = Image.open('clearsky2.jpg')
bg = ImageTk.PhotoImage(img_file)
bgl = Label(gui,image=bg)
bgl.place(x=0, y=0, relwidth=1,relheight=1)
This will work with JPEG and PNG files as well and, keep in mind you dont need to keep a reference unless your looping over images or creating image inside of a function.
Hope it cleared your problem, do let me know if any more errors
Cheers

Take "screenshot" of application window/label in tkinter

I am working on some sort of animation maker with tkinter but I have run into a problem, i would want to save the current application window or canvas widget to a .png file for later use, is this possible? Thanks in advance.
import pyautogui
myScreenshot = pyautogui.screenshot()
myScreenshot.save(r'Path to save screenshot\filename.png')
And there it is ! ^^
And if you don't want everything else on your screen, you can try to crop it with pil
How to crop an image using PIL?

Loading an image from filename in a wx.Panel

I'm seeking a sample code to load a PNG image inside a wx.Panel, I've seen a couple of them using Python Imaging Library (PIL) and a hug bunch of code line.
I would like to keep my environment with as few library as possible, which means without PIL since I don't need to do any kind of image processing and I suppose that wx enable such processing.
Thanks
EDIT : code to achieve this from Mike's answer
image = wx.Image('path/to/image.png', wx.BITMAP_TYPE_ANY)
imageBitmap = wx.StaticBitmap(myPanel, wx.ID_ANY, wx.BitmapFromImage(image))
Then imageBitmap can be used as any other wx widget.
I wrote a really simple image viewer tutorial here that might help you: http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/ It just uses wxPython, I think.

Basic inserting of image? Python

I use python on Ubuntu, and I am (as my name suggests) a complete noob at it all. I want to insert a very basic image, I have looked around, copied coding etc, but mothing seems to work. I want to insert an image, nothing else, so it just appears on the screen when I run it in shell. Please help? and please explain in simple terms, i'd like to know what I'm actually doing! Thank-you in advance.
you can install PIL imagemagick
then:
import Image
im = Image.open("your_image")
im.show()
I would try something like:
import webbrowser
webbrowser.open('file:///path/to/my/file.jpg')

Categories