XMWAI 0.2.7__py3-none-any.whl → 0.2.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of XMWAI might be problematic. Click here for more details.

XMWAI/__init__.py CHANGED
@@ -2,6 +2,9 @@ from .core import story, photo, reply, poem, crack # 从子模块导入函数
2
2
  from .magic_core import birthday # 从子模块导入函数到顶层
3
3
  from .bomb_core import bomb # 从子模块导入函数到顶层
4
4
  from .idiom_core import idiom, searchIdiom, get_json_path # 从子模块导入函数到顶层
5
+ from .trial_class import make, get_file_content_as_base64, save_pic, detect_windows_scale, get_file_content_as_base64_2, cartoon # 从子模块导入函数到顶层
5
6
 
6
7
  __all__ = ["story", "photo", "reply", "poem", 'birthday', 'bomb',
7
- "idiom", "searchIdiom", "get_json_path", "crack"] # 可选:明确导出的内容
8
+ "idiom", "searchIdiom", "get_json_path", "crack",
9
+ "make", "get_file_content_as_base64", "save_pic", "detect_windows_scale",
10
+ "get_file_content_as_base64_2", "cartoon"] # 可选:明确导出的内容
XMWAI/core.py CHANGED
@@ -5,9 +5,6 @@ import os
5
5
  import zipfile
6
6
 
7
7
 
8
- # AI绘图大师函数
9
-
10
-
11
8
  # U1-02 Story GPT----------------------------------
12
9
  def story(role, time, address, event, key=""):
13
10
  content = role+time+address+event
XMWAI/trial_class.py ADDED
@@ -0,0 +1,283 @@
1
+ import base64
2
+ import tkinter as tk
3
+ import urllib
4
+ import requests
5
+ import json
6
+ from PIL import Image
7
+ from PIL import ImageGrab
8
+ from io import BytesIO
9
+ import time
10
+ import os
11
+ import ctypes
12
+ import cv2
13
+ import numpy as np
14
+
15
+ '''-----------体验课1-----------'''
16
+
17
+
18
+ def make(screen):
19
+ save_pic(screen)
20
+ while not os.path.exists("pic.png"):
21
+ pass
22
+
23
+ """
24
+ 使用 AK,SK 生成鉴权签名(Access Token)
25
+ :return: access_token,或是None(如果错误)
26
+ """
27
+ API_KEY = "YleMT041wl8zkXhk1Y4AdEuk"
28
+ SECRET_KEY = "xQousjKEqphGwVMKHJlUSGDXp7PiUVpk"
29
+ url = "https://aip.baidubce.com/oauth/2.0/token"
30
+ params = {"grant_type": "client_credentials",
31
+ "client_id": API_KEY, "client_secret": SECRET_KEY}
32
+
33
+ # 获取访问令牌并构建API请求URL
34
+ access_token = str(requests.post(
35
+ url, params=params).json().get("access_token"))
36
+ create_url = f"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2imgv2?access_token={access_token}"
37
+ query_url = f"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImgv2?access_token={access_token}"
38
+
39
+ # 读取图像并转换为Base64编码
40
+ with open("pic.png", "rb") as image_file:
41
+ base64_string = base64.b64encode(image_file.read()).decode('utf-8')
42
+
43
+ # 构建创建任务的请求参数
44
+ create_payload = json.dumps({
45
+ "prompt": "参考当前图,希望画面卡通一些,画面可以丰富一些,内容积极向上,参考宫崎骏画风",
46
+ "width": 1024,
47
+ "height": 1024,
48
+ "image": base64_string,
49
+ "change_degree": 1
50
+ }, ensure_ascii=False)
51
+
52
+ headers = {
53
+ 'Content-Type': 'application/json',
54
+ 'Accept': 'application/json'
55
+ }
56
+
57
+ # 发送创建任务请求
58
+ response = requests.post(create_url, headers=headers,
59
+ data=create_payload.encode("utf-8"))
60
+ response.raise_for_status() # 检查请求是否成功
61
+ task_id = response.json()["data"]["task_id"]
62
+ # print(f"任务已创建,ID: {task_id}")
63
+
64
+ # 轮询检查任务状态
65
+ query_payload = json.dumps({"task_id": task_id}, ensure_ascii=False)
66
+ task_status = "RUNNING"
67
+ print("AI图片生成中.......")
68
+ while task_status == "RUNNING":
69
+ time.sleep(30)
70
+ response = requests.post(
71
+ query_url, headers=headers, data=query_payload.encode("utf-8"))
72
+ response.raise_for_status()
73
+ task_status = response.json()["data"]["task_status"]
74
+ # print(f"任务状态: {task_status}")
75
+
76
+ # 处理任务结果
77
+ if task_status == "SUCCESS":
78
+ picture = requests.get(response.json()[
79
+ "data"]["sub_task_result_list"][0]["final_image_list"][0]["img_url"])
80
+ image_data = BytesIO(picture.content)
81
+ image = Image.open(image_data)
82
+ image.save('image.gif')
83
+ os.system("image.gif")
84
+ else:
85
+ print(f"任务失败,状态: {task_status}")
86
+
87
+
88
+ def get_file_content_as_base64(path, urlencoded=False):
89
+ """
90
+ 获取文件base64编码
91
+ :param path: 文件路径
92
+ :param urlencoded: 是否对结果进行urlencoded
93
+ :return: base64编码信息
94
+ """
95
+ with open(path, "rb") as f:
96
+ content = base64.b64encode(f.read()).decode("utf8")
97
+ if urlencoded:
98
+ content = urllib.parse.quote_plus(content)
99
+ return content
100
+
101
+
102
+ def save_pic(screen, output_file="pic.png"):
103
+ """
104
+ 精准截取turtle绘图区域,不包含窗口边框和标题栏
105
+ 专为Windows系统优化,支持高DPI缩放
106
+
107
+ 参数:
108
+ screen: turtle的Screen对象
109
+ output_file: 输出文件名
110
+ """
111
+ canvas = screen.getcanvas()
112
+ screen.update()
113
+
114
+ try:
115
+ # 获取画布位置和大小
116
+ x = canvas.winfo_rootx()
117
+ y = canvas.winfo_rooty()
118
+ width = canvas.winfo_width()
119
+ height = canvas.winfo_height()
120
+
121
+ # 检测Windows屏幕缩放比例
122
+ scale_factor = detect_windows_scale()
123
+ # print(f"检测到屏幕缩放比例: {scale_factor}x")
124
+
125
+ # 调整截图区域,排除窗口边框
126
+ # 典型Windows窗口边框宽度约为8像素,标题栏约为30像素
127
+ border_width = int(8 * scale_factor)
128
+ title_height = int(30 * scale_factor)
129
+
130
+ # 计算实际绘图区域
131
+ img = ImageGrab.grab(
132
+ bbox=(
133
+ x + border_width, # 左边界,排除左侧边框
134
+ y + title_height, # 上边界,排除标题栏和上边框
135
+ x + width - border_width, # 右边界,排除右侧边框
136
+ y + height - border_width # 下边界,排除底部边框
137
+ )
138
+ )
139
+
140
+ # 保存为PNG
141
+ img.save(output_file)
142
+ # print(f"已保存图形到 {output_file} (尺寸: {img.size})")
143
+ except Exception as e:
144
+ print(f"截图时出错: {e}")
145
+ print("提示: 尝试使用save_turtle_canvas_fallback函数或手动截图")
146
+
147
+
148
+ def detect_windows_scale():
149
+ """
150
+ 检测Windows系统的屏幕缩放比例
151
+ 优先使用高精度方法,失败则降级使用兼容性方法
152
+ """
153
+ try:
154
+ # 高精度方法:获取系统DPI
155
+ user32 = ctypes.windll.user32
156
+ user32.SetProcessDPIAware()
157
+
158
+ # 尝试获取系统DPI (Windows 8.1及以上)
159
+ if hasattr(user32, 'GetDpiForSystem'):
160
+ dpi = user32.GetDpiForSystem()
161
+ return dpi / 96.0 # Windows标准DPI是96
162
+
163
+ # 尝试获取显示器DPI (Windows 10及以上)
164
+ if hasattr(user32, 'GetDpiForWindow'):
165
+ root = tk.Tk()
166
+ dpi = user32.GetDpiForWindow(root.winfo_id())
167
+ root.destroy()
168
+ return dpi / 96.0
169
+
170
+ # 兼容方法:获取屏幕尺寸并与标准尺寸比较
171
+ screen_width = user32.GetSystemMetrics(0)
172
+ return screen_width / 1920.0 # 假设标准分辨率为1920x1080
173
+ except Exception as e:
174
+ print(f"检测缩放比例时出错: {e}")
175
+ print("使用默认缩放比例1.0")
176
+ return 1.0
177
+
178
+
179
+ '''-----------体验课2-----------'''
180
+
181
+
182
+ def get_file_content_as_base64_2(path, urlencoded=False):
183
+ """
184
+ 获取文件base64编码
185
+ :param path: 文件路径
186
+ :param urlencoded: 是否对结果进行urlencoded
187
+ :return: base64编码信息
188
+ """
189
+ with open(path, "rb") as f:
190
+ content = base64.b64encode(f.read()).decode("utf8")
191
+ if urlencoded:
192
+ content = urllib.parse.quote_plus(content)
193
+ return content
194
+
195
+
196
+ def cartoon(name):
197
+ """
198
+ 使用 AK,SK 生成鉴权签名(Access Token)
199
+ :return: access_token,或是None(如果错误)
200
+ """
201
+ API_KEY = "LjdoA5Ar7MGrwynZfTFcB7K3"
202
+ SECRET_KEY = "3htSVp4IhW8LIyetP5Yo8NdWvF0yNH0W"
203
+ url = "https://aip.baidubce.com/oauth/2.0/token"
204
+ params = {"grant_type": "client_credentials",
205
+ "client_id": API_KEY, "client_secret": SECRET_KEY}
206
+ s = str(requests.post(url, params=params).json().get("access_token"))
207
+
208
+ url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + s
209
+ base64_string = get_file_content_as_base64_2("pic.jpg", True)
210
+ payload = "image={}".format(base64_string)
211
+ headers = {
212
+ 'Content-Type': 'application/x-www-form-urlencoded',
213
+ 'Accept': 'application/json'
214
+ }
215
+ response = requests.request(
216
+ "POST", url, headers=headers, data=payload.encode("utf-8"))
217
+ base64_data = response.json()["image"]
218
+ image_data = base64.b64decode(base64_data)
219
+ with open("img.png", 'wb') as f:
220
+ f.write(image_data)
221
+
222
+ photo = cv2.imread('img.png')
223
+ original = cv2.imread('pic.jpg')
224
+ height, width = photo.shape[:2]
225
+
226
+ # 扫描效果参数
227
+ scan_bar_height = 60 # 扫描光带高度
228
+ glow_width = 15 # 光晕宽度
229
+ glow_intensity = 0.3 # 光晕强度
230
+ scan_speed = 5 # 扫描速度(毫秒)
231
+
232
+ # 创建一个与图像大小相同的透明层用于绘制扫描效果
233
+ overlay = np.zeros_like(original, dtype=np.uint8)
234
+
235
+ for i in range(-scan_bar_height, height + scan_bar_height):
236
+ # 创建一个空白图像
237
+ display = original.copy()
238
+ # 只显示扫描到的部分
239
+ if i > 0:
240
+ display[:min(i, height), :] = photo[:min(i, height), :]
241
+
242
+ # 清除上一帧的扫描效果
243
+ overlay.fill(0)
244
+
245
+ # 计算扫描光带位置
246
+ scan_start = max(0, i - scan_bar_height // 2)
247
+ scan_end = min(height, i + scan_bar_height // 2)
248
+
249
+ if scan_start < scan_end:
250
+ # 创建渐变扫描光带
251
+ for y in range(scan_start, scan_end):
252
+ # 计算渐变因子(0-1-0)
253
+ distance_from_center = abs(y - i)
254
+ gradient_factor = 1.0 - \
255
+ (distance_from_center / (scan_bar_height / 2))
256
+
257
+ # 绿色扫描线主体
258
+ green_intensity = int(255 * gradient_factor)
259
+ overlay[y, :, 1] = green_intensity
260
+
261
+ # 添加轻微的蓝色调,使扫描线更真实
262
+ overlay[y, :, 2] = int(60 * gradient_factor)
263
+
264
+ # 添加光晕效果
265
+ for y in range(max(0, scan_start - glow_width), min(height, scan_end + glow_width)):
266
+ if y < scan_start or y >= scan_end:
267
+ # 计算光晕强度
268
+ distance_from_edge = min(
269
+ abs(y - scan_start), abs(y - scan_end))
270
+ glow_factor = max(
271
+ 0, 1.0 - (distance_from_edge / glow_width)) * glow_intensity
272
+
273
+ # 添加绿色光晕
274
+ overlay[y, :, 1] = np.clip(
275
+ overlay[y, :, 1] + (255 * glow_factor), 0, 255)
276
+
277
+ # 将半透明扫描效果叠加到图像上
278
+ alpha = 0.5 # 透明度
279
+ cv2.addWeighted(overlay, alpha, display, 1 - alpha, 0, display)
280
+
281
+ # 显示稳定的扫描效果
282
+ cv2.imshow(name, display)
283
+ cv2.waitKey(scan_speed) # 控制扫描速度
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: XMWAI
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Summary: Small code King AI related library
5
5
  Home-page: https://github.com/Tonykai88/XMWAI.git
6
6
  Author: pydevelopment
@@ -12,6 +12,9 @@ Requires-Python: >=3.7.0
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE.txt
14
14
  Requires-Dist: requests>=2.32.3
15
+ Requires-Dist: Pillow>=11.1.0
16
+ Requires-Dist: opencv-python>=3.4.18.65
17
+ Requires-Dist: numpy>=2.2.3
15
18
  Dynamic: author
16
19
  Dynamic: author-email
17
20
  Dynamic: classifier
@@ -1,9 +1,10 @@
1
- XMWAI/__init__.py,sha256=91jaQmcGqp_B4i68bNfn_0m6JTFzz-itTiCmt__vIR8,487
1
+ XMWAI/__init__.py,sha256=Kz93zgrnLiGqlqCp6y8Y4McEBJOznvQrS-IPhwsvCTw,795
2
2
  XMWAI/bomb_core.py,sha256=h2ZPH3SuoG2L_XOf1dcK8p3lhw5QzhneWl2yMLj1RiU,1819
3
3
  XMWAI/cookbook_core.py,sha256=mo3IYgh_bYD_3vH2Stkjwo9okFOb4abooZeJ-Djx6VU,3675
4
- XMWAI/core.py,sha256=H4EpXmXXakVNleUKyUMVnl3AmR8XjjVLsVfGvsLRUqg,7933
4
+ XMWAI/core.py,sha256=SxzfbOW7WZALuA5fQ4dGieJR2-roxufjXQ_mqwGDg3E,7905
5
5
  XMWAI/idiom_core.py,sha256=yU-VHhqqoutVm6GVikcjL3m9yuB1hUsFBpPYvwY4n5g,1689
6
6
  XMWAI/magic_core.py,sha256=Ms4b12PJ8rjsmceg1VUqWCWx2ebvdhLp4sIF6K_Vaok,3491
7
+ XMWAI/trial_class.py,sha256=GRDEe24a-DYMRyoTLgJbE5dCSQY0CbYqB5ALsBfYmtA,10226
7
8
  XMWAI/file/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
9
  XMWAI/file/idiom.json,sha256=HUtPRUzhxBbWoasjadbmbA_5ngQ5AXLu9weQSZ4hzhk,10319857
9
10
  XMWAI/gif/0.gif,sha256=LGpAReVTyZEb1J-bWYTpxxHbIxmLJ-3wA0lw4cBqdsY,303
@@ -93,8 +94,8 @@ XMWAI/gif/84.gif,sha256=6Ip_uQmvrr2fagLXu1YqWyI_DL2PVVtKCPtmNtzt3P4,38767
93
94
  XMWAI/gif/85.gif,sha256=6Ip_uQmvrr2fagLXu1YqWyI_DL2PVVtKCPtmNtzt3P4,38767
94
95
  XMWAI/gif/9.gif,sha256=cPouth-xwc3QPcg2m6HMP2OD1ZCeRBD_-RPImvvKAA0,9485
95
96
  XMWAI/gif/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
96
- xmwai-0.2.7.dist-info/licenses/LICENSE.txt,sha256=bcaIQMrIhdQ3O-PoZlexjmW6h-wLGvHxh5Oksl4ohtc,1066
97
- xmwai-0.2.7.dist-info/METADATA,sha256=-pGe6LcF2Fy7GeNWgQxhP8dEOcKyRpVZ_HeEQ2FnQDY,1033
98
- xmwai-0.2.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
99
- xmwai-0.2.7.dist-info/top_level.txt,sha256=yvGcDI-sggK5jqd9wz0saipZvk3oIE3hNGHlqUjxf0c,6
100
- xmwai-0.2.7.dist-info/RECORD,,
97
+ xmwai-0.2.9.dist-info/licenses/LICENSE.txt,sha256=bcaIQMrIhdQ3O-PoZlexjmW6h-wLGvHxh5Oksl4ohtc,1066
98
+ xmwai-0.2.9.dist-info/METADATA,sha256=73264zV2DE4eGCdSDdI-Ba7G6RvZhd1cyWc4cWX3hFU,1134
99
+ xmwai-0.2.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
+ xmwai-0.2.9.dist-info/top_level.txt,sha256=yvGcDI-sggK5jqd9wz0saipZvk3oIE3hNGHlqUjxf0c,6
101
+ xmwai-0.2.9.dist-info/RECORD,,
File without changes