-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
295 changed files
with
2,396 additions
and
2,320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,132 @@ | ||
import os | ||
from PIL import Image | ||
from PIL import Image # Pillow | ||
|
||
max_width = 1080 | ||
max_height = 1080 | ||
|
||
def compress_image(input_file_path, output_file_path, quality=85, max_width=800, max_height=800): | ||
|
||
def remove_alpha_channel(img): | ||
"""Remove the alpha channel from the image.""" | ||
# 先保留 alpha 通道 | ||
rgba_img = img.convert("RGBA") | ||
# 创建一个白色背景的图像, 大小与 PNG 图像相同 | ||
white_background = Image.new("RGBA", rgba_img.size, (255, 255, 255, 255)) | ||
# 将 PNG 图像粘贴到白色背景上, 使用 PNG 图像本身的 alpha 通道 | ||
combined_img = Image.alpha_composite(white_background, rgba_img) | ||
# 转换为 RGB 模式, 移除 alpha 通道 | ||
rgb_img = combined_img.convert("RGB") | ||
return rgb_img | ||
|
||
|
||
def convert_to_jpg(input_file_path, output_file_path, quality=90): | ||
"""Convert the image to jpg and save it to the output path.""" | ||
with Image.open(input_file_path) as img: | ||
original_width, original_height = img.size | ||
if original_width > max_width or original_height > max_height: | ||
return compress_image(input_file_path, output_file_path, quality) | ||
rgb_img = remove_alpha_channel(img) | ||
output_file_path = output_file_path.replace(".png", ".jpg") | ||
rgb_img.save(output_file_path, format="jpeg", | ||
optimize=True, quality=quality) | ||
return output_file_path | ||
|
||
|
||
def compress_image(input_file_path, output_file_path, quality=80): | ||
"""Compress the image, resize it and save it to the output path.""" | ||
with Image.open(input_file_path) as img: | ||
# 获取原图尺寸 | ||
original_width, original_height = img.size | ||
|
||
# 计算缩小比例,并重新计算宽度和高度 | ||
aspect_ratio = original_width / original_height | ||
if original_width > max_width or original_height > max_height: | ||
if aspect_ratio > 1: # 宽>高 | ||
if aspect_ratio > 1: | ||
# 宽 > 高 | ||
new_width = max_width | ||
new_height = int(max_width / aspect_ratio) | ||
else: # 高>宽 | ||
else: | ||
# 高 > 宽 | ||
new_height = max_height | ||
new_width = int(max_height * aspect_ratio) | ||
img = img.resize((new_width, new_height), Image.LANCZOS) | ||
else: | ||
new_width = original_width | ||
new_height = original_height | ||
|
||
img.save(output_file_path, optimize=True, quality=quality) | ||
rgb_img = remove_alpha_channel(img) | ||
output_file_path = output_file_path.replace(".png", ".jpg") | ||
rgb_img.save(output_file_path, format="jpeg", | ||
optimize=True, quality=quality) | ||
return output_file_path | ||
|
||
|
||
def scan_and_compress_images(directory, size_threshold=800*1024, quality=85): | ||
def scan_and_compress_images(directory): | ||
""" | ||
Scan the directory for .png and .jpg files larger than size_threshold and compress them. | ||
:param directory: Directory to scan for images. | ||
:param size_threshold: Size threshold in bytes (default 800KB). | ||
:param quality: Quality of the compressed image (default 85). | ||
""" | ||
size_threshold = 2 * 1024 * 1024 # 2MB | ||
compressed_files = [] | ||
for root, _, files in os.walk(directory): | ||
for file_name in files: | ||
if file_name.lower().endswith((".png", ".jpg", ".jpeg")): | ||
file_path = os.path.join(root, file_name) | ||
file_size = os.path.getsize(file_path) | ||
if not file_name.lower().endswith((".png", ".jpg", ".jpeg")): | ||
continue | ||
file_path = os.path.join(root, file_name) | ||
file_size = os.path.getsize(file_path) | ||
output_file_path = os.path.join(root, f".temp/{file_name}") | ||
# 不处理 .temp 目录 以及 Picture 目录 | ||
if file_path.find(".temp") != -1 or file_path.find("Picture") > -1: | ||
continue | ||
# 按需创建 .temp 目录 | ||
if not os.path.exists(os.path.join(root, ".temp")): | ||
os.makedirs(os.path.join(root, ".temp")) | ||
if file_size > size_threshold: | ||
# 如果文件大小超过阈值, 压缩图片长宽以及质量, 并转换为 jpg 格式 | ||
output_file_path = compress_image( | ||
file_path, output_file_path, 80) | ||
elif file_name.endswith(".png"): | ||
# 如果是 png 图片, 压缩质量并转换为 jpg 格式 | ||
output_file_path = convert_to_jpg( | ||
file_path, output_file_path, 90) | ||
else: | ||
continue | ||
output_file_size = os.path.getsize(output_file_path) | ||
# 如果压缩后的文件比原文件大, 则删除压缩后的文件 | ||
if output_file_size > file_size: | ||
os.remove(output_file_path) | ||
continue | ||
print( | ||
f"Convert: {file_size / (1024*1024):.2f}MB => {output_file_size / (1024*1024):.2f}MB" | ||
) | ||
# 删除原文件, 替换为压缩后的文件 | ||
os.remove(file_path) | ||
os.rename(output_file_path, | ||
output_file_path.replace("/.temp", "")) | ||
print(f"{file_path}") | ||
print(f"{output_file_path}") | ||
# 存储原文件名 | ||
compressed_files.append(file_name) | ||
return compressed_files | ||
|
||
|
||
if file_size > size_threshold and file_path.find(".temp") == -1: | ||
print( | ||
f"Compressing: {file_path} {file_size / (1024*1024):.2f} MB" | ||
) | ||
if not os.path.exists(os.path.join(root, ".temp")): | ||
os.makedirs(os.path.join(root, ".temp")) | ||
output_file_path = os.path.join(root, f".temp/{file_name}") | ||
compress_image(file_path, output_file_path, quality) | ||
def replace_md_file_content(directory): | ||
""" | ||
Replace the content of the markdown file with the compressed image file name. | ||
""" | ||
for root, _, files in os.walk(directory): | ||
for file_name in files: | ||
if not file_name.lower().endswith((".md")): | ||
continue | ||
file_path = os.path.join(root, file_name) | ||
with open(file_path, "r", encoding="utf-8") as file: | ||
content = file.read() | ||
raw_content = content | ||
for file_name in compressed_files: | ||
new_file_name = file_name.replace(".png", ".jpg") | ||
content = content.replace( | ||
f"screenshots/{file_name}", f"screenshots/{new_file_name}") | ||
if raw_content != content: | ||
print(f"Replace: {file_path}") | ||
with open(file_path, "w", encoding="utf-8") as file: | ||
file.write(content) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Replace with the path to your directory | ||
directory_to_scan = os.path.realpath(os.path.join(os.getcwd(), "../")) | ||
scan_and_compress_images(directory_to_scan) | ||
compressed_files = scan_and_compress_images(directory_to_scan) | ||
replace_md_file_content(directory_to_scan) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
# Recovery | ||
Recovery是一种可以对安卓手机内部的数据文件进行修改的模式,类似电脑的PE。不同的recovery有不同的功能。使用recovery可以说是刷机(卡刷)的基础,想要比较顺畅的刷机了解好rec是必不可少的。 | ||
|
||
> PS:为何不先介绍线刷 线刷看起来更容易,直接用手机连接电脑然后用各类刷机软件一键式操作就好。以前的话的确是这个样子,只要找到合适的刷机包一键刷机即可。但是目前各大手机厂商为了安全使用了BL锁,这样线刷就变得相对困难一些了,尤其是官方闭源,无法解锁BL的手机。 | ||
如何进入rec:在关机状态下,同时按住手的电源键和音量上(有的手机是音量下,这个键视手机而定)。 | ||
|
||
进入rec有何作用:除了刚介绍的刷机还可以进行数据的清除,某些时候手机无法开机,或者忘记了密码,并且不想去寻找售后的话,可以直接尝试清除数据 | ||
> PS:如果是忘记了密码,并且是下边将要介绍的第三方rec中的TWRP的话,可以直接删除密码文件然后就可以进入手机了 | ||
官方recovery(新买的手机官方自带的recovery),只能用它进行官方OTA系统升级以及恢复出厂设置。 | ||
第三方recovery目前主要有CWM和TWRP。CWM目前已经很少见了,主要是TWRP。 | ||
|
||
## CWM: | ||
|
||
![](screenshots/2023-04-14-20-28-42.png) | ||
|
||
1.CWM常用的只有刷入刷机包以及清除数据,并且无法采用触控的方式,只能利用音量键等进行操作,相对twrp recovery来说功能较少。 | ||
|
||
|
||
## TWRP | ||
|
||
![](screenshots/2023-04-14-20-28-55.png) | ||
|
||
1. install | ||
第三方rom、补丁的安装,也可以选install下的storage那里,切换至另一个存储器 | ||
2. wipe | ||
对手机的某些分区或者内存储、外置SD卡或者OTG设备进行清除。双清三清四清五清,刷机一般只需要双清即可 | ||
3. Back up | ||
对手机某些分区的备份 | ||
4. restore | ||
对手机某些分区的数据恢复还原 | ||
5. mount | ||
挂载手机某些分区 | ||
对系统文件进行管理或者终端操作system分区时,会发现找不到system,这时要用到mount命令。 | ||
6. advanced | ||
对手机内部数据管理,包括复制、移动、赋权限、删除和重命名(对system进行操作别忘了用MOUNT命令) | ||
7. SD卡分区; | ||
partition SD card,这个请自行尝试 | ||
8. reload theme | ||
修改recovery主题,可以美化recovery界面,以及使用中文界面 | ||
将下载的主题(ui.zip)放到TWRP/theme/目录下(可能是内存储,也可能是外置SD卡,看你recovery里面的storage选择哪里了),点击reload theme,即可使用主题。可以通过主题选择,将TWRP界面换成中文版。 | ||
9. terminal command | ||
手机端终端,对手机系统进行修改 | ||
10. file manager | ||
文件管理 | ||
11. adb sideload刷机 | ||
自动将电脑端第三方rom和补丁包推送到手机内存储或者sd卡(adb命令:adb sideload ***.zip) | ||
# Recovery | ||
Recovery是一种可以对安卓手机内部的数据文件进行修改的模式,类似电脑的PE。不同的recovery有不同的功能。使用recovery可以说是刷机(卡刷)的基础,想要比较顺畅的刷机了解好rec是必不可少的。 | ||
|
||
> PS:为何不先介绍线刷 线刷看起来更容易,直接用手机连接电脑然后用各类刷机软件一键式操作就好。以前的话的确是这个样子,只要找到合适的刷机包一键刷机即可。但是目前各大手机厂商为了安全使用了BL锁,这样线刷就变得相对困难一些了,尤其是官方闭源,无法解锁BL的手机。 | ||
如何进入rec:在关机状态下,同时按住手的电源键和音量上(有的手机是音量下,这个键视手机而定)。 | ||
|
||
进入rec有何作用:除了刚介绍的刷机还可以进行数据的清除,某些时候手机无法开机,或者忘记了密码,并且不想去寻找售后的话,可以直接尝试清除数据 | ||
> PS:如果是忘记了密码,并且是下边将要介绍的第三方rec中的TWRP的话,可以直接删除密码文件然后就可以进入手机了 | ||
官方recovery(新买的手机官方自带的recovery),只能用它进行官方OTA系统升级以及恢复出厂设置。 | ||
第三方recovery目前主要有CWM和TWRP。CWM目前已经很少见了,主要是TWRP。 | ||
|
||
## CWM: | ||
|
||
![](screenshots/2023-04-14-20-28-42.jpg) | ||
|
||
1.CWM常用的只有刷入刷机包以及清除数据,并且无法采用触控的方式,只能利用音量键等进行操作,相对twrp recovery来说功能较少。 | ||
|
||
|
||
## TWRP | ||
|
||
![](screenshots/2023-04-14-20-28-55.jpg) | ||
|
||
1. install | ||
第三方rom、补丁的安装,也可以选install下的storage那里,切换至另一个存储器 | ||
2. wipe | ||
对手机的某些分区或者内存储、外置SD卡或者OTG设备进行清除。双清三清四清五清,刷机一般只需要双清即可 | ||
3. Back up | ||
对手机某些分区的备份 | ||
4. restore | ||
对手机某些分区的数据恢复还原 | ||
5. mount | ||
挂载手机某些分区 | ||
对系统文件进行管理或者终端操作system分区时,会发现找不到system,这时要用到mount命令。 | ||
6. advanced | ||
对手机内部数据管理,包括复制、移动、赋权限、删除和重命名(对system进行操作别忘了用MOUNT命令) | ||
7. SD卡分区; | ||
partition SD card,这个请自行尝试 | ||
8. reload theme | ||
修改recovery主题,可以美化recovery界面,以及使用中文界面 | ||
将下载的主题(ui.zip)放到TWRP/theme/目录下(可能是内存储,也可能是外置SD卡,看你recovery里面的storage选择哪里了),点击reload theme,即可使用主题。可以通过主题选择,将TWRP界面换成中文版。 | ||
9. terminal command | ||
手机端终端,对手机系统进行修改 | ||
10. file manager | ||
文件管理 | ||
11. adb sideload刷机 | ||
自动将电脑端第三方rom和补丁包推送到手机内存储或者sd卡(adb命令:adb sideload ***.zip) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.
Diff not rendered.
Oops, something went wrong.