Create an image consisting of boxes in 2 colours using python
I have an array
arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]]
I want to create an image that makes a green box where there is a 1 and a red box where there is a 0
P.S. I do not want to make a plot, but an image.
Using Tkinter, I am getting all the boxes in a single row, but I need it to be arranged in rows and columns like in the 2D array. In my code, I am not able to figure how to move to a new line after printing the boxes corresponding to a row
import tkinter as tk root = tk.Tk() arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]] for i in range(len(arr)): for j in range(len(arr[i])): if arr[i][j]==0: w = tk.Label(root, text="red", bg="red", fg="white") w.pack(padx=5, pady=10) elif arr[i][j]==1: w = tk.Label(root, text="green", bg="green", fg="black") w.pack(padx=5, pady=20) w.pack(side=tk.LEFT) # need something here to move to a new line after printing each row tk.mainloop()
Current Output :
Tkinter grid() Method:
The Grid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each "cell" in the resulting table can hold a widget.
use the grid method to tell the manager in which row and column to place them. You don’t have to specify the size of the grid beforehand; the manager automatically determines that from the widgets in it.
root = tk.Tk() arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]] for i in range(len(arr)): for j in range(len(arr[i])): if arr[i][j]==0: w = tk.Label(root, text="red", bg="red", fg="white").grid(row=i, column=j) elif arr[i][j]==1: w = tk.Label(root, text="green", bg="green", fg="black").grid(row=i, column=j) tk.mainloop()
Note that the widgets are centered in their cells. You can use the sticky option to change this; this option takes one or more values from the set N, S, E, W.
example:To align the labels to the left border, you could use W (west)
in your case:
w = tk.Label(root, text="red", bg="red", fg="white").grid(row=i, column=j, sticky=(W, E))
Image Processing with Python, Explain how a digital image is composed of pixels. So, values in a computer are stored using a binary, or base-2 place-value number system. Now, if we zoomed in close enough to see the pixels in the red box, we would see Digital images use some color model to create a broad range of colors from a small set of� To convert a color image into a grayscale image, use the BGR2GRAY attribute of the cv2 module. This is demonstrated in the example below: Import the cv2 module: import cv2. Read the image: img = cv2.imread("pyimg.jpg") Use the cvtColor() method of the cv2 module which takes the original image and the COLOR_BGR2GRAY attribute as an argument.
You should use
.grid() layout manager
for i in range(len(arr)): #rows in arr for j in range(len(arr[i])): #cols in that row if arr[i][j]==0: w = tk.Label(root, text="red", bg="red", fg="white") elif arr[i][j]==1: w = tk.Label(root, text="green", bg="green", fg="black") w.grid(row = i, col = j)
This puts your label in a "2-Dimensional Table" consisting of rows and columns.
There is also
pady for padding Comment if something can be improved.
Image Manipulation, Enter Python. Pillow is a third-party Python module for interacting with image files . Images also have an alpha value to create RGBA values. If an image is Table 17-1 lists a selection of standard color names and their values. For example, the box tuple (3, 1, 9, 6) represents all the pixels in the black box in Figure 17-2. I want to write a python script which will resize them to a fixed size (say 800*800) but will keep the original image at the center and fill the excess area with a fixed color (say black). I am using PIL. I can resize the image using the resize function now, but that changes the aspect ratio. Is there any way to do this?
grid() layout instead of
pack(), you can also use
place() layout but in that you've to do a lot of work. Also the size of the labels will be different and will not fill the whole cell of a grid, to do that use
sticky = 'nsew', this will stretch the
Label to fill the cell.
Here is the code
enumerate() function it returns the index of the of the array.
If you want to have all label's object then you can create a list or dictionary to access each of them separately. For example create a
list in the beginning and do
list.append(L) in the loop to each
Label for future reference.
import tkinter as tk arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]] root = tk.Tk() for row , ar in enumerate(arr): for col, color in enumerate(ar): bg = 'green' if color else 'red' fg = 'black' if color else 'white' L = tk.Label(root, text=bg, bg=bg, fg=fg) L.grid(row=row, column=col, sticky='nswe') root.mainloop()
1. Nested Loops and Image Processing — Computer Science 20 , You need to save the Python file and the image file in the same directory on your This means that if there are 20 rows, they will be named 0,1,2, and so on through 19. This technique for creating color is known as the RGB Color Model. However, the pixel collection consists of only white pixels when it is created. Box Plot with plotly.express¶ Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. In a box plot created by px.box, the distribution of the column given as y argument is represented.
7.8. Nested Iteration: Image Processing — Foundations of Python , This technique for creating color is known as the RGB Color Model. However, the pixel collection consists of only “White” pixels. Line 2 opens the file and uses the contents to create an image object that is referred to by img . What would the image produced from ActiveCode box 16 look like if you replaced the lines:. Pasting an image on another image: The second argument can be a 2-tuple (specifying the top left corner), or a 4-tuple (left, upper, right, lower) – in this case the size of pasted image must match the size of this box region, or None which is equivalent to (0, 0).
Drawing in Tkinter - lines, shapes, colours, text, image, simple drawing. We draw lines, shapes, text, and image on the canvas widget. It can be used to create charts, custom widgets, or create games. We have a line consisting of alternating segments of 4 px dash and 2 px space. The coordinates are again the bounding box of the rectangle to be drawn. Final image containing only boxes. Now we will apply findContours() method to this image. This will find all the boxes and we will sort them from top to bottom.For sorting the contours we will use
It is up to you if you want to use floats or flex to create a three-column layout. However, if you need support for IE10 and down, you should use float. Tip: To learn more about the Flexible Box Layout Module, read our CSS Flexbox chapter.
pack()you would have to create separated
Framefor every row.
- This worked pretty well for me using the sticky option. I am now trying to save the image created inside the canvas (this grid of green and red boxes). Tried using postscript function, but doesnt seem to work. Any ideas ?
- u can use: from PIL import Image, ImageDraw