-2

I wonder how to compress (if that's what it's called) the image without loosing the quality (or loosing a little)? I want it to be compressed in order to it being loaded very quickly on my website. For example: the original image is 10 MB, I want to resize it and compress so it be like 2-3 MBs, or even better about 900KBs. How to do that besides resizing. What is the difference between resizing and compression? Which of them makes the image size (in MB) smaller?

I saw somewhere that this compresses the image, but does this actually what I want?

cv2.imwrite(self.image.path, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

Thanks in advance!

Christoph Rackwitz
  • 5,130
  • 3
  • 17
  • 27
  • What are the dimensions of your image? What format is it right now? – Bijay Regmi May 20 '22 at 21:27
  • Resizing will always get you to the file size you want. Compression may never get to that file size if it is too low for the compression method, especially for non-JPG (non-lossy) compressions such as PNG. So depending upon your desired file size, you may have to use resizing. Compression will not change the dimensions, but resizing will make the dimensions smaller. – fmw42 May 20 '22 at 22:31
  • all images will be in png format. so the only way to make it smaller is resizing it? – Beginner Programmer May 20 '22 at 22:42
  • 2
    PNGs are already compressed. A 10MB PNG must be an enormous image, beyond 4K HD. What is the image size, in pixels? – Tim Roberts May 20 '22 at 23:53
  • "What is the difference between resizing and compression?" This is a question about how computers work, specifically about how they represent images. It stands completely separate from the code, and is something that is best answered by doing research. Even as a specific question, it would make more sense on https://superuser.com or [cs.se]. That said, the extent that "the original image" can be compressed will depend on *what that image is* - did you get it from reading a PNG file? Then that file *was already compressed.* – Karl Knechtel May 21 '22 at 00:03
  • That said, it is possible for compressed image formats like PNG to try harder (or less hard) to compress the image; and it is possible for them to contain extra data that you may or may not care about besides the actual image. – Karl Knechtel May 21 '22 at 00:04
  • Some compression optimizers process the image with every possible compression setting for PNG and find the one that gives the smallest image size. That will be rather time consuming. Also it may still not compress to the size you want. – fmw42 May 21 '22 at 03:48
  • okay, thank yall i ll definitely consider everything above – Beginner Programmer May 21 '22 at 10:13

0 Answers0