2Nike2 commited on
Commit
014abd7
1 Parent(s): 8004cfc

Can upload user original card design.

Browse files
Files changed (3) hide show
  1. app.py +35 -30
  2. src/front_card_frame.py +65 -0
  3. src/front_card_image.py +2 -2
app.py CHANGED
@@ -7,6 +7,7 @@ import glob
7
 
8
  import src.constants as constants
9
 
 
10
  from src.front_card_image import create_card_image
11
  from src.front_card_image_historic_site import create_historic_site_card_image
12
  from src.picture_box_model import create_picture_box_model
@@ -29,11 +30,11 @@ def update_card_image(card_design_upload, card_design_dropdown, update_type):
29
  else:
30
  return default_card_design_image_dict[card_design_dropdown]
31
 
32
- def create_3dmodel(model_no, title, color, mark, historic_site_type, difficulty, description, is_thick, image):
33
 
34
- img_bytearray = BytesIO()
35
- image['background'].save(img_bytearray, "JPEG", quality=95)
36
- img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.
37
 
38
  option_dict = {
39
  'タイトル': title,
@@ -50,16 +51,21 @@ def create_3dmodel(model_no, title, color, mark, historic_site_type, difficulty,
50
  if model_no not in ['A', 'B']:
51
  # Create the card image (front side)
52
  if model_no == '1':
53
- front_img_bytearray = create_historic_site_card_image(img_bytearray, option_dict)
54
- else:
55
- front_img_bytearray = create_card_image(model_no, img_bytearray, option_dict)
56
-
57
- # Retrieve the card image (back side)
58
- back_path = constants.back_card_img_dict[model_no]
59
- back_img = PIL_Image.open(back_path)
60
- back_img_bytearray = BytesIO()
61
- back_img.convert('RGB').save(back_img_bytearray, "JPEG", quality=95)
62
- back_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data
 
 
 
 
 
63
 
64
  # Create a 3D model of the card (return value is the path of the created model)
65
  model_path = create_card_model(front_img_bytearray, back_img_bytearray, option_dict)
@@ -67,9 +73,9 @@ def create_3dmodel(model_no, title, color, mark, historic_site_type, difficulty,
67
  else:
68
  # Create a 3D model of the card (return value is the path of the created model)
69
  if model_no == 'A':
70
- model_path = create_picture_box_model(img_bytearray)
71
  if model_no == 'B':
72
- model_path = create_extracted_objects_model(img_bytearray)
73
 
74
  return model_path
75
 
@@ -90,29 +96,28 @@ with gr.Blocks() as demo:
90
  with gr.Row():
91
  with gr.Column():
92
  title = gr.Textbox(label=display_message_dict['label_title'], placeholder=display_message_dict['placeholder_title'])
93
- model_type = gr.Radio([(model_type_dict[key], key) for key in model_type_dict], value='2', label=display_message_dict['label_card_type'])
94
 
95
  with gr.Row():
96
  with gr.Column():
97
- card_design_dropdown = gr.Dropdown(list(default_card_design_image_dict.keys()), label=display_message_dict['label_card_design_sample'])
98
- card_design_upload = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="868:1213", label=display_message_dict['label_card_design_upload'])
99
  with gr.Column():
100
- card_design_preview = gr.Image(label=display_message_dict['label_card_design_preview'], type="pil")
101
  card_design_upload.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Upload'),
102
- inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview)
103
  card_design_dropdown.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Drop'),
104
- inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview)
105
 
106
  with gr.Column():
107
  description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
108
  is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
109
- image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])
110
 
111
  button = gr.Button(display_message_dict['label_button'])
112
  button.click(
113
- fn=lambda model_type, title, description, is_thick, image:
114
- create_3dmodel(model_type, title, None, None, None, None, description, is_thick, image),
115
- inputs=[model_type, title, description, is_thick, image],
116
  outputs=[gr.Model3D(camera_position=(90, 90, 5))]
117
  )
118
 
@@ -133,13 +138,13 @@ with gr.Blocks() as demo:
133
  difficulty = gr.Slider(1, 5, 3, step=1, label=display_message_dict['label_difficulty'])
134
  description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
135
  is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
136
- image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])
137
 
138
  button = gr.Button(display_message_dict['label_button'])
139
  button.click(
140
- fn=lambda title, color, mark, historic_site_type, difficulty, description, is_thick, image:
141
- create_3dmodel('1', title, color, mark, historic_site_type, difficulty, description, is_thick, image),
142
- inputs=[title, color, mark, historic_site_type, difficulty, description, is_thick, image],
143
  outputs=[gr.Model3D(camera_position=(90, 90, 5))]
144
  )
145
 
 
7
 
8
  import src.constants as constants
9
 
10
+ from src.front_card_frame import create_card_frame
11
  from src.front_card_image import create_card_image
12
  from src.front_card_image_historic_site import create_historic_site_card_image
13
  from src.picture_box_model import create_picture_box_model
 
30
  else:
31
  return default_card_design_image_dict[card_design_dropdown]
32
 
33
+ def create_3dmodel(model_no, title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image, card_design_preview_image):
34
 
35
+ picture_img_bytearray = BytesIO()
36
+ picture_image['background'].save(picture_img_bytearray, "JPEG", quality=95)
37
+ picture_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.
38
 
39
  option_dict = {
40
  'タイトル': title,
 
51
  if model_no not in ['A', 'B']:
52
  # Create the card image (front side)
53
  if model_no == '1':
54
+ front_img_bytearray = create_historic_site_card_image(picture_img_bytearray, option_dict)
55
+
56
+ # Retrieve the card image (back side)
57
+ back_path = constants.back_card_img_dict[model_no]
58
+ back_img = PIL_Image.open(back_path)
59
+ back_img_bytearray = BytesIO()
60
+ back_img.convert('RGB').save(back_img_bytearray, "JPEG", quality=95)
61
+ back_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data
62
+
63
+ else:
64
+ back_img_bytearray = BytesIO()
65
+ card_design_preview_image.save(back_img_bytearray, "JPEG", quality=95)
66
+ back_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.
67
+ card_frame_bytearray = create_card_frame(back_img_bytearray)
68
+ front_img_bytearray = create_card_image(card_frame_bytearray, picture_img_bytearray, option_dict)
69
 
70
  # Create a 3D model of the card (return value is the path of the created model)
71
  model_path = create_card_model(front_img_bytearray, back_img_bytearray, option_dict)
 
73
  else:
74
  # Create a 3D model of the card (return value is the path of the created model)
75
  if model_no == 'A':
76
+ model_path = create_picture_box_model(picture_img_bytearray)
77
  if model_no == 'B':
78
+ model_path = create_extracted_objects_model(picture_img_bytearray)
79
 
80
  return model_path
81
 
 
96
  with gr.Row():
97
  with gr.Column():
98
  title = gr.Textbox(label=display_message_dict['label_title'], placeholder=display_message_dict['placeholder_title'])
 
99
 
100
  with gr.Row():
101
  with gr.Column():
102
+ card_design_dropdown = gr.Dropdown(list(default_card_design_image_dict.keys()), label=display_message_dict['label_card_design_sample'])
103
+ card_design_upload = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="868:1213", label=display_message_dict['label_card_design_upload'])
104
  with gr.Column():
105
+ card_design_preview_image = gr.Image(label=display_message_dict['label_card_design_preview'], type="pil")
106
  card_design_upload.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Upload'),
107
+ inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview_image)
108
  card_design_dropdown.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Drop'),
109
+ inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview_image)
110
 
111
  with gr.Column():
112
  description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
113
  is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
114
+ picture_image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])
115
 
116
  button = gr.Button(display_message_dict['label_button'])
117
  button.click(
118
+ fn=lambda title, description, is_thick, picture_image, card_design_preview_image:
119
+ create_3dmodel(None, title, None, None, None, None, description, is_thick, picture_image, card_design_preview_image),
120
+ inputs=[title, description, is_thick, picture_image, card_design_preview_image],
121
  outputs=[gr.Model3D(camera_position=(90, 90, 5))]
122
  )
123
 
 
138
  difficulty = gr.Slider(1, 5, 3, step=1, label=display_message_dict['label_difficulty'])
139
  description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
140
  is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
141
+ picture_image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])
142
 
143
  button = gr.Button(display_message_dict['label_button'])
144
  button.click(
145
+ fn=lambda title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image:
146
+ create_3dmodel('1', title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image, None),
147
+ inputs=[title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image],
148
  outputs=[gr.Model3D(camera_position=(90, 90, 5))]
149
  )
150
 
src/front_card_frame.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageDraw
2
+ from io import BytesIO
3
+
4
+ # Pixel position information for each area of the card image
5
+
6
+ # Card
7
+ CARD_SIZE = (868, 1213)
8
+
9
+ # Picture
10
+ PICTURE_WHITE_LT_XY = (65, 188)
11
+ PICTURE_WHITE_RB_XY = (802, 925)
12
+
13
+ # Title
14
+ TITLE_BLEND_WHITE_LT_XY = (34, 29)
15
+ TITLE_BLEND_WHITE_RB_XY = (833, 149)
16
+
17
+ # Main Body of Description
18
+ DESCRIPTION_BLEND_WHITELT_XY = (34, 961)
19
+ DESCRIPTION_BLEND_WHITERB_XY = (833, 1185)
20
+
21
+ def average_with_white(img, area):
22
+
23
+ # Change alpha value
24
+ x1, y1, x2, y2 = area
25
+ for x in range(x1, x2):
26
+ for y in range(y1, y2):
27
+ r, g, b = img.getpixel((x, y))
28
+ img.putpixel((x, y), ((r + 255) // 2, (g + 255) // 2, (b + 255) // 2))
29
+
30
+ return img
31
+
32
+ def create_card_frame(img_bytearray):
33
+ card_img = Image.open(img_bytearray).convert('RGB')
34
+ card_img = card_img.resize(CARD_SIZE)
35
+ card_imgdraw = ImageDraw.Draw(card_img)
36
+
37
+ # Paint White to the Picture Area
38
+ card_img.paste((255, 255, 255), PICTURE_WHITE_LT_XY + PICTURE_WHITE_RB_XY)
39
+
40
+ # Make translucent to the Title Area
41
+ card_img = average_with_white(card_img, TITLE_BLEND_WHITE_LT_XY + TITLE_BLEND_WHITE_RB_XY)
42
+ # Draw black rectangle frame to the Title Area
43
+ card_imgdraw.rectangle(TITLE_BLEND_WHITE_LT_XY + TITLE_BLEND_WHITE_RB_XY, outline=(0, 0, 0), width=5)
44
+
45
+ # Make translucent to the Description Area
46
+ card_img = average_with_white(card_img, DESCRIPTION_BLEND_WHITELT_XY + DESCRIPTION_BLEND_WHITERB_XY)
47
+ # Draw rectangle frame to the Description Area
48
+ card_imgdraw.rectangle(DESCRIPTION_BLEND_WHITELT_XY + DESCRIPTION_BLEND_WHITERB_XY, outline=(0, 0, 0), width=5)
49
+
50
+ # Outputting Binary Data
51
+ output_img_bytearray = BytesIO()
52
+ card_img.save(output_img_bytearray, "PNG", quality=95)
53
+ output_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data
54
+
55
+ return output_img_bytearray
56
+
57
+ if __name__ == '__main__':
58
+ with open('data/cards/back/cats.png', 'rb') as f:
59
+ card_frame_bytearray = BytesIO(f.read())
60
+
61
+ card_frame_bytearray = create_card_frame(card_frame_bytearray)
62
+
63
+ with open('test.jpeg', 'wb') as f:
64
+ f.write(card_frame_bytearray.read())
65
+
src/front_card_image.py CHANGED
@@ -36,7 +36,7 @@ def crop_center(pil_img, crop_width, crop_height):
36
  def crop_max_square(pil_img):
37
  return crop_center(pil_img, min(pil_img.size), min(pil_img.size))
38
 
39
- def create_card_image(model_no, img_bytearray, option_dict):
40
 
41
  # Loading and Cropping of Picture
42
  picture_img = Image.open(img_bytearray)
@@ -44,7 +44,7 @@ def create_card_image(model_no, img_bytearray, option_dict):
44
  picture_img = picture_img.resize(PICTURE_SIZE)
45
 
46
  # Loading of Card Frame
47
- card_img = Image.open(front_card_img_dict[model_no])
48
 
49
  # Embedding a Picture in the Card
50
  card_img.paste(picture_img, PICTURE_LT_XY)
 
36
  def crop_max_square(pil_img):
37
  return crop_center(pil_img, min(pil_img.size), min(pil_img.size))
38
 
39
+ def create_card_image(card_frame_bytearray, img_bytearray, option_dict):
40
 
41
  # Loading and Cropping of Picture
42
  picture_img = Image.open(img_bytearray)
 
44
  picture_img = picture_img.resize(PICTURE_SIZE)
45
 
46
  # Loading of Card Frame
47
+ card_img = Image.open(card_frame_bytearray)
48
 
49
  # Embedding a Picture in the Card
50
  card_img.paste(picture_img, PICTURE_LT_XY)