I am trying to improve my code to make it more userfriendly using GUI. I have a simple code to select set of images and crop it. I have a code to that selects the folder directory containing images, now I want something similar using tkinter to select pixel range to crop images in the following code.
#Read images
root= tk.Tk()
var = tk.StringVar()
label = tk.Label( root, textvariable=var,relief = tk.RAISED,fg='peru',bg='white',font=('Helvetica',18,'bold'))
var.set("Choose image location")
label.pack()
img_list =[i for i in glob.glob(filedialog.askdirectory() +'/*.tif')] # choose the folder containing image list
images_read = np.array([(plt.imread(fname)) for fname in img_list]).astype(np.int32)
#crop_size=input()
images_crp =np.array([i[524:1024,275:775] for i in images_flp]) #crop the images to requried shape (or Pixel_row X pixel_column)
Here, I have manually selected pixel range, but I would love to have a pop up window asking me to enter the pixel range using GUI. Help in this regard is appreciated.
Thanks
Related
I have a .png image of a dice. All pixels are coloured black and I want the user to be able to change the pixel colour to a colour chosen through the tkinter colorchooser module. I am using pillow to change the colour of the image and tkinter to allow the user to choose the colour. The following code is the function I'm using.
def ColourCustomisation():
colourCode = colorchooser.askcolor(title ="Change Colour")
importD20 = Image.open("d20.png")
importD20 = importD20.convert("RGB")
datas = importD20.getdata()
new_image_data = []
for item in datas:
# change all pixels to input colour
if item[0] in list(range(0, 256)):
new_image_data.append(("colourCode"))
# update image data
importD20.putdata(new_image_data)
# save new image
importD20.save("d20.png")
ColourButton = Button(root, text = "Colour Customisation", command = ColourCustomisation)
ColourButton.grid(column=3, row=2)
The problem I run into is the format with which tkinter returns the value "colourCode". Instead of giving a RGB code it returns a RGB and Hex colour code.
For example, it returns the colour red as:
((255.99609375, 0.0, 0.0), '#ff0000')
I don't know, and cant find anything to explain how I would go about changing this output to something acceptable by pillow. I'd love some suggestions on how I can isolate the RGB code and then convert them into integers as pillow does not accept the values as floats
I want to create a Tkinter window where on the right there is an image as below.
In the left of the image, there will be an Entry box with written answers.
Users can check whether the information is correct (if the answer question 30 is 1 the Entry box should be 1, in case if it is not 1, users could fix it)
My so far code:
b,g,r = cv2.split(image)
img = cv2.merge((r,g,b))
root = tk.Tk()
im = Image.fromarray(img)
imgtk= ImageTk.PhotoImage(image=im)
image = tk.Label(root, image=imgtk).pack()
entry1 = tk.Entry(root)
entry1.insert(tk.END, 'the answer').pack()
canvas1.create_window(50,50, window = entry1)
canvas1.create_window(50,100, window = image) #I thought this would make it stood next to each other
root.mainloop()
Sorry, first time working with Tkinter, so my code may stupid
P/s: I know the shape of the image and the Y axis of every question to place the Entry box right next to it.
I am trying to add a bunch of images from a folder using a for loop using tkinter and I want each image to be placed on row = 20, but change columns for each image. So , I will want an image on (20,10), (20,11), (20,12) and so on. This works when I use grid for individual images, but I cant get it to work in a for loop where I want to add 5 images. Instead my first image starts appearing in the first row and first column but follows the column increments. Please let me know what I am doing wrong here? Thanks!
game = tkinter.Tk()
game.geometry("500x500")
images = random.sample(deck, 5)
path = "C:/Users/jhbhb/Documents/"
MAX_ROWS = 4
current_row = 20
current_column = 10
for file in images:
im = Image.open(f"{path}/{file}.png")
photo = ImageTk.PhotoImage(im)
label = tkinter.Label(image=photo)
label.image = photo
label.grid(row=current_row, column=current_column)
current_column += 1
game.mainloop()
I'm trying to copy text and images on a background image using python PIL library. I want to achieve this such that new images and text don't overlap each other or touch on bordeers, they have to be separately placed on the single background image. Image size and text size doesn't concern it can be any but its need to fit inside the background image, minimum one image and text must be there. the script I have written to achieve this as below
img = Image.open('imagefolder').resize((600,200))
b,l = img.size
width=20
height=20
while height<l-10:
hmax=0
width=10
select = random.choice([True,False])
ran_height = random.choice(range(5,55))
if select:
while width<b:
image_folder=random.choice(image_folders)
imagetemp=img.open(image_folder)
size_w,size_h=imagetemp.size
if size_h>hmax:
hmax=size_h
select_word = random.choice([True,False])
d.text([10,30], "hello", helvetica)
d.text([90,60], "python", helvetica)
if select_word:
if (width+size_w)<b-10 and (height+size_h)<l-10 :
(x, y) = (width, height)
image.paste(imagetemp,(x, y))
width = width+size_w+10
else:
height = height+hmax+10
break
else:
width =width+size_w+10
height = height+hmax+10
else:
height = height+ran_height+10
In this code ,
d.text([10,30], "hello", helvetica)
d.text([90,60], "python",helvetica)
prints text 'hello' and 'python' in the coordinate (10,30) and (90,60) respectively. and
image.paste(imagetemp,(x, y))
prints image in the coordinate of (x,y)
I want to pass a coordinate value for text like this such that it doesn't fall under the image coordinates or texts don't overlap with an image . for example
image
If I pass image coordinate value
d.text([x,y], "hello", helvetica)
texts will be printed inside the image this is not what I'm looking for, I want to print outside the image. Let me know if u have any questions. I searched online I didn't find even a single example of doing this, there are many examples for copying the text inside the image using PIL, not outside the image. Any suggestion will be helpful thanks.
**Fixed it by finding the size of a text
draw_txt.textsize(random_text, font=font)**
**Fixed it by finding the size of a text
draw_txt.textsize(random_text, font=font)**
Short question, I have 2 images. One is imported through:
Image = mpimg.imread('image.jpg')
While the other one is a processed image of the one imported above, this image is first converted from rgb to hls and then back. The outcome of this convertion gives a "list" which is different than the uint8 of the imported image.
When I'm trying to stick these images together with the function:
new_img2[:height,width:width*2]=image2
I don't see the second image in the combined image while by plotting the image through:
imgplot = plt.imshow(image2)
plt.show()
It works fine. What is the best way to convert the orignal to a "list" and then combine them or the "list" to uint8?
For some more information, the outcome has to be something like this:
enter image description here
Where the right side is black because the image I try to import in it has another type of array. The left image was an uint8 while the other is a "list". The second image is this one, which is saved from python:
enter image description here
Not sure how to do it the way you have show above but I have always been able to merge and save images as shown below!
def mergeImages(image1, image2, dir):
'''
Merge Image 1 and Image 2 side by side and delete the origional
'''
#adding a try/except would cut down on directory errors. not needed if you know you will always open correct images
if image1 == None:
image1.save(dir)
os.remove(image2)
return
im1 = Image.open(image1) #open image
im1.thumbnail((640,640)) #scales the image to 640, 480. Can be changed to whatever you need
im2 = Image.open(image2) #open Image
im1.thumbnail((640,480)) #Again scale
new_im = Image.new('RGB', (2000,720)) #Create a blank canvas image, size can be changed for your needs
new_im.paste(im1, (0,0)) #pasting image one at pos (0,0), can be changed for you
new_im.paste(im2, (640,0)) #again pasting
new_im.save(dir) #save image in defined directory
os.remove(image1) #Optionally deleting the origonal images, I do this to save on space
os.remove(image2)
After a day of searching I found out that both variables can be changed to the type of a float64. The "list" variable:
Image = np.asarray(Image)
This creates an float 64 from a List variable. While the uint8 can be changed to a float64 by:
Image2=np.asarray(Image2/255)
Than the 2 can be combined with:
totalImgage = np.hstack((Image,Image2))
Which than creates the wanted image.