restblogging.blogg.se

Resize image for twitter
Resize image for twitter








resize image for twitter

M圜anvas.create_text(0, 0, anchor=NW, text="original 200x200\nnow150x150")Īnd here is the 200x200 image, shrunk to 100x100 and expanded to 300x300 with this code: from tkinter import * M圜anvas.create_image(50, 50, anchor=NW, image=puppyImage) PuppyImage = resizeImage(puppyImage, 150, 150) # resized to 150px x 150px PuppyImage = PhotoImage(file="bassethound.png") # a 200px x 200px image M圜anvas = Canvas(root, width=300, height=300) Here would be example code: from tkinter import * To change the size of your image you could do something like this. Bottom line is you have to fill in the new pixel data by retrieving data from the original image. You can think of this process maybe using a line (y=mx+b where b=0) or ratio or scale factor or however you want to think about it. That function should resize an image, pixel by pixel and return a new image.īasically in a nutshell, you create an image of the desired size and must fill it in pixel by pixel with your desired colors using data from the original image. NewPhotoImage = PhotoImage(width=newWidth, height=newHeight) Here is the function and some examples: from tkinter import *ĭef resizeImage(img, newWidth, newHeight):

resize image for twitter resize image for twitter

If you want, you can reference your original image to the returned image effectively resizing your image. It returns a new instance of a PhotoImage. your image (an instance of a PhotoImage).(In this context/discussion when I refer to an image, I am referring to a PhotoImage or instance of PhotoImage.) It may be slow, but depending on your needs, it may suit you well. This is a rudimentary function that reads the image pixel by pixel simply scaling from one image to another. Here is a simple function that may suit your needs. Here is a way to resize images (PhotoImages) using just tkinter. However, other than the above reason I'd just use PIL. This is what I've been using when I need to compile a game or something I made and don't want to deal with PIL and it's compiling issues. So say our original image was 400x400 it is now effectively at 200x200. Self.canvas_image = canvas_image #or however you want to store a refernece so it's not collected as garbage in memory #Shrinks the image by a factor of 2 effectivelyĬanvas.create_image(0, 0, image = canvas_image, anchor = "nw") I wouldn't say it really resizes in a certain sense but if you look up the documentation it returns the same image but skips X amount of pixels specified in the method.Ĭanvas_image = tk.PhotoImage(file = path to some image)Ĭanvas_image = canvas_image.subsample(2, 2) #See below for more: You can use tkinter's PhotoImage => subsample method Just in case anyone comes across this for future reference, as I was looking for this myself earlier.










Resize image for twitter