Aylmaz
Posts: 4
Joined: Wed Jan 10, 2018 11:53 am

crop image problem

Fri Jan 12, 2018 4:44 pm

Hello everyone,

I am working on a project. I have a image, images resulution is 11811x11811. I have to frequently crop as 1440x900 and show fullscreen on the monitor.

My code is:

Code: Select all

import tkinter as tk
from PIL import Image, ImageTk
import RPi.GPIO as GPIO
import time

deneme=tk.Tk()
deneme.geometry("1440,900")
denem.attributes("-fullscreen, True")

a=0
b=0

while True:
	resim1=Image.open("/home/pi/Desktop/resim.jpg")
	alan=(a,b,1440,900)
	resim2=resim1.crop(alan)
	resim3=ImageTk.PhotoImage(resim2)
	goster=tk.Label(deneme, image=resim3)
	goster.place(x=0,y=0,width=1440,height=900)
	a=a+10
	b=0
	time.sleep(2)
This code don't work. What is my mistake?

User avatar
paddyg
Posts: 2339
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: crop image problem

Sat Jan 13, 2018 7:23 pm

With tk you need to use the built in loop system and get it to do regular updates using the 'after' function to recursively call a function. This is rather hacky mod of your code that works on this laptop.

Code: Select all

import tkinter as tk
from PIL import Image, ImageTk

deneme = tk.Tk()
deneme.geometry("400x400")

a=0
b=0

resim1 = Image.open("textures/planet02.png")
resim2 = resim1.crop((a, b, 400, 400))
resim3 = ImageTk.PhotoImage(resim2)
goster = tk.Label(deneme, image=resim3)
goster.place(x=0, y=0, width=400, height=400)

def update_img():
  global a, b
  resim2 = resim1.crop((a, b, 400 + a, 400 + b))
  resim3 = ImageTk.PhotoImage(resim2)
  goster.configure(image=resim3)
  goster.image = resim3
  a = (a + 10) % (resim1.width - 400) # or whatever
  b = (b + 2) % (resim1.height - 400)
  deneme.after(500, update_img) # 500ms

update_img()
deneme.mainloop()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”