admin commited on
Commit
5c3d7cf
·
1 Parent(s): 284d622

enable card size

Browse files
Files changed (1) hide show
  1. src/modules/bitly-api.min.py +59 -20
src/modules/bitly-api.min.py CHANGED
@@ -94,7 +94,7 @@ class UrlProcessor:
94
  return None, None, {"error": str(e)}
95
 
96
  @staticmethod
97
- def shorten_and_generate_qr(access_token, api_url, text, shorten, generate_qr, common_images, individual_images):
98
  bitly = UrlProcessor(access_token, api_url)
99
  urls = re.findall(r'http[s]?://\S+', text)
100
  results = []
@@ -106,6 +106,8 @@ class UrlProcessor:
106
  qr_pngs = []
107
  card_htmls = []
108
 
 
 
109
  for idx, url in enumerate(urls):
110
  shorten_url, shorten_response_json = None, None
111
  try:
@@ -164,7 +166,7 @@ class UrlProcessor:
164
 
165
  for idx, png in enumerate(qr_pngs):
166
  try:
167
- png = ImageProcessor.combine_images(layer, [png])
168
  if common_images:
169
  png = ImageProcessor.combine_images(png, common_images)
170
  if individual_images:
@@ -261,33 +263,43 @@ class TextProcessor:
261
  class ImageProcessor:
262
 
263
 
 
264
  def create_layer(layout='horizontal', size=(91 * 3.7795, 55 * 3.7795)):
 
265
  size = (int(size[0]), int(size[1])) # Convert to integers
266
  img = Image.new('RGBA', size, (255, 255, 255, 0))
267
  draw = ImageDraw.Draw(img)
268
  if layout == 'vertical':
269
- draw.rectangle([(0, 0), (size[0], size[1])], outline="black", width=3)
270
  elif layout == 'horizontal':
271
- draw.rectangle([(0, 0), (size[1], size[0])], outline="black", width=3)
272
 
273
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmpfile:
274
- img.save(tmpfile.name, format="PNG")
275
- return tmpfile.name
276
 
277
  @staticmethod
278
- def combine_images(base_image_path, overlay_image_paths):
279
- with open(base_image_path, 'rb') as f:
280
- base_img = Image.open(f).convert("RGBA")
281
- for overlay_path in overlay_image_paths:
282
- with open(overlay_path, 'rb') as f:
283
- overlay_img = Image.open(f).convert("RGBA")
284
- # overlay_img = overlay_img.resize(base_img.size, Image.NEAREST )
285
- base_img = ImageOps.contain(overlay_img, base_img.size)
286
-
287
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmpfile:
288
- base_img.save(tmpfile.name, format="PNG")
289
- return tmpfile.name
 
 
 
290
 
 
 
 
 
 
291
  # @staticmethod
292
  # def insert_txt(png_path, text):
293
  # img = Image.open(png_path)
@@ -316,6 +328,21 @@ class ImageProcessor:
316
  # img.save(tmpfile.name, format="PNG")
317
  # return tmpfile.name
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  class Interface:
320
  @staticmethod
321
  def get_tempdir():
@@ -353,10 +380,18 @@ with gr.Blocks() as demo:
353
  shorten_checkbox = gr.Checkbox(label="Shorten URL", value=True)
354
  qr_checkbox = gr.Checkbox(label="Generate QR Code", value=True)
355
  submit_button = gr.Button("Process URLs", variant='primary')
 
356
  with gr.Accordion(open=False, label="Insert"):
357
  with gr.Row():
358
  cm_img_input = gr.Files(label='Insert common images')
359
  idv_img_input = gr.Files(label='Insert individual images')
 
 
 
 
 
 
 
360
  with gr.Column():
361
  file_output = gr.File(label="Download ZIP")
362
  with gr.Accordion(open=True, label="Gallery"):
@@ -388,7 +423,11 @@ with gr.Blocks() as demo:
388
  shorten_checkbox,
389
  qr_checkbox,
390
  cm_img_input,
391
- idv_img_input],
 
 
 
 
392
  outputs=[preview_output, gallery_output, file_output, json_output]
393
  )
394
 
 
94
  return None, None, {"error": str(e)}
95
 
96
  @staticmethod
97
+ def shorten_and_generate_qr(access_token, api_url, text, shorten, generate_qr, common_images, individual_images, pos_x, pos_y, pos_p):
98
  bitly = UrlProcessor(access_token, api_url)
99
  urls = re.findall(r'http[s]?://\S+', text)
100
  results = []
 
106
  qr_pngs = []
107
  card_htmls = []
108
 
109
+ positions = [(pos_p, pos_p), (int(0.9 * pos_x * 3.7795), int(0.9 * pos_y * 3.7795))]
110
+
111
  for idx, url in enumerate(urls):
112
  shorten_url, shorten_response_json = None, None
113
  try:
 
166
 
167
  for idx, png in enumerate(qr_pngs):
168
  try:
169
+ png = ImageProcessor.combine_images(layer, [png], positions)
170
  if common_images:
171
  png = ImageProcessor.combine_images(png, common_images)
172
  if individual_images:
 
263
  class ImageProcessor:
264
 
265
 
266
+ @staticmethod
267
  def create_layer(layout='horizontal', size=(91 * 3.7795, 55 * 3.7795)):
268
+ ts, temp_dir = Interface.get_tempdir()
269
  size = (int(size[0]), int(size[1])) # Convert to integers
270
  img = Image.new('RGBA', size, (255, 255, 255, 0))
271
  draw = ImageDraw.Draw(img)
272
  if layout == 'vertical':
273
+ draw.rectangle([(0, 0), (size[0], size[1])], outline="lightgray")
274
  elif layout == 'horizontal':
275
+ draw.rectangle([(0, 0), (size[0], size[1])], outline="lightgray")
276
 
277
+ file_path = os.path.join(temp_dir, 'layer.png')
278
+ img.save(file_path, format="PNG")
279
+ return file_path
280
 
281
  @staticmethod
282
+ def combine_images(base_image_path, overlay_image_paths, positions=None, temp_dir=None):
283
+ try:
284
+ if temp_dir is None:
285
+ _, temp_dir = Interface.get_tempdir()
286
+ with open(base_image_path, 'rb') as f:
287
+ base_img = Image.open(f).convert("RGBA")
288
+ for i, overlay_path in enumerate(overlay_image_paths):
289
+ with open(overlay_path, 'rb') as f:
290
+ overlay_img = Image.open(f).convert("RGBA")
291
+ if positions and i < len(positions):
292
+ position = positions[i]
293
+ else:
294
+ position = (0, 0)
295
+ overlay_img_resized = ImageOps.contain(overlay_img, base_img.size)
296
+ base_img.paste(overlay_img_resized, position, overlay_img_resized)
297
 
298
+ file_path = os.path.join(temp_dir, 'combined.png')
299
+ base_img.save(file_path, format="PNG")
300
+ return file_path
301
+ except Exception as e:
302
+ raise Exception(f"Error combining images: {e}")
303
  # @staticmethod
304
  # def insert_txt(png_path, text):
305
  # img = Image.open(png_path)
 
328
  # img.save(tmpfile.name, format="PNG")
329
  # return tmpfile.name
330
 
331
+ @staticmethod
332
+ def move_image(image_path, position, temp_dir=None):
333
+ try:
334
+ if temp_dir is None:
335
+ _, temp_dir = Interface.get_tempdir()
336
+ with open(image_path, 'rb') as f:
337
+ img = Image.open(f).convert("RGBA")
338
+ result_img = Image.new("RGBA", img.size, (255, 255, 255, 0))
339
+ result_img.paste(img, position, img)
340
+ file_path = os.path.join(temp_dir, 'moved.png')
341
+ result_img.save(file_path, format="PNG")
342
+ return file_path
343
+ except Exception as e:
344
+ raise Exception(f"Error moving image: {e}")
345
+
346
  class Interface:
347
  @staticmethod
348
  def get_tempdir():
 
380
  shorten_checkbox = gr.Checkbox(label="Shorten URL", value=True)
381
  qr_checkbox = gr.Checkbox(label="Generate QR Code", value=True)
382
  submit_button = gr.Button("Process URLs", variant='primary')
383
+
384
  with gr.Accordion(open=False, label="Insert"):
385
  with gr.Row():
386
  cm_img_input = gr.Files(label='Insert common images')
387
  idv_img_input = gr.Files(label='Insert individual images')
388
+
389
+ with gr.Row():
390
+ pos_x = gr.Number(91, label="Position X", minimum=0, maximum=1000, step=1)
391
+ pos_y = gr.Number(55, label="Position Y", minimum=0, maximum=1000, step=1)
392
+ pos_p = gr.Number(10, label="Position Padding", minimum=0, maximum=1000, step=1)
393
+
394
+ # positions = [(10, 10), (int(0.9 * s * 3.7795), int(0.9 * 55 * 3.7795))]
395
  with gr.Column():
396
  file_output = gr.File(label="Download ZIP")
397
  with gr.Accordion(open=True, label="Gallery"):
 
423
  shorten_checkbox,
424
  qr_checkbox,
425
  cm_img_input,
426
+ idv_img_input,
427
+ pos_x,
428
+ pos_y,
429
+ pos_p,
430
+ ],
431
  outputs=[preview_output, gallery_output, file_output, json_output]
432
  )
433