Size of .jpg and .jpeg increased after compression [closed]












0















I am trying to compress the images using the K-means algorithm but the size of some .jpg images have increased after compression. How can I do it for .jpg and .jpeg images. I have saved the jpg and jpeg images in png format before applying the compression



for f in os.listdir('.'):
if f.endswith('.png'):
image = io.imread(f,0)
rows = image.shape[0]
cols = image.shape[1]

pixels = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
kmeans = MiniBatchKMeans(n_clusters=128, n_init=10, max_iter=200)
kmeans.fit(pixels)

clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
labels = np.asarray(kmeans.labels_, dtype=np.uint8)
labels = labels.reshape(rows, cols)
colored = clusters[labels]

# np.save('codebook'+f+'.npy', clusters)
io.imsave('compressed_' + f, colored)

img1 = mpimg.imread(f,0)
img2 = mpimg.imread('compressed_' + f,0)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
ax1.imshow(img1)
ax1.set_title('Original image')
ax2.imshow(img2)
ax2.set_title('Compressed image')
plt.show()

fig, ax = plt.subplots(2, 1)

img = cv2.imread(f, 0)
ax[0].hist(img.ravel(), 256, [0, 256]);
ax[0].set_title("Original image")
img1 = cv2.imread('compressed_' + f,0)
ax[1].hist(img1.ravel(), 256, [0, 256]);
ax[1].set_title("Compressed image")
plt.show()

print('size of original image: ', int(os.stat(f).st_size / 1024), 'kB')
print('size of compressed image:', int(os.stat('compressed_' + f).st_size / 1024), 'kB')


enter image description here










share|improve this question















closed as unclear what you're asking by Gábor Bakos, tripleee, Michael Dodd, ewolden, Pearly Spencer Nov 19 '18 at 11:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.




















    0















    I am trying to compress the images using the K-means algorithm but the size of some .jpg images have increased after compression. How can I do it for .jpg and .jpeg images. I have saved the jpg and jpeg images in png format before applying the compression



    for f in os.listdir('.'):
    if f.endswith('.png'):
    image = io.imread(f,0)
    rows = image.shape[0]
    cols = image.shape[1]

    pixels = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
    kmeans = MiniBatchKMeans(n_clusters=128, n_init=10, max_iter=200)
    kmeans.fit(pixels)

    clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
    labels = np.asarray(kmeans.labels_, dtype=np.uint8)
    labels = labels.reshape(rows, cols)
    colored = clusters[labels]

    # np.save('codebook'+f+'.npy', clusters)
    io.imsave('compressed_' + f, colored)

    img1 = mpimg.imread(f,0)
    img2 = mpimg.imread('compressed_' + f,0)
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
    ax1.imshow(img1)
    ax1.set_title('Original image')
    ax2.imshow(img2)
    ax2.set_title('Compressed image')
    plt.show()

    fig, ax = plt.subplots(2, 1)

    img = cv2.imread(f, 0)
    ax[0].hist(img.ravel(), 256, [0, 256]);
    ax[0].set_title("Original image")
    img1 = cv2.imread('compressed_' + f,0)
    ax[1].hist(img1.ravel(), 256, [0, 256]);
    ax[1].set_title("Compressed image")
    plt.show()

    print('size of original image: ', int(os.stat(f).st_size / 1024), 'kB')
    print('size of compressed image:', int(os.stat('compressed_' + f).st_size / 1024), 'kB')


    enter image description here










    share|improve this question















    closed as unclear what you're asking by Gábor Bakos, tripleee, Michael Dodd, ewolden, Pearly Spencer Nov 19 '18 at 11:29


    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















      0












      0








      0








      I am trying to compress the images using the K-means algorithm but the size of some .jpg images have increased after compression. How can I do it for .jpg and .jpeg images. I have saved the jpg and jpeg images in png format before applying the compression



      for f in os.listdir('.'):
      if f.endswith('.png'):
      image = io.imread(f,0)
      rows = image.shape[0]
      cols = image.shape[1]

      pixels = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
      kmeans = MiniBatchKMeans(n_clusters=128, n_init=10, max_iter=200)
      kmeans.fit(pixels)

      clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
      labels = np.asarray(kmeans.labels_, dtype=np.uint8)
      labels = labels.reshape(rows, cols)
      colored = clusters[labels]

      # np.save('codebook'+f+'.npy', clusters)
      io.imsave('compressed_' + f, colored)

      img1 = mpimg.imread(f,0)
      img2 = mpimg.imread('compressed_' + f,0)
      fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
      ax1.imshow(img1)
      ax1.set_title('Original image')
      ax2.imshow(img2)
      ax2.set_title('Compressed image')
      plt.show()

      fig, ax = plt.subplots(2, 1)

      img = cv2.imread(f, 0)
      ax[0].hist(img.ravel(), 256, [0, 256]);
      ax[0].set_title("Original image")
      img1 = cv2.imread('compressed_' + f,0)
      ax[1].hist(img1.ravel(), 256, [0, 256]);
      ax[1].set_title("Compressed image")
      plt.show()

      print('size of original image: ', int(os.stat(f).st_size / 1024), 'kB')
      print('size of compressed image:', int(os.stat('compressed_' + f).st_size / 1024), 'kB')


      enter image description here










      share|improve this question
















      I am trying to compress the images using the K-means algorithm but the size of some .jpg images have increased after compression. How can I do it for .jpg and .jpeg images. I have saved the jpg and jpeg images in png format before applying the compression



      for f in os.listdir('.'):
      if f.endswith('.png'):
      image = io.imread(f,0)
      rows = image.shape[0]
      cols = image.shape[1]

      pixels = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
      kmeans = MiniBatchKMeans(n_clusters=128, n_init=10, max_iter=200)
      kmeans.fit(pixels)

      clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
      labels = np.asarray(kmeans.labels_, dtype=np.uint8)
      labels = labels.reshape(rows, cols)
      colored = clusters[labels]

      # np.save('codebook'+f+'.npy', clusters)
      io.imsave('compressed_' + f, colored)

      img1 = mpimg.imread(f,0)
      img2 = mpimg.imread('compressed_' + f,0)
      fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
      ax1.imshow(img1)
      ax1.set_title('Original image')
      ax2.imshow(img2)
      ax2.set_title('Compressed image')
      plt.show()

      fig, ax = plt.subplots(2, 1)

      img = cv2.imread(f, 0)
      ax[0].hist(img.ravel(), 256, [0, 256]);
      ax[0].set_title("Original image")
      img1 = cv2.imread('compressed_' + f,0)
      ax[1].hist(img1.ravel(), 256, [0, 256]);
      ax[1].set_title("Compressed image")
      plt.show()

      print('size of original image: ', int(os.stat(f).st_size / 1024), 'kB')
      print('size of compressed image:', int(os.stat('compressed_' + f).st_size / 1024), 'kB')


      enter image description here







      python k-means image-compression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 10:26









      Matthieu Brucher

      13.4k32140




      13.4k32140










      asked Nov 19 '18 at 7:20









      Samrat ShresthaSamrat Shrestha

      326




      326




      closed as unclear what you're asking by Gábor Bakos, tripleee, Michael Dodd, ewolden, Pearly Spencer Nov 19 '18 at 11:29


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






      closed as unclear what you're asking by Gábor Bakos, tripleee, Michael Dodd, ewolden, Pearly Spencer Nov 19 '18 at 11:29


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          If I understand your approach correctly, you use K-means clustering to reduce the number of colors in the image by finding areas (clusters) of similar colored pixels and grouping them together.



          While this can theoretically reduce the filesize of the image, saving it again as a jpeg applies a whole different image compression algorithm on the reduced image, which cannot take full advantage of big areas of the same color with sharp edges. It will necessarily "blur" the image, and in some cases, this can even lead to an increased file size.



          Try storing the reduced image in a different format (e.g. as a png file), that can make use of big, evenly colored areas.






          share|improve this answer
























          • I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

            – Samrat Shrestha
            Nov 19 '18 at 8:26











          • Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

            – tripleee
            Nov 19 '18 at 8:28






          • 1





            @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

            – Christian König
            Nov 19 '18 at 8:37











          • If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

            – tripleee
            Nov 19 '18 at 9:55











          • Sorry, I added it.

            – Samrat Shrestha
            Nov 19 '18 at 10:07


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If I understand your approach correctly, you use K-means clustering to reduce the number of colors in the image by finding areas (clusters) of similar colored pixels and grouping them together.



          While this can theoretically reduce the filesize of the image, saving it again as a jpeg applies a whole different image compression algorithm on the reduced image, which cannot take full advantage of big areas of the same color with sharp edges. It will necessarily "blur" the image, and in some cases, this can even lead to an increased file size.



          Try storing the reduced image in a different format (e.g. as a png file), that can make use of big, evenly colored areas.






          share|improve this answer
























          • I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

            – Samrat Shrestha
            Nov 19 '18 at 8:26











          • Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

            – tripleee
            Nov 19 '18 at 8:28






          • 1





            @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

            – Christian König
            Nov 19 '18 at 8:37











          • If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

            – tripleee
            Nov 19 '18 at 9:55











          • Sorry, I added it.

            – Samrat Shrestha
            Nov 19 '18 at 10:07
















          1














          If I understand your approach correctly, you use K-means clustering to reduce the number of colors in the image by finding areas (clusters) of similar colored pixels and grouping them together.



          While this can theoretically reduce the filesize of the image, saving it again as a jpeg applies a whole different image compression algorithm on the reduced image, which cannot take full advantage of big areas of the same color with sharp edges. It will necessarily "blur" the image, and in some cases, this can even lead to an increased file size.



          Try storing the reduced image in a different format (e.g. as a png file), that can make use of big, evenly colored areas.






          share|improve this answer
























          • I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

            – Samrat Shrestha
            Nov 19 '18 at 8:26











          • Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

            – tripleee
            Nov 19 '18 at 8:28






          • 1





            @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

            – Christian König
            Nov 19 '18 at 8:37











          • If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

            – tripleee
            Nov 19 '18 at 9:55











          • Sorry, I added it.

            – Samrat Shrestha
            Nov 19 '18 at 10:07














          1












          1








          1







          If I understand your approach correctly, you use K-means clustering to reduce the number of colors in the image by finding areas (clusters) of similar colored pixels and grouping them together.



          While this can theoretically reduce the filesize of the image, saving it again as a jpeg applies a whole different image compression algorithm on the reduced image, which cannot take full advantage of big areas of the same color with sharp edges. It will necessarily "blur" the image, and in some cases, this can even lead to an increased file size.



          Try storing the reduced image in a different format (e.g. as a png file), that can make use of big, evenly colored areas.






          share|improve this answer













          If I understand your approach correctly, you use K-means clustering to reduce the number of colors in the image by finding areas (clusters) of similar colored pixels and grouping them together.



          While this can theoretically reduce the filesize of the image, saving it again as a jpeg applies a whole different image compression algorithm on the reduced image, which cannot take full advantage of big areas of the same color with sharp edges. It will necessarily "blur" the image, and in some cases, this can even lead to an increased file size.



          Try storing the reduced image in a different format (e.g. as a png file), that can make use of big, evenly colored areas.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 7:29









          Christian KönigChristian König

          2,461918




          2,461918













          • I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

            – Samrat Shrestha
            Nov 19 '18 at 8:26











          • Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

            – tripleee
            Nov 19 '18 at 8:28






          • 1





            @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

            – Christian König
            Nov 19 '18 at 8:37











          • If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

            – tripleee
            Nov 19 '18 at 9:55











          • Sorry, I added it.

            – Samrat Shrestha
            Nov 19 '18 at 10:07



















          • I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

            – Samrat Shrestha
            Nov 19 '18 at 8:26











          • Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

            – tripleee
            Nov 19 '18 at 8:28






          • 1





            @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

            – Christian König
            Nov 19 '18 at 8:37











          • If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

            – tripleee
            Nov 19 '18 at 9:55











          • Sorry, I added it.

            – Samrat Shrestha
            Nov 19 '18 at 10:07

















          I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

          – Samrat Shrestha
          Nov 19 '18 at 8:26





          I have converted the jpeg files into png before applying the compression algorithm but still, the images are of larger size.

          – Samrat Shrestha
          Nov 19 '18 at 8:26













          Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

          – tripleee
          Nov 19 '18 at 8:28





          Without access to your sample image I don't think anything more detailed or specific can be stated. Perhaps you could make your program print more information about what it actually does. How many clusters did it find, how large are they, what did it do with them?

          – tripleee
          Nov 19 '18 at 8:28




          1




          1





          @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

          – Christian König
          Nov 19 '18 at 8:37





          @SamratShrestha - it is not about the filetype of the image before compression, it is about the filetype you use to store the compressed image. If you used the most elaborate compression algorithm and store the results as a bmp file, you would throw everything gained by your compression algorithm away. Same thing can happen (on a much smaller scale) when saving as jpeg.

          – Christian König
          Nov 19 '18 at 8:37













          If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

          – tripleee
          Nov 19 '18 at 9:55





          If you have posted your sample image somewhere, you forgot to tell us where. Repeating text from the question in a comment seems rather superfluous.

          – tripleee
          Nov 19 '18 at 9:55













          Sorry, I added it.

          – Samrat Shrestha
          Nov 19 '18 at 10:07





          Sorry, I added it.

          – Samrat Shrestha
          Nov 19 '18 at 10:07



          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?