诈骗失败 X2
前面一直在研究前辈和其他人的排版,然后就和Claude一起手搓了一个自动根据图片排版的Python脚本。
#!/usr/bin/env python3
"""Convert an image to an HTML table where each cell represents a pixel."""
from PIL import Image
import sys
import os
def image_to_html_table(image_path, output_path, width=120):
"""Convert an image to an HTML table.
Args:
image_path: Path to the input image
output_path: Path for the output HTML file
width: Target width in cells (height calculated to maintain aspect ratio)
"""
# Load the image
img = Image.open(image_path)
# Calculate new height to maintain aspect ratio
aspect_ratio = img.height / img.width
height = int(width * aspect_ratio)
# Resize the image
img = img.resize((width, height), Image.Resampling.LANCZOS)
# Convert to grayscale
img = img.convert('L')
# Get pixel data
pixels = list(img.getdata())
# Build HTML
html = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bad Apple - HTML Table</title>
</head>
<body>
<table style="border-collapse:collapse;table-layout:fixed">
"""
# Generate table rows
for y in range(height):
html += " <tr>"
for x in range(width):
pixel_value = pixels[y * width + x]
# Convert grayscale to hex color
hex_color = f"#{pixel_value:02x}{pixel_value:02x}{pixel_value:02x}"
html += f'<td style="background-color:{hex_color};width:6px;height:6px;padding:0;line-height:0"></td>'
html += "</tr>\n"
html += """ </table>
</body>
</html>
"""
# Write output
with open(output_path, 'w') as f:
f.write(html)
print(f"Generated HTML table: {output_path}")
print(f"Dimensions: {width}x{height} cells")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python image_to_table_v2.py <image_path> [width]")
sys.exit(1)
image_path = sys.argv[1]
# Derive output path from input filename
base_name = os.path.splitext(os.path.basename(image_path))[0]
output_path = f"{base_name}_table.html"
# Optional: accept width from command line
width = int(sys.argv[2]) if len(sys.argv) > 2 else 120
image_to_html_table(image_path, output_path, width)