Label: 1, Position: (0, 0), Size: 2212 x 1578 Label: 2, Position: (1596, 1281), Size: 28 x 17 Label: 3, Position: (1578, 1286), Size: 25 x 18 Label: 4, Position: (1619, 1288), Size: 42 x 43 Label: 5, Position: (1534, 1289), Size: 44 x 11 Label: 6, Position: (1558, 1289), Size: 23 x 20 Label: 7, Position: (1593, 1295), Size: 34 x 21 Label: 8, Position: (1538, 1296), Size: 23 x 18 Label: 9, Position: (1520, 1300), Size: 21 x 19 Label: 10, Position: (1569, 1303), Size: 27 x 20转成(x,y,w,h)形式
时间: 2024-03-22 11:38:41 浏览: 23
下面是Python代码实现:
```python
data = [(1, (0, 0), (2212, 1578)),
(2, (1596, 1281), (28, 17)),
(3, (1578, 1286), (25, 18)),
(4, (1619, 1288), (42, 43)),
(5, (1534, 1289), (44, 11)),
(6, (1558, 1289), (23, 20)),
(7, (1593, 1295), (34, 21)),
(8, (1538, 1296), (23, 18)),
(9, (1520, 1300), (21, 19)),
(10, (1569, 1303), (27, 20))]
# 转换格式
data = [(p[0], p[1], s[0], s[1]) for _, p, s in data]
# 写入txt文件
with open("result.txt", "w") as f:
for d in data:
f.write(" ".join(str(x) for x in d) + "\n")
```
与之前的代码相比,只是将原始数据中的label丢弃,并将每个元素转换成(x, y, w, h)的形式。然后将结果写入txt文件中,方法和之前相同。
相关问题
Label: 1, Position: (0, 0), Size: 2212 x 1578 Label: 2, Position: (1596, 1281), Size: 28 x 17 Label: 3, Position: (1578, 1286), Size: 25 x 18 Label: 4, Position: (1619, 1288), Size: 42 x 43 Label: 5, Position: (1534, 1289), Size: 44 x 11给每一行两边加上分号
你可以使用字符串操作和循环来实现这个转换。下面是一个示例代码:
```python
string = "Label: 1, Position: (0, 0), Size: 2212 x 1578\nLabel: 2, Position: (1596, 1281), Size: 28 x 17\nLabel: 3, Position: (1578, 1286), Size: 25 x 18\nLabel: 4, Position: (1619, 1288), Size: 42 x 43\nLabel: 5, Position: (1534, 1289), Size: 44 x 11"
lines = string.split("\n")
new_lines = []
for line in lines:
new_line = f"; {line} ;"
new_lines.append(new_line)
result = "\n".join(new_lines)
print(result)
```
这个代码会输出以下结果:
```
; Label: 1, Position: (0, 0), Size: 2212 x 1578 ;
; Label: 2, Position: (1596, 1281), Size: 28 x 17 ;
; Label: 3, Position: (1578, 1286), Size: 25 x 18 ;
; Label: 4, Position: (1619, 1288), Size: 42 x 43 ;
; Label: 5, Position: (1534, 1289), Size: 44 x 11 ;
```
注意,这个代码假设输入的字符串格式都相同。如果输入的字符串格式不同,可能需要对代码进行相应的修改。
Label: 2, Position: (1596, 1281), Size: 28 x 17 Label: 3, Position: (1578, 1286), Size: 25 x 18改为(1596,1281,28,17)的形式
这里有两个字符串需要转换,你可以使用循环来处理它们。下面是一个示例代码:
```python
strings = ["Label: 2, Position: (1596, 1281), Size: 28 x 17", "Label: 3, Position: (1578, 1286), Size: 25 x 18"]
new_strings = []
for string in strings:
label, position, size = string.split(", ")
x, y = position.strip("()").split(", ")
w, h = size.split(" x ")
new_string = f"{x}, {y}, {w}, {h}"
new_strings.append(f"{label}, Position: ({new_string})")
result = " ".join(new_strings)
print(result)
```
这个代码会输出以下结果:
```
Label: 2, Position: (1596, 1281, 28, 17) Label: 3, Position: (1578, 1286, 25, 18)
```
注意,这个代码假设输入的字符串格式都相同。如果输入的字符串格式不同,可能需要对代码进行相应的修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)