首页 > 解决方案 > 使用 PIL 处理图像

问题描述

我正在尝试遵循本指南:

https://www.reddit.com/r/GlobalOffensiveTrade/comments/b7g538/psa_how_paint_seed_actually_works_technical/

我采用相同的样本数据,它们都匹配(比例、平移、平移、旋转)

我正在尝试制作一个正方形并将其放置在与指南中相同的位置,但是在进行更改后我将其放置在不同的位置:

基础纹理

缩放

翻译 X

翻译 Y

这就是我得到的: 旋转

这就是我应该得到的: 目标

这就是我正在做的事情:

  1. 通过取图像的边并乘以比例 (512*0.438) 来计算正方形边的长度
  2. 把正方形放在中间
  3. 通过将正方形的大小乘以 TranslateX 计算 X 轴变换并将正方形向右移动 ( (512*0.438) * 0.471 )
  4. 对 Y 轴执行相同操作 ( (512*0.438) * 0.623 )
  5. 将图像旋转给定的 96.7 度

这是我的代码:

data = {
    "pattern": {
        "scale": 0.438,
        "transformation_x": 0.47135912,
        "transformation_y": 0.62364169,
        "rotation": 96.70385945
    }
}
 
image = Image.open(path)
width, height = image.size
 
# base
lt = (0, 0)
rt = (width, 0)
 
lb = (0, height)
rb = (width, height)
 
# scale
scale = data["pattern"]["scale"]
center = (width/2, height/2)
 
lt = (center[0]-(width*scale)/2, center[1]-(height*scale)/2)
rt = (center[0]+(width*scale)/2, center[1]-(height*scale)/2)
 
lb = (center[0]-(width*scale)/2, center[1]+(height*scale)/2)
rb = (center[0]+(width*scale)/2, center[1]+(height*scale)/2)
 
# transform x
lt = (lt[0]+data["pattern"]["transformation_x"] * width * scale, lt[1])
rt = (rt[0]+data["pattern"]["transformation_x"] * width * scale, rt[1])
 
lb = (lb[0]+data["pattern"]["transformation_x"] * width * scale, lb[1])
rb = (rb[0]+data["pattern"]["transformation_x"] * width * scale, rb[1])
 
# transform y
lt = (lt[0], lt[1]+data["pattern"]["transformation_y"] * height * scale)
rt = (rt[0], rt[1]+data["pattern"]["transformation_y"] * height * scale)
 
lb = (lb[0], lb[1]+data["pattern"]["transformation_y"] * height * scale)
rb = (rb[0], rb[1]+data["pattern"]["transformation_y"] * height * scale)
 
# rotation
rotated = rotated.rotate(data["pattern"]["rotation"], expand=1, fillcolor=(255, 255, 255))

标签: pythonmathpython-imaging-library

解决方案


推荐阅读