Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

7/19/2024

draw image as grid on notebook

simply check below code and example result.

.


def image_grid(imgs, rows, cols):
assert len(imgs) == rows*cols
w, h = imgs[0].size
grid = Image.new('RGB', size=(cols*w, rows*h))
grid_w, grid_h = grid.size
for i, img in enumerate(imgs):
grid.paste(img, box=(i%cols*w, i//cols*h))
return grid
# read image prompt
image = Image.open("assets/images/statue.png")
depth_map = Image.open("assets/structure_controls/depth.png")
image_grid([image.resize((256, 256)), depth_map.resize((256, 256))], 1, 2)

..


Thank you.

2/25/2022

crop image with grid, python

Example is to do:

Grid 5x5 and resize 200x200

Each image is numpy type.

--

import cv2
import numpy as np

img = cv2.imread("./data/example.jpg")

def img_to_grid(img, row, col, resize_row, resize_col):
ww = [[i.min(), i.max()] for i in np.array_split(range(img.shape[0]),row)]
hh = [[i.min(), i.max()] for i in np.array_split(range(img.shape[1]),col)]
grid = [img[j:jj,i:ii,:] for j,jj in ww for i,ii in hh]
re_grid=[]
for img in grid:
resized = cv2.resize(img, (resize_row,resize_col), interpolation = cv2.INTER_AREA)
re_grid.append(resized)
return re_grid, len(ww), len(hh)

#------------------------------------
how_many_row, how_many_col = 5, 5
resize_row, resize_col = 200, 200
grid , r,c = img_to_grid(img, how_many_row, how_many_col, resize_row, resize_col)
print(type(grid), type(grid[0]), len(grid), r, c)
#------------------------------------

--


Thank you.

www.marearts.com

๐Ÿ™‡๐Ÿป‍♂️