Diary

Python Script to Convert All JPEG Files in a Specified Directory to PNG Format

1 Mins read
# Python script to convert all JPEG files in a specified directory to PNG format
# Python 3
# pip install Pillow
#
# Command below does not delete original files
# python jpeg-png.py
#
# Command below deletes original files
# python jpeg-png.py rm=1


from PIL import Image
import os
import sys


def convert_jpeg_to_png(directory, remove_jpeg=False):
for root, dirs, files in os.walk(directory):
for file in files:
if file.lower().endswith('.jpg') or file.lower().endswith('.jpeg'):
jpeg_path = os.path.join(root, file)
png_path = os.path.splitext(jpeg_path)[0] + '.png'


try:
image = Image.open(jpeg_path)
image.save(png_path, format='PNG')
print(f"{jpeg_path}を{png_path}に変換しました")


if remove_jpeg:
os.remove(jpeg_path)
print(f"{jpeg_path}を削除しました")
except Exception as e:
print(f"{jpeg_path}の変換に失敗しました: {e}")


# Get and specify current directory
current_directory = os.getcwd()


# Check command line arguments
remove_jpeg = False
if len(sys.argv) > 1 and sys.argv[1] == 'rm=1':
remove_jpeg = True


convert_jpeg_to_png(current_directory, remove_jpeg)
Read more
Diary

Python script to convert all PNG files in a specified directory to JPEG format

1 Mins read
Python script to convert all PNG files in a specified directory to JPEG format
# Python script to convert all PNG files in a specified directory to JPEG format
# python3
# pip install Pillow
#
# Don't delete the file below
# python png-jpeg.py
#
# Delete the file below
# python png-jpeg.py rm=1


from PIL import Image
import os
import sys


def convert_png_to_jpeg(directory, remove_png=False):
for root, dirs, files in os.walk(directory):
for file in files:
if file.lower().endswith('.png'):
png_path = os.path.join(root, file)
jpeg_path = os.path.splitext(png_path)[0] + '.jpg'


try:
image = Image.open(png_path)
rgb_im = image.convert('RGB')
rgb_im.save(jpeg_path, quality=100)
print(f"{png_path}を{jpeg_path}に変換しました")


if remove_png:
os.remove(png_path)
print(f"{png_path}を削除しました")
except Exception as e:
print(f"{png_path}の変換に失敗しました: {e}")


# Get and specify the current directory
current_directory = os.getcwd()


# Check command line arguments
remove_png = False
if len(sys.argv) > 1 and sys.argv[1] == 'rm=1':
remove_png = True


convert_png_to_jpeg(current_directory, remove_png)

 

Read more
Diary

[Mac] Port 5000 Unavailable in Monterey

1 Mins read

Developing with port 5000 but getting an error?

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5000 -> 0.0.0.0:0: listen tcp 0.0.0.0:5000: bind: address already in use

lsof -i:5000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ControlCe 85856 user10 26u IPv4 0x4ff853972c8bedd 0t0 TCP *:commplex-main (LISTEN)
ControlCe 85856 user10 27u IPv6 0x4ff85430b21e97d 0t0 TCP *:commplex-main (LISTEN)

What’s this thing?

Turns out Monterey added an AirPlay feature that’s hijacking the port.

System Preference > Sharing > AirPlay Receiver > Uncheck it

That’ll free it up, but thinking ahead, other people will hit the same issue. Better to just change the dev side to use a different port.

Read more
Diary

[Mac] Terminal and Operation not permitted – Configure for Developer Use

1 Mins read

macOS Monterey
Version 12.4

■1. Change “SIP” to disabled
(Disable System Integrity Protection: SIP)

Shut down Mac once
At startup, press “command + R”, release when the mark appears
Recovery mode starts
From the menu bar at the top, select “Open Terminal”

# Command: Check if "csrutil status" returns "enabled"
csrutil status

# Command: Run "csrutil disable" (changes take effect only after reboot!)
csrutil disable

# Command: Reboot Mac with "reboot"
reboot

■2. Set Terminal to full file access
After normal startup
Go to “System Preferences”

“Security & Privacy”

“Privacy” tab, then “Full Disk Access” on the right

Check the “Terminal” checkbox On

■3. Delete “.DS_Store” and prevent creation

# Delete all .DS_Store files
sudo find / -name ".DS_Store" -delete
# Restart Finder
Killall Finder

# Prevent .DS_Store files from being created at all
defaults write com.apple.desktopservices DSDontWriteNetworkStores True
# Restart Finder
Killall Finder

■4. If not needed, disable iCloud sync

Read more
Diary

[Mac] Limiting VPN Connection Destinations

1 Mins read

When connected to a VPN, route traffic destined for networks outside the VPN connection (non-VPN destination networks) directly through your local network instead of through the VPN tunnel. This keeps network traffic responsive.

※If you’re working remotely, check with your IT department first—some organizations require all traffic to route through internal networks.

※Be aware that many external servers are configured to allow access only through the VPN tunnel.

※If accessing the VPN destination by domain name, watch out for DNS settings. Depending on the VPN’s DNS configuration, you may need to edit the hosts file.

macOS Monterey
Version 12.3
VPN Connection Method: L2TP/IPsec (PPP tunneling)

■Open Network Settings and access VPN connection details
Uncheck “Send all traffic over VPN connection”
設定画像

■Create a routing addition script
The /etc/ppp/ip-up script is executed when the connection is established.
Add the following to this file to “add IP routes when VPN connects”.
In this example, “172.31.1.0/24” is the route you want to send through the VPN.

#Confirm ppp0 exists after VPN connection
$ ifconfig

#After disconnecting the VPN
#Edit the file with vi
$ sudo vi /etc/ppp/ip-up
#!/bin/sh

if [ "$1" = "ppp0" ]; then
    /sbin/route add -net 172.31.1.0/24 -interface ppp0
fi

#Save the file in vi
#Give the file execute permission
$ sudo chmod +x /etc/ppp/ip-up

#Check the routing table
$ netstat -rn

Notes
When routing multiple paths, you can add multiple entries:
/sbin/route add -net 172.31.1.0/22 -interface ppp0
/sbin/route add -net 172.31.4.0/22 -interface ppp0
/sbin/route add -net 172.31.8.0/22 -interface ppp0

Read more