使用 PIL 处理图像

时间:2021-03-10 09:00:02

标签: python math python-imaging-library

我正在尝试遵循本指南:

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

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

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

Base Texture

Scaled

Translate X

Translate Y

这是我得到的: Rotated

这是我应该得到的: Goal

这是我正在做的事情:

  1. 通过取图像的边并乘以比例来计算正方形边的长度 (512*0.438)
  2. 把正方形放在中间
  3. 通过将正方形的大小乘以 TranslateX 并将正方形向右移动 ( (512*0.438) * 0.471 ) 来计算 X 轴变换
  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))

0 个答案:

没有答案