radames HF staff commited on
Commit
fc59b67
β€’
1 Parent(s): 33e1a30

compress resize PNGs

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -7,13 +7,15 @@ import json
7
  from datasets import load_dataset
8
  from flask import Flask
9
  from flask_cors import CORS
 
10
 
11
  app = Flask(__name__, static_url_path='/static')
12
 
13
  CORS(app)
14
 
15
  TOKEN = os.environ.get('dataset_token')
16
- dataset = load_dataset("huggingface-projects/wordalle_prompts", use_auth_token=TOKEN)
 
17
  Path("static/images").mkdir(parents=True, exist_ok=True)
18
 
19
  # extract images and prompts from dataset and save to dis
@@ -22,8 +24,9 @@ for row in dataset['train']:
22
  prompt = dataset['train'].features['label'].int2str(row['label'])
23
  image = row['image']
24
  hash = uuid.uuid4().hex
25
- image_file = Path(f'static/images/{hash}.png')
26
- image.save(image_file)
 
27
  if prompt not in data:
28
  data[prompt] = []
29
  data[prompt].append(str(image_file))
@@ -36,6 +39,7 @@ with open('static/data.json', 'w') as f:
36
  def index():
37
  return app.send_static_file('index.html')
38
 
 
39
  @app.route('/data')
40
  def getdata():
41
  return app.send_static_file('data.json')
 
7
  from datasets import load_dataset
8
  from flask import Flask
9
  from flask_cors import CORS
10
+ from PIL import Image
11
 
12
  app = Flask(__name__, static_url_path='/static')
13
 
14
  CORS(app)
15
 
16
  TOKEN = os.environ.get('dataset_token')
17
+ dataset = load_dataset(
18
+ "huggingface-projects/wordalle_prompts", use_auth_token=TOKEN)
19
  Path("static/images").mkdir(parents=True, exist_ok=True)
20
 
21
  # extract images and prompts from dataset and save to dis
 
24
  prompt = dataset['train'].features['label'].int2str(row['label'])
25
  image = row['image']
26
  hash = uuid.uuid4().hex
27
+ image_file = Path(f'static/images/{hash}.jpg')
28
+ image_compress = image.resize((136, 136), Image.Resampling.LANCZOS)
29
+ image_compress.save(image_file, optimize=True, quality=95)
30
  if prompt not in data:
31
  data[prompt] = []
32
  data[prompt].append(str(image_file))
 
39
  def index():
40
  return app.send_static_file('index.html')
41
 
42
+
43
  @app.route('/data')
44
  def getdata():
45
  return app.send_static_file('data.json')