Spaces:
Runtime error
Runtime error
push
Browse files- .DS_Store +0 -0
- app.py +130 -0
- images/dices/1.jpeg +0 -0
- images/dices/2.jpeg +0 -0
- images/dices/3.jpeg +0 -0
- images/dices/4.jpeg +0 -0
- images/dices/5.jpeg +0 -0
- images/dices/6.jpeg +0 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from openpyxl import Workbook
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def gray_to_dice_1(x: int) -> int:
|
| 7 |
+
if 0 <= x and x <= 41:
|
| 8 |
+
return 1
|
| 9 |
+
elif 41 < x and x <= 83:
|
| 10 |
+
return 2
|
| 11 |
+
elif 83 < x and x <= 124:
|
| 12 |
+
return 3
|
| 13 |
+
elif 124 < x and x <= 165:
|
| 14 |
+
return 4
|
| 15 |
+
elif 165 < x and x <= 206:
|
| 16 |
+
return 5
|
| 17 |
+
elif 206 < x and x <= 247:
|
| 18 |
+
return 6
|
| 19 |
+
else:
|
| 20 |
+
return 6
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def gray_to_dice_2(x: int) -> int:
|
| 24 |
+
if 0 <= x and x <= 41:
|
| 25 |
+
return 0
|
| 26 |
+
elif 41 < x and x <= 83:
|
| 27 |
+
return 1
|
| 28 |
+
elif 83 < x and x <= 124:
|
| 29 |
+
return 2
|
| 30 |
+
elif 124 < x and x <= 165:
|
| 31 |
+
return 3
|
| 32 |
+
elif 165 < x and x <= 206:
|
| 33 |
+
return 4
|
| 34 |
+
elif 206 < x and x <= 247:
|
| 35 |
+
return 5
|
| 36 |
+
else:
|
| 37 |
+
return 6
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def dice_to_image(
|
| 41 |
+
image: Image.Image,
|
| 42 |
+
resolution_x: int,
|
| 43 |
+
resolution_y: int,
|
| 44 |
+
dice_size: int,
|
| 45 |
+
):
|
| 46 |
+
resolution_x, resolution_y = int(resolution_x), int(resolution_y)
|
| 47 |
+
dice_size = int(dice_size)
|
| 48 |
+
|
| 49 |
+
image = image.convert("L")
|
| 50 |
+
image = image.resize((resolution_x, resolution_y))
|
| 51 |
+
|
| 52 |
+
dice_img_1 = Image.new("L", (image.size[0] * dice_size, image.size[1] * dice_size))
|
| 53 |
+
dice_img_2 = Image.new("L", (image.size[0] * dice_size, image.size[1] * dice_size))
|
| 54 |
+
|
| 55 |
+
dices = [Image.new("L", (dice_size, dice_size))] + [
|
| 56 |
+
Image.open(f"images/dices/{i}.jpeg").resize((dice_size, dice_size)).convert("L")
|
| 57 |
+
for i in range(1, 7)
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
wb = Workbook()
|
| 61 |
+
for sheet_name in wb.sheetnames:
|
| 62 |
+
wb.remove(wb[sheet_name])
|
| 63 |
+
ws1 = wb.create_sheet("Style #1")
|
| 64 |
+
ws2 = wb.create_sheet("Style #2")
|
| 65 |
+
|
| 66 |
+
for i in range(image.size[0]):
|
| 67 |
+
for j in range(image.size[1]):
|
| 68 |
+
pixel = int(image.getpixel((i, j)))
|
| 69 |
+
dice_index_1 = gray_to_dice_1(pixel)
|
| 70 |
+
dice_index_2 = gray_to_dice_2(pixel)
|
| 71 |
+
|
| 72 |
+
ws1.cell(row=j + 1, column=i + 1).value = dice_index_1
|
| 73 |
+
ws2.cell(row=j + 1, column=i + 1).value = dice_index_2
|
| 74 |
+
|
| 75 |
+
box = (
|
| 76 |
+
i * dice_size,
|
| 77 |
+
j * dice_size,
|
| 78 |
+
(i + 1) * dice_size,
|
| 79 |
+
(j + 1) * dice_size,
|
| 80 |
+
)
|
| 81 |
+
dice_img_1.paste(dices[dice_index_1], box=box)
|
| 82 |
+
dice_img_2.paste(dices[dice_index_2], box=box)
|
| 83 |
+
|
| 84 |
+
wb.save("results.xlsx")
|
| 85 |
+
|
| 86 |
+
return dice_img_1, dice_img_2, "results.xlsx"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
with gr.Blocks() as app:
|
| 91 |
+
gr.Markdown("Image to Dice")
|
| 92 |
+
|
| 93 |
+
with gr.Row(variant="panel"):
|
| 94 |
+
image_input = gr.Image(type="pil")
|
| 95 |
+
|
| 96 |
+
with gr.Row(variant="panel"):
|
| 97 |
+
resolution_x = gr.Number(value=200, precision=0, label="分辨率(宽)", show_label=True)
|
| 98 |
+
resolution_y = gr.Number(value=200, precision=0, label="分辨率(高)", show_label=True)
|
| 99 |
+
dice_size = gr.Number(value=16, precision=0, label="骰子大小", show_label=True)
|
| 100 |
+
|
| 101 |
+
with gr.Row(variant="panel"):
|
| 102 |
+
run_button = gr.Button("生成")
|
| 103 |
+
|
| 104 |
+
with gr.Row(variant="panel"):
|
| 105 |
+
image_output1 = gr.Image(type="pil", label="不用全黑", show_label=True)
|
| 106 |
+
image_output2 = gr.Image(type="pil", label="用全黑", show_label=True)
|
| 107 |
+
|
| 108 |
+
with gr.Row(variant="panel"):
|
| 109 |
+
excel_file = gr.File(label="Excel结果", show_label=True)
|
| 110 |
+
|
| 111 |
+
run_button.click(
|
| 112 |
+
dice_to_image,
|
| 113 |
+
inputs=[
|
| 114 |
+
image_input,
|
| 115 |
+
resolution_x,
|
| 116 |
+
resolution_y,
|
| 117 |
+
dice_size,
|
| 118 |
+
],
|
| 119 |
+
outputs=[
|
| 120 |
+
image_output1,
|
| 121 |
+
image_output2,
|
| 122 |
+
excel_file,
|
| 123 |
+
],
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
app.launch(share=True)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
main()
|
images/dices/1.jpeg
ADDED
|
images/dices/2.jpeg
ADDED
|
images/dices/3.jpeg
ADDED
|
images/dices/4.jpeg
ADDED
|
images/dices/5.jpeg
ADDED
|
images/dices/6.jpeg
ADDED
|