简体   繁体   中英

Find new coordinates of a point after image resize

I've resized my image to newX, newY . Prior to resizing I had a point (x,y) . Now that I've resized my image I'd like to know where the point is on the new image. Sounds simple but I'm bad at math. Any ideas?

It is just a matter of ratios:

On the x-axis, you have resized by a ratio Rx = newX/oldX , and by a ratio Ry = newY/oldY on the y-axis.

Therefore, your new coordinates for point (x,y) are (Rx * x, Ry * y) .

from heatmappy import Heatmapper
from PIL import Image
import cv2
import numpy as np
from PIL import ImageFont
from PIL import ImageDraw



def generate_heatmap_data_list(coord_list):
    cumulative_data_list = []
    for lsingledata in coord_list:
        temp = []
        for x in range(lsingledata[0][0],lsingledata[1][0]):
            for y in range(lsingledata[0][1],lsingledata[1][1]):
                temp.append([x,y])    
        data = [temp[i] for i in range(len(temp)) if (i%250)<lsingledata[2]]
        cumulative_data_list += data
    return cumulative_data_list


coord = [[[774,265],[909,409],1],[[985,809],[1139,992],5],[[514,842],[803,1024],10],[[127,629],[283, 869],20],[[258,442],[429,  584],30],
             [[827,851],[980,1033],40],[[343,611],[514,753],1],[[500,358],[595,409],50],[[163,879],[334,999],15]]


data = generate_heatmap_data_list(coord)
example_img_path = r"C:\Workarea\heatmap\code_testing_pyheatmap\blue_print.jpg"
example_img = Image.open(example_img_path)
print("###", type(example_img))
width, height = example_img.size
original_dim = (width,height)
##resize_dim to plot the heatmap size
resize_dim = (1237,1036)
example_img = example_img.resize(resize_dim)
new_point_list = []
for lsingle_point in data:
    x1 = int((lsingle_point[0] * (resize_dim[0] / original_dim[0])))
    y1 = int((lsingle_point[1] * (resize_dim[1] / original_dim[1])))
    new_point_list.append([x1,y1])
heatmapper = Heatmapper()
heatmap = heatmapper.heatmap_on_img(new_point_list, example_img)
print("PIL_type: ", type(heatmap))
heatmap.save('temp.png')
######if you want to plot percentage on image
img = cv2.imread('temp.png')
print("cv2_type:", type(img))
img = cv2.putText(img, '1%', (803,341), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##FRANGRANCE
img = cv2.putText(img, '5%', (1027,919), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##COSMETICS
img = cv2.putText(img, '10%', (661,977), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##HONEY
img = cv2.putText(img, '20%', (209,765), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##AJILE
img = cv2.putText(img, '30%', (337,539), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##ANNABELLE
img = cv2.putText(img, '40%', (909,953), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##SUNGLASSES
img = cv2.putText(img, '1%', (423,707), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##VANHEUSEN
img = cv2.putText(img, '50%', (539,405), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##JALLUS
img = cv2.putText(img, '15%', (231,961), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 2, cv2.LINE_AA)##DENIM
cv2.imwrite("put_text_03_01_2022_heatmap.jpg", img)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM