Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement python Picture format conversion script

Shulou Source: shulou.com Published: 2022-06-02 04:18:29 09月11日 Update

This article is about how to implement the python image format conversion script. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1.cv2

After pip install opencv-python, you can import cv2.

1.1.The import osimport cv2import sysimport numpy as np1.2 path setting path = r "C:\\ Users\\ 86775\\ PycharmProjects\\ pythonProject2\\ ResNet\\ test_change\\" path3 = r "C:\\ Users\\ 86775\ PycharmProjects\ pythonProject2\\ ResNet\\ test_change2\\" 1.3 reformat and retransmit

1.3.1 use os.path.splitext () to judge

Images = os.listdir (path) for i in os.listdir (path): print (os.path.splitext (I)) # ('34474006827920603,' .png') if os.path.splitext (I) [1] = ".jpeg": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".jpeg", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif os.path.splitext (I) [1] = ".png": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".png", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif os.path.splitext (I) [1] = ".JPG": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".jpg", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif os.path.splitext (I) [1] = ".PNG": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".PNG", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif os.path.splitext (I) [1] = ".jpg": img = cv2.imread (path + I) # print (img) cv2.imwrite (path3 + I, img)

1.3.2 use split () to judge

Images = os.listdir (path) for i in os.listdir (path): print (i.split (".") If i.split (".") [1] = "jpeg": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".jpeg", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif i.split (".") [1] = "png": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".png", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif i.split (".") [1] = "JPG": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".jpg", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif i.split (".") [1] = "PNG": img = cv2.imread (path + I) # print (img) new_imagename = i.replace (".PNG", ".jpg") cv2.imwrite (path3 + new_imagename) Img) elif i.split (".") [1] = "jpg": img = cv2.imread (path + I) # print (img) cv2.imwrite (path3 + I, img) 1.4 View the results

2.pillow

After pip install pillow, you can from PIL import Image.

2.1 Guide package import osfrom PIL import Image2.2 path setting path2 = r "C:\\ Users\\ 86775\\ PycharmProjects\\ pythonProject2\\ ResNet\\ test_change\\" path3 = r "C:\\ Users\\ 86775\ PycharmProjects\ pythonProject2\\ ResNet\\ test_change2\\" 2.3 write format modification function

RGBA means red, green, blue, Alpha color space, Alpha refers to transparency, and JPG does not support transparency.

Therefore, if other formats are not changed, jpg can be directly saved to .png format. If you switch to jpg, you need to discard Alpha.

Img = img.convert ("RGB"), converted to RGB to ensure the same number of channels as jpg format. It is also an error prevention mechanism for image type conversion.

Def jpeg2jpg (path_in, path_out): img = Image.open (path_in) img = img.convert ("RGB") img.save (path_out, "JPEG", quality=80, optimize=True, progressive=True) def png2jpg (path_in, path_out): img = Image.open (path_in) img = img.convert ("RGB") img.save (path_out, "JPEG", quality=80, optimize=True, progressive=True) def JPG2jpg Path_out): img = Image.open (path_in) img = img.convert ("RGB") img.save (path_out, "JPEG", quality=80, optimize=True, progressive=True) def PNG2jpg (path_in, path_out): img = Image.open (path_in) img = img.convert ("RGB") img.save (path_out, "JPEG", quality=80, optimize=True, progressive=True) def jpg2jpg (path_in Path_out): img = Image.open (path_in) img = img.convert ("RGB") img.save (path_out, "JPEG", quality=80, optimize=True Progressive=True) 2.4commencing conversion images = os.listdir (path2) for i in images: print (I) if os.path.splitext (I) [1] = = ".jpeg": source = path2 + "\\" + str (i.split (".") [0]) + ".jpeg" target = path3 + "\" + str (i.split (".") [0]) + ".jpg" jpeg2jpg (source) Target) elif os.path.splitext (I) [1] = ".png": source = path2 + "\\" + str (i.split (".") [0]) + ".png" target = path3 + "\" + str (i.split (".") [0]) + ".jpg" png2jpg (source) Target) elif os.path.splitext (I) [1] = ".JPG": source = path2 + "\\" + str (i.split (".") [0]) + ".JPG" target = path3 + "\" + str (i.split (".") [0]) + ".jpg" JPG2jpg (source) Target) elif os.path.splitext (I) [1] = ".PNG": source = path2 + "\\" + str (i.split (".") [0]) + ".PNG" target = path3 + "\" + str (i.split (".") [0]) + ".jpg" PNG2jpg (source) Target) elif os.path.splitext (I) [1] = = ".jpg": source = path2 + "\" + str (i.split (".") [0]) + ".jpg" target = path3 + "\" + str (i.split (".") [0]) + ".jpg" jpg2jpg (source, target) 2.5.View the result

Thank you for reading! On "how to achieve python picture format conversion script" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Format picture script content more article result path transparency good practical same function meaning article mechanism see knowledge space type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Linux Redmi MySQL