Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
only one device output
Browse files Browse the repository at this point in the history
  • Loading branch information
exherb committed May 12, 2015
1 parent 6ff8c10 commit 9031848
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 155 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ just type `pip install icons` in your term.

```
icons [--type {launch,notification,image,tab,webclip,toolbar,icon}]
[--devices {ios,android} [--zip]
[--device {ios,android} [--zip]
icon_path [-o TARGET_PATH] [--baseline BASELINE]
# with gui
icons_gui
Expand All @@ -35,7 +35,7 @@ icons_gui
* target_path(optional) - the target path
* baseline(optional) - baseline scale. the default is 3, and it's mean the source image is at @3x scale
* type(optional) - icon type, the default is icon
* devices(optional) - including only devices, the default is iOS
* device(optional) - including only devices, the default is iOS
* --zip(optional) - put icons into a zip file instead of directory

![screenshot](screenshots/icons_gui.png)
Expand Down
186 changes: 89 additions & 97 deletions icons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,107 +676,99 @@ def supported_types():


def make_images(image, image_name, to_object, type,
allowed_devices=None, baseline_scale=2):
to_device, baseline_scale=2):
original_image_width, original_image_height = image.size
if type not in _sizes_:
raise RuntimeError('Error: no such icon type')
devices = _sizes_[type]
if not allowed_devices:
allowed_devices = devices.keys()
all_devices = _sizes_[type]
if not to_device:
to_device = all_devices.keys()[0]
sizes = all_devices[to_device]
all_contents = {}
for device, sizes in devices.items():
if device not in allowed_devices:
continue
if isinstance(to_object, ZipFile):
device_path = device
if isinstance(to_object, ZipFile):
device_path = to_device
else:
device_path = to_object
if not os.path.exists(device_path):
os.makedirs(device_path)
widths = [x[0]*x[2] for x in sizes.values()
if isinstance(x[0], int)]
min_image_width = None
if min_image_width:
min_image_width = max(widths)
if min_image_width:
heights = [x[1]*x[2] for x in sizes.values()
if isinstance(x[1], int)]
min_image_height = None
if heights:
min_image_height = max(heights)
if min_image_height is None:
min_image_height = min_image_width*1.0 /\
original_image_width*original_image_height
scale = max(min_image_width*1.0/original_image_width,
min_image_height*1.0/original_image_height)
crop_image_width = min_image_width/scale
crop_image_height = min_image_height/scale
if original_image_width > crop_image_width and\
original_image_height > crop_image_height:
x_offset = (original_image_width - crop_image_width)*0.5
y_offset = (original_image_height - crop_image_height)*0.5
image = image.crop((x_offset, y_offset,
x_offset + crop_image_width,
y_offset + crop_image_height))
for name, size_info in sizes.items():
width, height, scale = size_info[:3]
if len(size_info) > 3:
system_version = size_info[3]
else:
system_version = None
if width is None:
width = original_image_width*1.0/baseline_scale
if height is None:
height = width*1.0/original_image_width*original_image_height
name = name.replace('{filename}', image_name)
image_path = os.path.join(device_path, name)
_, ext = os.path.splitext(name)
if ext.lower() == '.ico':
with open(image_path, 'wb') as f:
_save_to_ico(image, f)
else:
device_path = os.path.join(to_object, device)
if not os.path.exists(device_path):
os.makedirs(device_path)
elif not os.path.isdir(device_path):
print('Warning: can\'t make dir {} for {}'.
format(device_path, device))
if not ext:
image_path = image_path + '.png'
if scale > baseline_scale:
print(('Warning: {} scale {} is bigger than ' +
'base line scale {}').
format(image_path, scale, baseline_scale))
continue
if not sizes:
continue
widths = [x[0]*x[2] for x in sizes.values()
if isinstance(x[0], int)]
min_image_width = None
if min_image_width:
min_image_width = max(widths)
if min_image_width:
heights = [x[1]*x[2] for x in sizes.values()
if isinstance(x[1], int)]
min_image_height = None
if heights:
min_image_height = max(heights)
if min_image_height is None:
min_image_height = min_image_width*1.0 /\
original_image_width*original_image_height
scale = max(min_image_width*1.0/original_image_width,
min_image_height*1.0/original_image_height)
crop_image_width = min_image_width/scale
crop_image_height = min_image_height/scale
if original_image_width > crop_image_width and\
original_image_height > crop_image_height:
x_offset = (original_image_width - crop_image_width)*0.5
y_offset = (original_image_height - crop_image_height)*0.5
image = image.crop((x_offset, y_offset,
x_offset + crop_image_width,
y_offset + crop_image_height))
for name, size_info in sizes.items():
width, height, scale = size_info[:3]
if len(size_info) > 3:
system_version = size_info[3]
else:
system_version = None
if width is None:
width = original_image_width*1.0/baseline_scale
if height is None:
height = width*1.0/original_image_width*original_image_height
name = name.replace('{filename}', image_name)
image_path = os.path.join(device_path, name)
_, ext = os.path.splitext(name)
if ext.lower() == '.ico':
with open(image_path, 'wb') as f:
_save_to_ico(image, f)
else:
if not ext:
image_path = image_path + '.png'
if scale > baseline_scale:
print(('Warning: {} scale {} is bigger than ' +
'base line scale {}').
format(image_path, scale, baseline_scale))
if not isinstance(to_object, ZipFile):
image_dir_path = os.path.dirname(image_path)
if not os.path.exists(image_dir_path):
os.makedirs(image_dir_path)
elif not os.path.isdir(image_dir_path):
print('Warning: can\'t make dir {} for {}({}x{})'.
format(image_dir_path, image_path, width*scale,
height*scale))
continue
if not isinstance(to_object, ZipFile):
image_dir_path = os.path.dirname(image_path)
if not os.path.exists(image_dir_path):
os.makedirs(image_dir_path)
elif not os.path.isdir(image_dir_path):
print('Warning: can\'t make dir {} for {}({}x{})'.
format(image_dir_path, image_path, width*scale,
height*scale))
continue
if os.path.exists(image_path):
print('Warning: {} is already exists'.
format(image_path))
continue
if width > original_image_width or\
height > original_image_height:
print('Warning: {}x{} is too small for {}({}x{})'.
format(original_image_width, original_image_height,
image_path, width*scale, height*scale))
if os.path.exists(image_path):
print('Warning: {} is already exists'.
format(image_path))
continue
_resize_image_(image, to_object, image_path,
(width*scale, height*scale))

for type in _configs_.keys():
if type in name:
_modify_config_file_(type, all_contents, image_path,
(width, height),
scale,
system_version)
break
if width > original_image_width or\
height > original_image_height:
print('Warning: {}x{} is too small for {}({}x{})'.
format(original_image_width, original_image_height,
image_path, width*scale, height*scale))
continue
_resize_image_(image, to_object, image_path,
(width*scale, height*scale))

for type in _configs_.keys():
if type in name:
_modify_config_file_(type, all_contents, image_path,
(width, height),
scale,
system_version)
break
_save_configs_(to_object, all_contents)


Expand All @@ -799,10 +791,10 @@ def _main_():
choices=supported_types(),
dest='icon_type',
help='icon type')
parser.add_argument('--devices', '-d',
parser.add_argument('--device', '-d',
default='ios',
choices=['ios', 'android'],
help='including devices')
help='device')
parser.add_argument('--zip', '-z', action='store_const', const=True)
args = parser.parse_args()
if not args.target_path:
Expand All @@ -820,10 +812,10 @@ def _main_():
with closing(ZipFile(args.target_path,
"w", ZIP_DEFLATED)) as zip_file:
make_images(image, image_name, zip_file, args.icon_type,
args.devices, args.baseline)
args.device, args.baseline)
else:
make_images(image, image_name, args.target_path, args.icon_type,
args.devices, args.baseline)
args.device, args.baseline)

if __name__ == '__main__':
_main_()
86 changes: 31 additions & 55 deletions icons/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def _main_():
icon_types.append(('notification',
'Notification Icon'))
icon_type = tk.StringVar(window, 'icon')

device_types = []
device_type = tk.StringVar(window, 'device')

baseline = tk.IntVar(window, 3, 'baseline')

Expand Down Expand Up @@ -187,38 +186,20 @@ def _main_():
variable=baseline, value=4,
text='4').pack(anchor='w')

toggle_icon_types = tk.BooleanVar(window, True)

def on_toggle_icon_types(*args):
state = toggle_icon_types.get()
for device_type in device_types:
device_type[0].set(state)
toggle_icon_types.trace('w', on_toggle_icon_types)

def on_icon_type_changed(*args):
raw_icon_type = icon_type.get()
devices = supported_devices(raw_icon_type)
selected_devices = set()
for (device_var, device, _) in device_types:
if device_var.get():
selected_devices.add(device)
del device_types[:]
toggle_icon_types.set(False)
for device in devices:
device_types.append((tk.BooleanVar(window,
device in selected_devices),
device,
device_name(device)))

devices_frame = tk.LabelFrame(frame, bg=background_color,
text='Devices')
for device_type in device_types:
tk.Checkbutton(devices_frame, bg=background_color,
variable=device_type[0],
text=device_type[2]).pack(anchor='w')
if len(device_types) > 1:
tk.Checkbutton(devices_frame, bg=background_color,
variable=toggle_icon_types,
text='All').pack(anchor='w')
if device_type.get() not in devices:
device_type.set(devices[0])
for device in devices:
tk.Radiobutton(devices_frame, bg=background_color,
variable=device_type,
value=device,
text=device_name(device)).pack(anchor='w')

for old_frame in frame.grid_slaves(row=1, column=1):
old_frame.grid_remove()
devices_frame.grid(row=1, column=1, sticky='nwne', padx=10)
Expand Down Expand Up @@ -324,32 +305,27 @@ def on_select_icon(event):
askopenfilename(title='Select your icon',
filetypes=[('Images', '.png .jpg .jpeg .bmp')])
if icon_path:
to_devices = []
for device_type in device_types:
if device_type[0].get():
to_devices.append(device_type[1])
if to_devices:
try:
image = Image.open(icon_path)
except Exception:
return

image_name, _ = os.path.splitext(os.path.basename(icon_path))
if output_path.get():
to_output_path = output_path.get()
else:
to_output_path = os.path.dirname(icon_path)
to_icon_type = icon_type.get()
to_baseline = baseline.get()
progressbar.add_task(target=make_images,
args=(image, image_name,
to_output_path,
to_icon_type,
to_devices, to_baseline))

def callback():
_show_in_finder_(to_output_path)
progressbar.start(callback)
try:
image = Image.open(icon_path)
except Exception:
return

image_name, _ = os.path.splitext(os.path.basename(icon_path))
if output_path.get():
to_output_path = output_path.get()
else:
to_output_path = os.path.dirname(icon_path)
to_icon_type = icon_type.get()
to_baseline = baseline.get()
progressbar.add_task(target=make_images,
args=(image, image_name,
to_output_path,
to_icon_type,
device_type.get(), to_baseline))

def callback():
_show_in_finder_(to_output_path)
progressbar.start(callback)
window.is_picking_file = False
drop_button.bind('<ButtonRelease-1>', on_select_icon)
dnd = TkDND(window)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


setup(name='icons',
version='0.1',
version='0.1.1',
description='generate icons of all sizes and config file ' +
'(e.g. Contents.json) required by iOS or Android app, ' +
'inspired by http://makeappicon.com',
Expand Down

0 comments on commit 9031848

Please sign in to comment.