How to convert all the images in a directory to jpg?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite
1












I have so many different images in a directory with sequential names 1 2 3 4 ... I was wondering if it was possible to convert all of them to jpg and resize them all to 900x900 without preserving aspect ratio.



What I tried for the first part is:



$ mogrify -format jpg -- *


But when I do this memory usage increases so quickly and then the process in killed.
What I tried for resizing is:



$ mogrify -resize 900x900 -- *
mogrify: insufficient image data in file `109.jpg' @ error/jpeg.c/ReadJPEGImage/1039.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.


As I've said the names of the files are sequential numbers. I also have the following:



$ file * | grep -Po "^d*: *K[^:,]*(?=,)" | sort | uniq
ASCII text
GIF image data
JPEG image data
PNG image data


So What is the problem? How can I fix it?










share|improve this question





















  • Do you want to keep or remove the original images after processing?
    – Byte Commander
    Feb 5 at 10:59










  • @Byte I want to remove them.
    – yukashima huksay
    Feb 5 at 11:02














up vote
2
down vote

favorite
1












I have so many different images in a directory with sequential names 1 2 3 4 ... I was wondering if it was possible to convert all of them to jpg and resize them all to 900x900 without preserving aspect ratio.



What I tried for the first part is:



$ mogrify -format jpg -- *


But when I do this memory usage increases so quickly and then the process in killed.
What I tried for resizing is:



$ mogrify -resize 900x900 -- *
mogrify: insufficient image data in file `109.jpg' @ error/jpeg.c/ReadJPEGImage/1039.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.


As I've said the names of the files are sequential numbers. I also have the following:



$ file * | grep -Po "^d*: *K[^:,]*(?=,)" | sort | uniq
ASCII text
GIF image data
JPEG image data
PNG image data


So What is the problem? How can I fix it?










share|improve this question





















  • Do you want to keep or remove the original images after processing?
    – Byte Commander
    Feb 5 at 10:59










  • @Byte I want to remove them.
    – yukashima huksay
    Feb 5 at 11:02












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have so many different images in a directory with sequential names 1 2 3 4 ... I was wondering if it was possible to convert all of them to jpg and resize them all to 900x900 without preserving aspect ratio.



What I tried for the first part is:



$ mogrify -format jpg -- *


But when I do this memory usage increases so quickly and then the process in killed.
What I tried for resizing is:



$ mogrify -resize 900x900 -- *
mogrify: insufficient image data in file `109.jpg' @ error/jpeg.c/ReadJPEGImage/1039.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.


As I've said the names of the files are sequential numbers. I also have the following:



$ file * | grep -Po "^d*: *K[^:,]*(?=,)" | sort | uniq
ASCII text
GIF image data
JPEG image data
PNG image data


So What is the problem? How can I fix it?










share|improve this question













I have so many different images in a directory with sequential names 1 2 3 4 ... I was wondering if it was possible to convert all of them to jpg and resize them all to 900x900 without preserving aspect ratio.



What I tried for the first part is:



$ mogrify -format jpg -- *


But when I do this memory usage increases so quickly and then the process in killed.
What I tried for resizing is:



$ mogrify -resize 900x900 -- *
mogrify: insufficient image data in file `109.jpg' @ error/jpeg.c/ReadJPEGImage/1039.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.


As I've said the names of the files are sequential numbers. I also have the following:



$ file * | grep -Po "^d*: *K[^:,]*(?=,)" | sort | uniq
ASCII text
GIF image data
JPEG image data
PNG image data


So What is the problem? How can I fix it?







format images resize convert mogrify






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 5 at 10:21









yukashima huksay

272216




272216











  • Do you want to keep or remove the original images after processing?
    – Byte Commander
    Feb 5 at 10:59










  • @Byte I want to remove them.
    – yukashima huksay
    Feb 5 at 11:02
















  • Do you want to keep or remove the original images after processing?
    – Byte Commander
    Feb 5 at 10:59










  • @Byte I want to remove them.
    – yukashima huksay
    Feb 5 at 11:02















Do you want to keep or remove the original images after processing?
– Byte Commander
Feb 5 at 10:59




Do you want to keep or remove the original images after processing?
– Byte Commander
Feb 5 at 10:59












@Byte I want to remove them.
– yukashima huksay
Feb 5 at 11:02




@Byte I want to remove them.
– yukashima huksay
Feb 5 at 11:02










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










I'd recommend using the convert tool from ImageMagick:





convert input.png -resize 900x900 output.jpg


The -resize option should be pretty obvious and the output file format is automatically determined using its filename extension.



To run this on all files in the current directory, try this:



for inputfile in ./* ; do
outputfile="$inputfile%.*.jpg"
convert "$inputfile" -resize 900x900 "$outputfile" &&
[[ -e "$outputfile" && "$inputfile" != "$outputfile" ]] && rm "$inputfile"
done


This will take all files fromt he current directory (no matter the file type), and for each input file create the respective output file name by stripping the old extension and adding ".jpg" instead. Then it uses convert as described above to resize and convert the image, which creates a new file and leaves the original as it is. If that was successful (&&), check if the output file exists and if the input file name is different from the output file name (e.g. if one of the original files already was a jpg). Now if these conditions are met, we assume we can delete the input file.






share|improve this answer




















  • why should this work if mogrify -format jpg -- * doesn't
    – yukashima huksay
    Feb 8 at 9:18










  • I think the problem is that the files does not have extensions
    – yukashima huksay
    Feb 8 at 9:21






  • 1




    You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
    – Byte Commander
    Feb 8 at 9:31










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003218%2fhow-to-convert-all-the-images-in-a-directory-to-jpg%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










I'd recommend using the convert tool from ImageMagick:





convert input.png -resize 900x900 output.jpg


The -resize option should be pretty obvious and the output file format is automatically determined using its filename extension.



To run this on all files in the current directory, try this:



for inputfile in ./* ; do
outputfile="$inputfile%.*.jpg"
convert "$inputfile" -resize 900x900 "$outputfile" &&
[[ -e "$outputfile" && "$inputfile" != "$outputfile" ]] && rm "$inputfile"
done


This will take all files fromt he current directory (no matter the file type), and for each input file create the respective output file name by stripping the old extension and adding ".jpg" instead. Then it uses convert as described above to resize and convert the image, which creates a new file and leaves the original as it is. If that was successful (&&), check if the output file exists and if the input file name is different from the output file name (e.g. if one of the original files already was a jpg). Now if these conditions are met, we assume we can delete the input file.






share|improve this answer




















  • why should this work if mogrify -format jpg -- * doesn't
    – yukashima huksay
    Feb 8 at 9:18










  • I think the problem is that the files does not have extensions
    – yukashima huksay
    Feb 8 at 9:21






  • 1




    You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
    – Byte Commander
    Feb 8 at 9:31














up vote
4
down vote



accepted










I'd recommend using the convert tool from ImageMagick:





convert input.png -resize 900x900 output.jpg


The -resize option should be pretty obvious and the output file format is automatically determined using its filename extension.



To run this on all files in the current directory, try this:



for inputfile in ./* ; do
outputfile="$inputfile%.*.jpg"
convert "$inputfile" -resize 900x900 "$outputfile" &&
[[ -e "$outputfile" && "$inputfile" != "$outputfile" ]] && rm "$inputfile"
done


This will take all files fromt he current directory (no matter the file type), and for each input file create the respective output file name by stripping the old extension and adding ".jpg" instead. Then it uses convert as described above to resize and convert the image, which creates a new file and leaves the original as it is. If that was successful (&&), check if the output file exists and if the input file name is different from the output file name (e.g. if one of the original files already was a jpg). Now if these conditions are met, we assume we can delete the input file.






share|improve this answer




















  • why should this work if mogrify -format jpg -- * doesn't
    – yukashima huksay
    Feb 8 at 9:18










  • I think the problem is that the files does not have extensions
    – yukashima huksay
    Feb 8 at 9:21






  • 1




    You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
    – Byte Commander
    Feb 8 at 9:31












up vote
4
down vote



accepted







up vote
4
down vote



accepted






I'd recommend using the convert tool from ImageMagick:





convert input.png -resize 900x900 output.jpg


The -resize option should be pretty obvious and the output file format is automatically determined using its filename extension.



To run this on all files in the current directory, try this:



for inputfile in ./* ; do
outputfile="$inputfile%.*.jpg"
convert "$inputfile" -resize 900x900 "$outputfile" &&
[[ -e "$outputfile" && "$inputfile" != "$outputfile" ]] && rm "$inputfile"
done


This will take all files fromt he current directory (no matter the file type), and for each input file create the respective output file name by stripping the old extension and adding ".jpg" instead. Then it uses convert as described above to resize and convert the image, which creates a new file and leaves the original as it is. If that was successful (&&), check if the output file exists and if the input file name is different from the output file name (e.g. if one of the original files already was a jpg). Now if these conditions are met, we assume we can delete the input file.






share|improve this answer












I'd recommend using the convert tool from ImageMagick:





convert input.png -resize 900x900 output.jpg


The -resize option should be pretty obvious and the output file format is automatically determined using its filename extension.



To run this on all files in the current directory, try this:



for inputfile in ./* ; do
outputfile="$inputfile%.*.jpg"
convert "$inputfile" -resize 900x900 "$outputfile" &&
[[ -e "$outputfile" && "$inputfile" != "$outputfile" ]] && rm "$inputfile"
done


This will take all files fromt he current directory (no matter the file type), and for each input file create the respective output file name by stripping the old extension and adding ".jpg" instead. Then it uses convert as described above to resize and convert the image, which creates a new file and leaves the original as it is. If that was successful (&&), check if the output file exists and if the input file name is different from the output file name (e.g. if one of the original files already was a jpg). Now if these conditions are met, we assume we can delete the input file.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 5 at 11:18









Byte Commander

59.9k26159269




59.9k26159269











  • why should this work if mogrify -format jpg -- * doesn't
    – yukashima huksay
    Feb 8 at 9:18










  • I think the problem is that the files does not have extensions
    – yukashima huksay
    Feb 8 at 9:21






  • 1




    You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
    – Byte Commander
    Feb 8 at 9:31
















  • why should this work if mogrify -format jpg -- * doesn't
    – yukashima huksay
    Feb 8 at 9:18










  • I think the problem is that the files does not have extensions
    – yukashima huksay
    Feb 8 at 9:21






  • 1




    You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
    – Byte Commander
    Feb 8 at 9:31















why should this work if mogrify -format jpg -- * doesn't
– yukashima huksay
Feb 8 at 9:18




why should this work if mogrify -format jpg -- * doesn't
– yukashima huksay
Feb 8 at 9:18












I think the problem is that the files does not have extensions
– yukashima huksay
Feb 8 at 9:21




I think the problem is that the files does not have extensions
– yukashima huksay
Feb 8 at 9:21




1




1




You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
– Byte Commander
Feb 8 at 9:31




You said memory usage grew too huge, so I replaced your single call with all files at once with a loop processing all images separately. Also I think convert is better as you want to rename the files (extension) when converting to jpg, I assume. Also convert should recognize the format based on the content (magic number) if no extension is given.
– Byte Commander
Feb 8 at 9:31

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003218%2fhow-to-convert-all-the-images-in-a-directory-to-jpg%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491