Spaces:
Runtime error
Runtime error
File size: 4,889 Bytes
ca9772c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
from PIL import Image
import math
import os
import subprocess
from .tele import SendToTele
class GeneratePhoto:
def __init__(self,paper_size,gap,images,img_max_width,img_max_height,raw_export):
self.paper_size = paper_size
self.gap = gap
self.images = images
self.img_max_width = img_max_width
self.img_max_height =img_max_height
self.raw_export = raw_export
def deleteimage(self,file_path):
# Check if the file exists before deleting
if os.path.exists(file_path):
# Delete the file
os.remove(file_path)
print("File deleted successfully.")
else:
print("File does not exist.")
def export(self):
paper_size = self.paper_size
gap = self.gap
images = self.images
img_max_width = self.img_max_width
img_max_height = self.img_max_height
#Calculate the images that how much fit inside the paper
limited_img_xaxis = int(paper_size[0] / (img_max_width + gap))
limited_img_yaxis = int(paper_size[1] / (img_max_height + gap))
limited_paper_images = limited_img_xaxis * limited_img_yaxis
img_qty = 0;
for (a,b) in images:
img_qty += int(b)
# print(math.ceil(img_qty/limited_paper_images))
# print('limited image',limited_paper_images)
# print(img_qty)
paper_qty = math.ceil(img_qty/limited_paper_images)
sets = [] # Initialize an empty list of sets
current_set = [] # Initialize an empty list to hold the images for the current set
# Iterate over the images
for image in images:
name, count = image
# Add the current image to the current set count times
for i in range(int(count)):
# If adding the current image would exceed the limit, add the current set to the list of sets and start a new set
if len(current_set) == limited_paper_images:
sets.append(current_set)
current_set = []
current_set.append(name)
# Add the last set to the list of sets
if current_set:
sets.append(current_set)
# Count the duplicate image names in each set and print the sets
count_set = []
for i, set in enumerate(sets):
name_counts = {}
for name in set:
if name in name_counts:
name_counts[name] += 1
else:
name_counts[name] = 1
# print(f"Name counts: {name_counts}")
count_set.append(name_counts)
print(count_set)
pname = 0
for p in count_set:
a4_image = Image.new('RGB', paper_size, color='white')
values_list = list(p.values())
key_list = list(p.keys())
imgs_sum = sum(values_list)
index = 0
print('Img : ', self.raw_export +os.path.basename(key_list[index]) )
img = Image.open(self.raw_export + os.path.basename(key_list[index]))
self.deleteimage(self.raw_export + os.path.basename(key_list[index]))
#Change pictures to Thumbnail
img = img.resize((int(img_max_width),int(img_max_height)),resample=Image.BICUBIC) #Resize Image
print(img.size,'Image size')
img.thumbnail((img_max_width,img_max_height))
for a in range(imgs_sum):
x_pos = a % limited_img_xaxis
y_pos = a // limited_img_xaxis
print(f"({x_pos}, {y_pos})")
position = (int(x_pos*img_max_width)+(gap*x_pos)+gap), int((y_pos*img_max_height)+(gap*y_pos)+gap)
print(position)
a4_image.paste(img,position)
if a == sum(values_list[:index+1])-1:
index += 1
try:
img = Image.open(self.raw_export+os.path.basename(key_list[index]))
img = img.resize((int(img_max_width),int(img_max_height)),resample=Image.BICUBIC) #Resize Image
print(img.size,'Resize Image')
img.thumbnail((img_max_width,img_max_height))
self.deleteimage(self.raw_export + os.path.basename(key_list[index]))
except IndexError:
print(IndexError)
pname += 1
pathname = 'paper/paper'+str(pname)+'.png'
a4_image.save(pathname)
SendToTele(document_path=pathname)
self.deleteimage(pathname)
|