Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6963cfcb-30dc-8008-a9d7-31c2ca4736e8
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{DISPLAYTITLE:Bash script invert colors}} ''This conversation was exported from ChatGPT''<ref>{{cite web|title=Bash script invert colors|url=https://chatgpt.com/share/6963cfcb-30dc-8008-a9d7-31c2ca4736e8|publisher=ChatGPT|access-date=2026-01-13}}</ref> == gpt-5-1 == === User: Write a bash script to invert color in png or jpg files. === Write a bash script to invert color in png or jpg files. === Assistant: Below is a clean, production-ready Bash script that inverts the colors of PNG or JPG files. === Below is a clean, production-ready Bash script that inverts the colors of PNG or JPG files. It uses ImageMagick’s convert (or magick on newer systems). The script handles single files, multiple files, or whole directories, and writes inverted images with a suffix. === <syntaxhighlight lang="bash">#!/bin/bash === === Exit on error === set -e === Confirm ImageMagick is installed === if ! command -v convert >/dev/null 2>&1 && ! command -v magick >/dev/null 2>&1; then echo "Error: ImageMagick not found. Install it (sudo apt install imagemagick)." exit 1 fi === Choose the appropriate command (convert or magick convert) === if command -v magick >/dev/null 2>&1; then IM_CMD="magick convert" else IM_CMD="convert" fi === Check usage === if [ $# -lt 1 ]; then echo "Usage: $0 <file-or-directory> [...]" exit 1 fi === Process each argument === for input in "$@"; do if [ -d "$input" ]; then # Directory: process supported files inside shopt -s nullglob for f in "$input"/*.{png,PNG,jpg,JPG,jpeg,JPEG}; do out="${f%.''}_inverted.${f##''.}" echo "Inverting: $f -> $out" $IM_CMD "$f" -negate "$out" done shopt -u nullglob elif [ -f "$input" ]; then # File: invert directly ext="${input##*.}" base="${input%.*}" out="${base}_inverted.${ext}" echo "Inverting: $input -> $out" $IM_CMD "$input" -negate "$out" else echo "Warning: $input is not a valid file or directory. Skipping." fi done </syntaxhighlight> === 1. Save the script as invert.sh. === # Make it executable: <syntaxhighlight lang="bash">chmod +x invert.sh </syntaxhighlight> # Run it on a file: <syntaxhighlight lang="bash">./invert.sh photo.png </syntaxhighlight> # Or on multiple files: <syntaxhighlight lang="bash">./invert.sh a.jpg b.png </syntaxhighlight> # Or on a directory: <syntaxhighlight lang="bash">./invert.sh /path/to/images/ </syntaxhighlight> Each output file will be saved as: <syntaxhighlight>originalname_inverted.png originalname_inverted.jpg </syntaxhighlight> If you want a variant that overwrites the original file, or processes nested subdirectories, let me know. === User: One more bash script to make png or jpg files square by making a square of the length or width of the original file, wh… === One more bash script to make png or jpg files square by making a square of the length or width of the original file, whichever is larger, resizing to center, and flattening with white background. === Assistant: Below is a robust Bash script that takes any PNG/JPG image, determines its larger dimension, creates a square canvas of… === Below is a robust Bash script that takes any PNG/JPG image, determines its larger dimension, creates a square canvas of that size, centers the image on a white background, and outputs a square version. It supports individual files, multiple files, and directories. === <syntaxhighlight lang="bash">#!/bin/bash === set -e === Ensure ImageMagick is available === if ! command -v convert >/dev/null 2>&1 && ! command -v magick >/dev/null 2>&1; then echo "Error: ImageMagick not installed. Install it via your package manager." exit 1 fi === Select the correct convert command === if command -v magick >/dev/null 2>&1; then IM_CMD="magick" else IM_CMD="convert" fi if [ $# -lt 1 ]; then echo "Usage: $0 <file-or-directory> [...]" exit 1 fi process_file() { f="$1" ext="${f##*.}" base="${f%.*}" out="${base}_square.${ext}" # Read dimensions read width height <<< $($IM_CMD identify -format "%w %h" "$f") # Determine longest side if [ "$width" -gt "$height" ]; then size=$width else size=$height fi echo "Squaring: $f -> $out (canvas ${size}x${size})" # Create square image, centered on white background $IM_CMD "$f" -gravity center -background white -extent "${size}x${size}" "$out" } for input in "$@"; do if [ -d "$input" ]; then shopt -s nullglob for f in "$input"/*.{png,PNG,jpg,JPG,jpeg,JPEG}; do process_file "$f" done shopt -u nullglob elif [ -f "$input" ]; then process_file "$input" else echo "Warning: '$input' is not a valid file or directory. Skipping." fi done </syntaxhighlight> === 1. Identifies width and height using ImageMagick’s identify. === # Computes the larger dimension to define a square canvas. # Places the original image centered within this square. # Fills empty space with white (-background white -extent WxH). # Saves as: <syntaxhighlight>originalname_square.png originalname_square.jpg </syntaxhighlight> === <syntaxhighlight lang="bash">chmod +x squareify.sh === ./squareify.sh image.jpg ./squareify.sh a.png b.jpeg ./squareify.sh /path/to/images/ </syntaxhighlight> If you want the variant to: * overwrite the original file, * optionally pad with black or transparent background, * resize the image to fill the square while preserving aspect ratio, * process nested directories, I can prepare those versions as well. === User: These errors are reported with script is run: convert-im6.q16: unable to open image identify': No such file or director… === These errors are reported with script is run: convert-im6.q16: unable to open image identify': No such file or directory @ error/blob.c/OpenBlob/2924. convert-im6.q16: no decode delegate for this image format ' @ error/constitute.c/ReadImage/580. === Assistant: The error occurs because in the earlier script the command: === The error occurs because in the earlier script the command: <syntaxhighlight>$IM_CMD identify -format "%w %h" "$f" </syntaxhighlight> is incorrect. convert cannot run identify. ImageMagick has separate commands: * convert (or magick convert) * identify (or magick identify) Your system is interpreting identify as a filename and failing. Below is a corrected, fully working script. === Corrected Script: squareify.sh === <syntaxhighlight lang="bash">#!/bin/bash set -e === Ensure ImageMagick is installed === if ! command -v convert >/dev/null 2>&1 && ! command -v magick >/dev/null 2>&1; then echo "Error: ImageMagick not installed." exit 1 fi === Select correct ImageMagick commands === if command -v magick >/dev/null 2>&1; then CONVERT="magick convert" IDENTIFY="magick identify" else CONVERT="convert" IDENTIFY="identify" fi if [ $# -lt 1 ]; then echo "Usage: $0 <file-or-directory> [...]" exit 1 fi process_file() { f="$1" ext="${f##*.}" base="${f%.*}" out="${base}_square.${ext}" # Read dimensions properly read width height <<< $($IDENTIFY -format "%w %h" "$f") # Determine largest side if [ "$width" -gt "$height" ]; then size=$width else size=$height fi echo "Squaring: $f -> $out (canvas ${size}x${size})" # Create square output $CONVERT "$f" \ -gravity center \ -background white \ -extent "${size}x${size}" \ "$out" } === Loop over input parameters === for input in "$@"; do if [ -d "$input" ]; then shopt -s nullglob for f in "$input"/*.{png,PNG,jpg,JPG,jpeg,JPEG}; do process_file "$f" done shopt -u nullglob elif [ -f "$input" ]; then process_file "$input" else echo "Warning: '$input' is not a valid file or directory. Skipping." fi done </syntaxhighlight> === Why This Works === The fix is simply: * Use identify explicitly instead of $IM_CMD identify. * Use magick identify if your system uses the modern magick wrapper. This avoids the “unable to open image identify” error because now the command is interpreted correctly. If you want, I can also provide: * a version that overwrites the original files, * a version that adds padding proportionally, * a version that makes all images the same fixed square size (e.g., 1024×1024). Just let me know. == References == <references /> [[Category:ChatGPT Shares]] [[Category:openai]]
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Cite web
(
edit
)
Module:Citation/CS1
(
edit
)
Module:Citation/CS1/COinS
(
edit
)
Module:Citation/CS1/Configuration
(
edit
)
Module:Citation/CS1/Date validation
(
edit
)
Module:Citation/CS1/Identifiers
(
edit
)
Module:Citation/CS1/Utilities
(
edit
)
Module:Citation/CS1/Whitelist
(
edit
)
Module:Citation/CS1/styles.css
(
edit
)